Skip to content

Commit 9c17ae2

Browse files
committed
Don't need annotation introspector
1 parent e4c7edf commit 9c17ae2

14 files changed

Lines changed: 106 additions & 72 deletions

src/main/java/com/hubspot/jinjava/JinjavaConfig.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import com.hubspot.jinjava.el.ObjectUnwrapper;
2929
import com.hubspot.jinjava.el.ext.AllowlistMethodValidator;
3030
import com.hubspot.jinjava.el.ext.AllowlistReturnTypeValidator;
31-
import com.hubspot.jinjava.el.ext.MethodValidatorConfig;
32-
import com.hubspot.jinjava.el.ext.ReturnTypeValidatorConfig;
3331
import com.hubspot.jinjava.features.FeatureConfig;
3432
import com.hubspot.jinjava.features.Features;
3533
import com.hubspot.jinjava.interpret.Context.Library;
@@ -166,16 +164,12 @@ default TokenScannerSymbols getTokenScannerSymbols() {
166164

167165
@Value.Default
168166
default AllowlistMethodValidator getMethodValidator() {
169-
return AllowlistMethodValidator.create(
170-
MethodValidatorConfig.builder().addDefaultAllowlistGroups().build()
171-
);
167+
return AllowlistMethodValidator.DEFAULT;
172168
}
173169

174170
@Value.Default
175171
default AllowlistReturnTypeValidator getReturnTypeValidator() {
176-
return AllowlistReturnTypeValidator.create(
177-
ReturnTypeValidatorConfig.builder().addDefaultAllowlistGroups().build()
178-
);
172+
return AllowlistReturnTypeValidator.DEFAULT;
179173
}
180174

181175
@Value.Default

src/main/java/com/hubspot/jinjava/el/ext/AllowlistGroup.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.hubspot.jinjava.util.ForLoop;
2525
import java.lang.reflect.Method;
2626
import java.math.BigDecimal;
27-
import java.util.AbstractCollection;
2827
import java.util.ArrayList;
2928
import java.util.LinkedHashMap;
3029
import java.util.Map;
@@ -101,7 +100,6 @@ String[] allowedDeclaredMethodsFromClasses() {
101100
ForwardingMap.class.getCanonicalName(),
102101
ForwardingSet.class.getCanonicalName(),
103102
ForwardingCollection.class.getCanonicalName(),
104-
AbstractCollection.class.getCanonicalName(),
105103
LinkedHashMap.class.getCanonicalName(),
106104
"%s.Entry".formatted(LinkedHashMap.class.getCanonicalName()),
107105
"%s.LinkedValues".formatted(LinkedHashMap.class.getCanonicalName()),

src/main/java/com/hubspot/jinjava/el/ext/AllowlistMethodValidator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
public final class AllowlistMethodValidator {
99

10+
public static final AllowlistMethodValidator DEFAULT = AllowlistMethodValidator.create(
11+
MethodValidatorConfig.builder().addDefaultAllowlistGroups().build()
12+
);
1013
private final ConcurrentHashMap<Method, Boolean> allowedMethodsCache;
1114
private final ImmutableSet<Method> allowedMethods;
1215
private final ImmutableSet<String> allowedDeclaredMethodsFromCanonicalClassPrefixes;

src/main/java/com/hubspot/jinjava/el/ext/AllowlistReturnTypeValidator.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
public final class AllowlistReturnTypeValidator {
88

9+
public static final AllowlistReturnTypeValidator DEFAULT =
10+
AllowlistReturnTypeValidator.create(
11+
ReturnTypeValidatorConfig.builder().addDefaultAllowlistGroups().build()
12+
);
913
private final ConcurrentHashMap<String, Boolean> allowedReturnTypesCache;
1014

1115
private final ImmutableSet<String> allowedCanonicalClassPrefixes;
@@ -68,11 +72,20 @@ public boolean allowReturnTypeClass(Class<?> clazz) {
6872
return true;
6973
}
7074
String canonicalClassName = clazz.getCanonicalName();
71-
return allowedReturnTypesCache.computeIfAbsent(
75+
boolean isAllowedReturnType = allowedReturnTypesCache.computeIfAbsent(
7276
canonicalClassName,
7377
c ->
7478
allowedCanonicalClassNames.contains(canonicalClassName) ||
7579
allowedCanonicalClassPrefixes.stream().anyMatch(canonicalClassName::startsWith)
7680
);
81+
if (!isAllowedReturnType) {
82+
return false;
83+
}
84+
for (ReturnTypeValidator v : additionalValidators) {
85+
if (!v.allowReturnTypeClass(clazz)) {
86+
return false;
87+
}
88+
}
89+
return true;
7790
}
7891
}

src/main/java/com/hubspot/jinjava/el/ext/ReturnTypeValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
public interface ReturnTypeValidator {
44
Object validateReturnType(Object o);
5+
boolean allowReturnTypeClass(Class<?> c);
56
}

src/main/java/com/hubspot/jinjava/objects/serialization/PyishObjectMapper.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.fasterxml.jackson.databind.ObjectWriter;
99
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
1010
import com.fasterxml.jackson.databind.SerializerProvider;
11-
import com.fasterxml.jackson.databind.introspect.Annotated;
12-
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
1311
import com.fasterxml.jackson.databind.module.SimpleModule;
1412
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
1513
import com.google.common.annotations.Beta;
@@ -55,25 +53,6 @@ private static ObjectMapper getPyishObjectMapper() {
5553
.addSerializer(PyishSerializable.class, PyishSerializer.INSTANCE)
5654
);
5755
mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());
58-
mapper.setAnnotationIntrospector(
59-
new JacksonAnnotationIntrospector() {
60-
@Override
61-
protected boolean _isIgnorable(Annotated a) {
62-
return (
63-
super._isIgnorable(a) ||
64-
!JinjavaInterpreter
65-
.getCurrentMaybe()
66-
.map(interpreter ->
67-
interpreter
68-
.getConfig()
69-
.getReturnTypeValidator()
70-
.allowReturnTypeClass(a.getRawType())
71-
)
72-
.orElse(false)
73-
);
74-
}
75-
}
76-
);
7756
return mapper;
7857
}
7958

src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
55

6-
import com.fasterxml.jackson.annotation.JsonIgnore;
76
import com.google.common.collect.ImmutableMap;
87
import com.google.common.collect.Lists;
98
import com.hubspot.jinjava.BaseJinjavaTest;
109
import com.hubspot.jinjava.Jinjava;
1110
import com.hubspot.jinjava.JinjavaConfig;
11+
import com.hubspot.jinjava.LegacyOverrides;
1212
import com.hubspot.jinjava.features.FeatureConfig;
1313
import com.hubspot.jinjava.features.FeatureStrategies;
1414
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
@@ -19,6 +19,7 @@
1919
import com.hubspot.jinjava.mode.PreserveRawExecutionMode;
2020
import com.hubspot.jinjava.objects.date.FormattedDate;
2121
import com.hubspot.jinjava.objects.date.StrftimeFormatter;
22+
import com.hubspot.jinjava.testobjects.JinjavaInterpreterTestObjects;
2223
import com.hubspot.jinjava.tree.TextNode;
2324
import com.hubspot.jinjava.tree.output.BlockInfo;
2425
import com.hubspot.jinjava.tree.output.OutputList;
@@ -106,64 +107,59 @@ public void resolveBlockStubsWithCycle() {
106107

107108
// Ex VariableChain stuff
108109

109-
static class Foo {
110-
111-
private String bar;
112-
113-
public Foo(String bar) {
114-
this.bar = bar;
115-
}
116-
117-
public String getBar() {
118-
return bar;
119-
}
120-
121-
public String getBarFoo() {
122-
return bar;
123-
}
124-
125-
public String getBarFoo1() {
126-
return bar;
127-
}
128-
129-
@JsonIgnore
130-
public String getBarHidden() {
131-
return bar;
132-
}
133-
}
134-
135110
@Test
136111
public void singleWordProperty() {
137112
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
138-
assertThat(interpreter.resolveProperty(new Foo("a"), "bar")).isEqualTo("a");
113+
assertThat(
114+
interpreter.resolveProperty(new JinjavaInterpreterTestObjects.Foo("a"), "bar")
115+
)
116+
.isEqualTo("a");
139117
}
140118
}
141119

142120
@Test
143121
public void multiWordCamelCase() {
144122
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
145-
assertThat(interpreter.resolveProperty(new Foo("a"), "barFoo")).isEqualTo("a");
123+
assertThat(
124+
interpreter.resolveProperty(new JinjavaInterpreterTestObjects.Foo("a"), "barFoo")
125+
)
126+
.isEqualTo("a");
146127
}
147128
}
148129

149130
@Test
150131
public void multiWordSnakeCase() {
151132
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
152-
assertThat(interpreter.resolveProperty(new Foo("a"), "bar_foo")).isEqualTo("a");
133+
assertThat(
134+
interpreter.resolveProperty(new JinjavaInterpreterTestObjects.Foo("a"), "bar_foo")
135+
)
136+
.isEqualTo("a");
153137
}
154138
}
155139

156140
@Test
157141
public void multiWordNumberSnakeCase() {
158142
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
159-
assertThat(interpreter.resolveProperty(new Foo("a"), "bar_foo_1")).isEqualTo("a");
143+
assertThat(
144+
interpreter.resolveProperty(
145+
new JinjavaInterpreterTestObjects.Foo("a"),
146+
"bar_foo_1"
147+
)
148+
)
149+
.isEqualTo("a");
160150
}
161151
}
162152

163153
@Test
164154
public void jsonIgnore() {
165155
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
166-
assertThat(interpreter.resolveProperty(new Foo("a"), "barHidden")).isEqualTo("a");
156+
assertThat(
157+
interpreter.resolveProperty(
158+
new JinjavaInterpreterTestObjects.Foo("a"),
159+
"barHidden"
160+
)
161+
)
162+
.isEqualTo("a");
167163
}
168164
}
169165

@@ -385,6 +381,17 @@ public void itInterpretsWhitespaceControl() {
385381

386382
@Test
387383
public void itInterpretsEmptyExpressions() {
384+
jinjava =
385+
new Jinjava(
386+
BaseJinjavaTest
387+
.newConfigBuilder()
388+
.withTimeZone(ZoneId.of("America/New_York"))
389+
.withLegacyOverrides(
390+
LegacyOverrides.THREE_POINT_0.withParseWhitespaceControlStrictly(false)
391+
)
392+
.build()
393+
);
394+
interpreter = jinjava.newInterpreter();
388395
assertThat(interpreter.render("{{}}")).isEqualTo("");
389396
}
390397

src/test/java/com/hubspot/jinjava/lib/expression/EagerExpressionStrategyTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public void eagerSetup() throws Exception {
7272

7373
@Test
7474
public void itPreservesRawTags() {
75+
interpreter =
76+
new JinjavaInterpreter(
77+
jinjava,
78+
new Context(),
79+
BaseJinjavaTest
80+
.newConfigBuilder()
81+
.withExecutionMode(EagerExecutionMode.instance())
82+
.build()
83+
);
7584
assertExpectedOutput(
7685
interpreter,
7786
"{{ '{{ foo }}' }} {{ '{% something %}' }} {{ 'not needed' }}",

src/test/java/com/hubspot/jinjava/lib/filter/time/FormatDateFilterTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.google.common.collect.ImmutableMap;
77
import com.hubspot.jinjava.BaseJinjavaTest;
88
import com.hubspot.jinjava.Jinjava;
9-
import com.hubspot.jinjava.JinjavaConfig;
109
import com.hubspot.jinjava.features.DateTimeFeatureActivationStrategy;
1110
import com.hubspot.jinjava.features.FeatureConfig;
1211
import com.hubspot.jinjava.interpret.RenderResult;
@@ -34,7 +33,7 @@ public class FormatDateFilterTest {
3433

3534
@Before
3635
public void setUp() throws Exception {
37-
jinjava = new Jinjava();
36+
jinjava = new Jinjava(BaseJinjavaTest.newConfigBuilder().build());
3837
jinjava.getGlobalContext().registerClasses(FormatDateFilter.class);
3938
}
4039

src/test/java/com/hubspot/jinjava/lib/tag/MacroTagTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.hubspot.jinjava.BaseInterpretingTest;
1010
import com.hubspot.jinjava.BaseJinjavaTest;
1111
import com.hubspot.jinjava.Jinjava;
12-
import com.hubspot.jinjava.JinjavaConfig;
1312
import com.hubspot.jinjava.interpret.DeferredValue;
1413
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1514
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
@@ -304,7 +303,7 @@ public void itPreventsRecursionForMacroWithVar() {
304303
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
305304
String jinja =
306305
"{%- macro func(var) %}" +
307-
"{%- for f in var %}" +
306+
"{%- for k,f in var.items() %}" +
308307
"{{ f.val }}" +
309308
"{%- endfor %}" +
310309
"{%- endmacro %}" +

0 commit comments

Comments
 (0)