Skip to content

Commit ddd35c0

Browse files
authored
Merge pull request #1132 from HubSpot/958-update-pprint
Update PrettyPrintFilter to produce more useful output by serializing objects into JSON
2 parents bf66df2 + 27d86d3 commit ddd35c0

File tree

2 files changed

+49
-58
lines changed

2 files changed

+49
-58
lines changed
Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
package com.hubspot.jinjava.lib.filter;
22

3-
import static com.hubspot.jinjava.util.Logging.ENGINE_LOG;
4-
3+
import com.fasterxml.jackson.databind.node.POJONode;
54
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
65
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
76
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
8-
import com.hubspot.jinjava.interpret.DeferredValueException;
97
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
108
import com.hubspot.jinjava.objects.date.PyishDate;
11-
import java.beans.BeanInfo;
12-
import java.beans.IntrospectionException;
13-
import java.beans.Introspector;
14-
import java.beans.PropertyDescriptor;
15-
import java.lang.reflect.InvocationTargetException;
16-
import java.lang.reflect.Method;
17-
import java.util.LinkedList;
18-
import java.util.List;
199
import java.util.Map;
2010
import java.util.Objects;
21-
import org.apache.commons.lang3.StringEscapeUtils;
22-
import org.apache.commons.lang3.StringUtils;
2311

2412
@JinjavaDoc(
2513
value = "Pretty print a variable. Useful for debugging.",
@@ -60,50 +48,11 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
6048
) {
6149
varStr = Objects.toString(var);
6250
} else {
63-
varStr = objPropsToString(var);
51+
varStr = new POJONode(var).toPrettyString();
6452
}
6553

66-
return StringEscapeUtils.escapeHtml4(
54+
return EscapeFilter.escapeHtmlEntities(
6755
"{% raw %}(" + var.getClass().getSimpleName() + ": " + varStr + "){% endraw %}"
6856
);
6957
}
70-
71-
private String objPropsToString(Object var) {
72-
List<String> props = new LinkedList<>();
73-
74-
try {
75-
BeanInfo beanInfo = Introspector.getBeanInfo(var.getClass());
76-
77-
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
78-
try {
79-
if (pd.getPropertyType() != null && pd.getPropertyType().equals(Class.class)) {
80-
continue;
81-
}
82-
83-
Method readMethod = pd.getReadMethod();
84-
if (
85-
readMethod != null && !readMethod.getDeclaringClass().equals(Object.class)
86-
) {
87-
props.add(pd.getName() + "=" + readMethod.invoke(var));
88-
}
89-
} catch (Exception e) {
90-
if (
91-
e instanceof InvocationTargetException &&
92-
(
93-
(InvocationTargetException) e
94-
).getTargetException() instanceof DeferredValueException
95-
) {
96-
throw (DeferredValueException) (
97-
(InvocationTargetException) e
98-
).getTargetException();
99-
}
100-
ENGINE_LOG.error("Error reading bean value", e);
101-
}
102-
}
103-
} catch (IntrospectionException e) {
104-
ENGINE_LOG.error("Error inspecting bean", e);
105-
}
106-
107-
return '{' + StringUtils.join(props, ", ") + '}';
108-
}
10958
}

src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
56
import com.google.common.collect.ImmutableMap;
67
import com.google.common.collect.Lists;
8+
import com.hubspot.jinjava.Jinjava;
9+
import com.hubspot.jinjava.JinjavaConfig;
10+
import com.hubspot.jinjava.interpret.Context;
11+
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
712
import com.hubspot.jinjava.objects.date.PyishDate;
813
import java.time.ZoneOffset;
914
import java.time.ZonedDateTime;
1015
import org.junit.Before;
1116
import org.junit.Test;
1217

1318
public class PrettyPrintFilterTest {
19+
JinjavaInterpreter i;
1420
PrettyPrintFilter f;
1521

1622
@Before
1723
public void setup() {
24+
JinjavaConfig config = JinjavaConfig.newBuilder().build();
25+
Jinjava jinjava = new Jinjava(config);
26+
Context context = jinjava.getGlobalContext();
27+
i = new JinjavaInterpreter(jinjava, context, config);
1828
f = new PrettyPrintFilter();
1929
}
2030

2131
@Test
2232
public void ppString() {
23-
assertThat(f.filter("foobar", null))
24-
.isEqualTo("{% raw %}(String: foobar){% endraw %}");
33+
assertThat(f.filter("foobar", i)).isEqualTo("{% raw %}(String: foobar){% endraw %}");
2534
}
2635

2736
@Test
@@ -54,10 +63,27 @@ public void ppList() {
5463

5564
@Test
5665
public void ppObject() {
57-
assertThat(f.filter(new MyClass(), null))
58-
.isEqualTo("{% raw %}(MyClass: {bar=123, foo=foofoo}){% endraw %}");
66+
MyClass myClass = new MyClass();
67+
assertThat(f.filter(myClass, i))
68+
.isEqualTo(
69+
String.format(
70+
"{%% raw %%}(MyClass: {\n" +
71+
" &quot;foo&quot; : &quot;%s&quot;,\n" +
72+
" &quot;bar&quot; : %d,\n" +
73+
" &quot;nestedClass&quot; : {\n" +
74+
" &quot;fooField&quot; : &quot;%s&quot;,\n" +
75+
" &quot;barField&quot; : %d\n" +
76+
" }\n" +
77+
"}){%% endraw %%}",
78+
myClass.getFoo(),
79+
myClass.getBar(),
80+
myClass.getNestedClass().getFooField(),
81+
myClass.getNestedClass().getBarField()
82+
)
83+
);
5984
}
6085

86+
@JsonPropertyOrder({ "foo", "bar", "nestedClass" })
6187
public static class MyClass {
6288

6389
public String getFoo() {
@@ -67,5 +93,21 @@ public String getFoo() {
6793
public int getBar() {
6894
return 123;
6995
}
96+
97+
public MyNestedClass getNestedClass() {
98+
return new MyNestedClass();
99+
}
100+
}
101+
102+
@JsonPropertyOrder({ "fooField", "barField" })
103+
public static class MyNestedClass {
104+
105+
public String getFooField() {
106+
return "foofieldfoofield";
107+
}
108+
109+
public int getBarField() {
110+
return 123;
111+
}
70112
}
71113
}

0 commit comments

Comments
 (0)