Skip to content

Commit

Permalink
Fix PrefixToPreserveState creation by operating against
Browse files Browse the repository at this point in the history
DeferredLazyReference and not LazyReference.
This was the intended functionality to make sure that the order of
reconstructed values was such that {% set foo = [] %}{% set bar = foo
%}, rather than {% set bar = foo %}{% set foo = [] %} would happen
  • Loading branch information
jasmith-hs committed Aug 23, 2024
1 parent 7486760 commit b2e9fb2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import static com.hubspot.jinjava.util.EagerReconstructionUtils.buildSetTag;

import com.google.common.annotations.Beta;
import com.hubspot.jinjava.interpret.DeferredLazyReference;
import com.hubspot.jinjava.interpret.DeferredLazyReferenceSource;
import com.hubspot.jinjava.interpret.DeferredValueShadow;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.LazyReference;
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult;
import com.hubspot.jinjava.util.EagerReconstructionUtils;
Expand Down Expand Up @@ -65,7 +65,7 @@ public PrefixToPreserveState getPrefixToPreserveState() {
.collect(Collectors.toList());
filteredEntries
.stream()
.filter(entry -> !(entry.getValue() instanceof LazyReference))
.filter(entry -> !(entry.getValue() instanceof DeferredLazyReference))
.forEach(entry ->
EagerReconstructionUtils.hydrateBlockOrInlineSetTagRecursively(
prefixToPreserveState,
Expand All @@ -76,11 +76,13 @@ public PrefixToPreserveState getPrefixToPreserveState() {
);
filteredEntries
.stream()
.filter(entry -> (entry.getValue() instanceof LazyReference))
.filter(entry -> (entry.getValue() instanceof DeferredLazyReference))
.map(entry ->
new AbstractMap.SimpleImmutableEntry<>(
entry.getKey(),
PyishObjectMapper.getAsPyishString(entry.getValue())
PyishObjectMapper.getAsPyishString(
((DeferredLazyReference) entry.getValue()).getOriginalValue()
)
)
)
.sorted((a, b) ->
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,14 @@ public void itDefersCallTagWithDeferredArgumentSecondPass() {
@Test
public void itHandlesDuplicateVariableReferenceModification() {
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"handles-duplicate-variable-reference-modification"
"handles-duplicate-variable-reference-modification/test"
);
}

@Test
public void itHandlesDuplicateVariableReferenceModification2() {
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"handles-duplicate-variable-reference-modification-2/test"
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% set foo = ['a', 1] %}{% set bar = foo %}{% if deferred %}
{% do bar.append(2) %}
{% endif %}
{% do bar.append(3) %}
{{ foo ~ 'and' ~ bar }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% set foo = ['a'] -%}
{%- set bar = foo -%}
{% do bar.append(1) %}
{% if deferred %}
{% do bar.append(2) %}
{% endif %}
{% do bar.append(3) %}
{{ foo ~ 'and' ~ bar }}

0 comments on commit b2e9fb2

Please sign in to comment.