Skip to content

Commit a8b36c8

Browse files
committed
Don't need annotation introspector
1 parent 34f4207 commit a8b36c8

14 files changed

Lines changed: 96 additions & 74 deletions

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import com.hubspot.jinjava.el.ObjectUnwrapper;
2727
import com.hubspot.jinjava.el.ext.AllowlistMethodValidator;
2828
import com.hubspot.jinjava.el.ext.AllowlistReturnTypeValidator;
29-
import com.hubspot.jinjava.el.ext.MethodValidatorConfig;
30-
import com.hubspot.jinjava.el.ext.ReturnTypeValidatorConfig;
3129
import com.hubspot.jinjava.features.FeatureConfig;
3230
import com.hubspot.jinjava.features.Features;
3331
import com.hubspot.jinjava.interpret.Context.Library;
@@ -165,16 +163,12 @@ default TokenScannerSymbols getTokenScannerSymbols() {
165163

166164
@Value.Default
167165
default AllowlistMethodValidator getMethodValidator() {
168-
return AllowlistMethodValidator.create(
169-
MethodValidatorConfig.builder().addDefaultAllowlistGroups().build()
170-
);
166+
return AllowlistMethodValidator.DEFAULT;
171167
}
172168

173169
@Value.Default
174170
default AllowlistReturnTypeValidator getReturnTypeValidator() {
175-
return AllowlistReturnTypeValidator.create(
176-
ReturnTypeValidatorConfig.builder().addDefaultAllowlistGroups().build()
177-
);
171+
return AllowlistReturnTypeValidator.DEFAULT;
178172
}
179173

180174
@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: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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;
@@ -20,6 +19,7 @@
2019
import com.hubspot.jinjava.mode.PreserveRawExecutionMode;
2120
import com.hubspot.jinjava.objects.date.FormattedDate;
2221
import com.hubspot.jinjava.objects.date.StrftimeFormatter;
22+
import com.hubspot.jinjava.testobjects.JinjavaInterpreterTestObjects;
2323
import com.hubspot.jinjava.tree.TextNode;
2424
import com.hubspot.jinjava.tree.output.BlockInfo;
2525
import com.hubspot.jinjava.tree.output.OutputList;
@@ -107,64 +107,59 @@ public void resolveBlockStubsWithCycle() {
107107

108108
// Ex VariableChain stuff
109109

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

143120
@Test
144121
public void multiWordCamelCase() {
145122
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
146-
assertThat(interpreter.resolveProperty(new Foo("a"), "barFoo")).isEqualTo("a");
123+
assertThat(
124+
interpreter.resolveProperty(new JinjavaInterpreterTestObjects.Foo("a"), "barFoo")
125+
)
126+
.isEqualTo("a");
147127
}
148128
}
149129

150130
@Test
151131
public void multiWordSnakeCase() {
152132
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
153-
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");
154137
}
155138
}
156139

157140
@Test
158141
public void multiWordNumberSnakeCase() {
159142
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
160-
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");
161150
}
162151
}
163152

164153
@Test
165154
public void jsonIgnore() {
166155
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
167-
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");
168163
}
169164
}
170165

@@ -388,8 +383,8 @@ public void itInterpretsWhitespaceControl() {
388383
public void itInterpretsEmptyExpressions() {
389384
jinjava =
390385
new Jinjava(
391-
JinjavaConfig
392-
.builder()
386+
BaseJinjavaTest
387+
.newConfigBuilder()
393388
.withTimeZone(ZoneId.of("America/New_York"))
394389
.withLegacyOverrides(
395390
LegacyOverrides.THREE_POINT_0.withParseWhitespaceControlStrictly(false)

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)