Skip to content

Commit b4bc172

Browse files
committed
Make copy of global context.
1 parent fb946da commit b4bc172

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public Context getGlobalContext() {
123123
return globalContext;
124124
}
125125

126+
public Context getGlobalContextCopy() {
127+
return copyGlobalContext();
128+
}
129+
126130
public ResourceLocator getResourceLocator() {
127131
return resourceLocator;
128132
}
@@ -192,7 +196,11 @@ public RenderResult renderForResult(
192196
Map<String, ?> bindings,
193197
JinjavaConfig renderConfig
194198
) {
195-
Context context = new Context(globalContext, bindings, renderConfig.getDisabled());
199+
Context context = new Context(
200+
copyGlobalContext(),
201+
bindings,
202+
renderConfig.getDisabled()
203+
);
196204

197205
JinjavaInterpreter parentInterpreter = JinjavaInterpreter.getCurrent();
198206
if (parentInterpreter != null) {
@@ -256,6 +264,16 @@ public RenderResult renderForResult(
256264
public JinjavaInterpreter newInterpreter() {
257265
return globalConfig
258266
.getInterpreterFactory()
259-
.newInstance(this, this.getGlobalContext(), this.getGlobalConfig());
267+
.newInstance(this, copyGlobalContext(), this.getGlobalConfig());
268+
}
269+
270+
private Context copyGlobalContext() {
271+
Context context = new Context(null, globalContext);
272+
// copy registered.
273+
globalContext.getAllExpTests().forEach(context::registerExpTest);
274+
globalContext.getAllFilters().forEach(context::registerFilter);
275+
globalContext.getAllFunctions().forEach(context::registerFunction);
276+
globalContext.getAllTags().forEach(context::registerTag);
277+
return context;
260278
}
261279
}

src/main/java/com/hubspot/jinjava/doc/JinjavaDocFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public JinjavaDoc get() {
4242
}
4343

4444
private void addExpTests(JinjavaDoc doc) {
45-
for (ExpTest t : jinjava.getGlobalContext().getAllExpTests()) {
45+
for (ExpTest t : jinjava.getGlobalContextCopy().getAllExpTests()) {
4646
com.hubspot.jinjava.doc.annotations.JinjavaDoc docAnnotation = t
4747
.getClass()
4848
.getAnnotation(com.hubspot.jinjava.doc.annotations.JinjavaDoc.class);
@@ -83,7 +83,7 @@ private void addExpTests(JinjavaDoc doc) {
8383
}
8484

8585
private void addFilterDocs(JinjavaDoc doc) {
86-
for (Filter f : jinjava.getGlobalContext().getAllFilters()) {
86+
for (Filter f : jinjava.getGlobalContextCopy().getAllFilters()) {
8787
com.hubspot.jinjava.doc.annotations.JinjavaDoc docAnnotation = f
8888
.getClass()
8989
.getAnnotation(com.hubspot.jinjava.doc.annotations.JinjavaDoc.class);
@@ -124,7 +124,7 @@ private void addFilterDocs(JinjavaDoc doc) {
124124
}
125125

126126
private void addFnDocs(JinjavaDoc doc) {
127-
for (ELFunctionDefinition fn : jinjava.getGlobalContext().getAllFunctions()) {
127+
for (ELFunctionDefinition fn : jinjava.getGlobalContextCopy().getAllFunctions()) {
128128
if (StringUtils.isBlank(fn.getNamespace())) {
129129
Method realMethod = fn.getMethod();
130130
if (
@@ -182,7 +182,7 @@ private void addFnDocs(JinjavaDoc doc) {
182182
}
183183

184184
private void addTagDocs(JinjavaDoc doc) {
185-
for (Tag t : jinjava.getGlobalContext().getAllTags()) {
185+
for (Tag t : jinjava.getGlobalContextCopy().getAllTags()) {
186186
if (t instanceof EndTag) {
187187
continue;
188188
}

0 commit comments

Comments
 (0)