From 27d86d39680c735f7a630a0992147caa8dfad9e8 Mon Sep 17 00:00:00 2001 From: Maple Buice Date: Thu, 30 Nov 2023 14:49:59 -0500 Subject: [PATCH] Update PrettyPrintFilter to produce more useful output by serializing objects into JSON --- .../jinjava/lib/filter/PrettyPrintFilter.java | 57 +------------------ .../lib/filter/PrettyPrintFilterTest.java | 50 ++++++++++++++-- 2 files changed, 49 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java index 81af674d3..0e7844ee8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java @@ -1,25 +1,13 @@ package com.hubspot.jinjava.lib.filter; -import static com.hubspot.jinjava.util.Logging.ENGINE_LOG; - +import com.fasterxml.jackson.databind.node.POJONode; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; -import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.objects.date.PyishDate; -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.LinkedList; -import java.util.List; import java.util.Map; import java.util.Objects; -import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.commons.lang3.StringUtils; @JinjavaDoc( value = "Pretty print a variable. Useful for debugging.", @@ -60,50 +48,11 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) ) { varStr = Objects.toString(var); } else { - varStr = objPropsToString(var); + varStr = new POJONode(var).toPrettyString(); } - return StringEscapeUtils.escapeHtml4( + return EscapeFilter.escapeHtmlEntities( "{% raw %}(" + var.getClass().getSimpleName() + ": " + varStr + "){% endraw %}" ); } - - private String objPropsToString(Object var) { - List props = new LinkedList<>(); - - try { - BeanInfo beanInfo = Introspector.getBeanInfo(var.getClass()); - - for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { - try { - if (pd.getPropertyType() != null && pd.getPropertyType().equals(Class.class)) { - continue; - } - - Method readMethod = pd.getReadMethod(); - if ( - readMethod != null && !readMethod.getDeclaringClass().equals(Object.class) - ) { - props.add(pd.getName() + "=" + readMethod.invoke(var)); - } - } catch (Exception e) { - if ( - e instanceof InvocationTargetException && - ( - (InvocationTargetException) e - ).getTargetException() instanceof DeferredValueException - ) { - throw (DeferredValueException) ( - (InvocationTargetException) e - ).getTargetException(); - } - ENGINE_LOG.error("Error reading bean value", e); - } - } - } catch (IntrospectionException e) { - ENGINE_LOG.error("Error inspecting bean", e); - } - - return '{' + StringUtils.join(props, ", ") + '}'; - } } diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java index 2a4d028b5..822a83c97 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java @@ -2,8 +2,13 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import com.hubspot.jinjava.Jinjava; +import com.hubspot.jinjava.JinjavaConfig; +import com.hubspot.jinjava.interpret.Context; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.objects.date.PyishDate; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -11,17 +16,21 @@ import org.junit.Test; public class PrettyPrintFilterTest { + JinjavaInterpreter i; PrettyPrintFilter f; @Before public void setup() { + JinjavaConfig config = JinjavaConfig.newBuilder().build(); + Jinjava jinjava = new Jinjava(config); + Context context = jinjava.getGlobalContext(); + i = new JinjavaInterpreter(jinjava, context, config); f = new PrettyPrintFilter(); } @Test public void ppString() { - assertThat(f.filter("foobar", null)) - .isEqualTo("{% raw %}(String: foobar){% endraw %}"); + assertThat(f.filter("foobar", i)).isEqualTo("{% raw %}(String: foobar){% endraw %}"); } @Test @@ -54,10 +63,27 @@ public void ppList() { @Test public void ppObject() { - assertThat(f.filter(new MyClass(), null)) - .isEqualTo("{% raw %}(MyClass: {bar=123, foo=foofoo}){% endraw %}"); + MyClass myClass = new MyClass(); + assertThat(f.filter(myClass, i)) + .isEqualTo( + String.format( + "{%% raw %%}(MyClass: {\n" + + " "foo" : "%s",\n" + + " "bar" : %d,\n" + + " "nestedClass" : {\n" + + " "fooField" : "%s",\n" + + " "barField" : %d\n" + + " }\n" + + "}){%% endraw %%}", + myClass.getFoo(), + myClass.getBar(), + myClass.getNestedClass().getFooField(), + myClass.getNestedClass().getBarField() + ) + ); } + @JsonPropertyOrder({ "foo", "bar", "nestedClass" }) public static class MyClass { public String getFoo() { @@ -67,5 +93,21 @@ public String getFoo() { public int getBar() { return 123; } + + public MyNestedClass getNestedClass() { + return new MyNestedClass(); + } + } + + @JsonPropertyOrder({ "fooField", "barField" }) + public static class MyNestedClass { + + public String getFooField() { + return "foofieldfoofield"; + } + + public int getBarField() { + return 123; + } } }