Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update PrettyPrintFilter to produce more useful output by serializing…
… objects into JSON
  • Loading branch information
Maple Buice committed Nov 30, 2023
commit 27d86d39680c735f7a630a0992147caa8dfad9e8
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down Expand Up @@ -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<String> 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, ", ") + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@

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;
import org.junit.Before;
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
Expand Down Expand Up @@ -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" +
" &quot;foo&quot; : &quot;%s&quot;,\n" +
" &quot;bar&quot; : %d,\n" +
" &quot;nestedClass&quot; : {\n" +
" &quot;fooField&quot; : &quot;%s&quot;,\n" +
" &quot;barField&quot; : %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() {
Expand All @@ -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;
}
}
}