Skip to content

Commit

Permalink
JEXL-416: fixed edge case of null pragma value;
Browse files Browse the repository at this point in the history
- added unit test;
  • Loading branch information
Henri Biestro committed Nov 23, 2023
1 parent 44963a7 commit 0a2e943
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ New Features in 3.3.1:

Bugs Fixed in 3.3.1:
===================
* JEXL-416: Null-valued pragma throws NPE in 3.3
* JEXL-415: Incorrect template eval result
* JEXL-414: SoftCache may suffer from race conditions
* JEXL-412: Ambiguous syntax between namespace function call and map object definition.
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
Allow 'trailing commas' or ellipsis while defining array, map and set literals
</action>
<!-- FIX -->
<action dev="henrib" type="fix" issue="JEXL-416" due-to="William Price" >
Null-valued pragma throws NPE in 3.3
</action>
<action dev="henrib" type="fix" issue="JEXL-415" due-to="Xu Pengcheng" >
Incorrect template eval result.
</action>
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,20 @@ protected void declarePragma(final String key, final Object value) {
}
}
// merge new value into a set created on the fly if key is already mapped
pragmas.merge(key, value, (previous, newValue)->{
if (previous instanceof Set<?>) {
((Set<Object>) previous).add(newValue);
return previous;
}
final Set<Object> values = new LinkedHashSet<>();
values.add(previous);
values.add(newValue);
return values;
});
if (value == null) {
pragmas.putIfAbsent(key, null);
} else {
pragmas.merge(key, value, (previous, newValue) -> {
if (previous instanceof Set<?>) {
((Set<Object>) previous).add(newValue);
return previous;
}
final Set<Object> values = new LinkedHashSet<>();
values.add(previous);
values.add(newValue);
return values;
});
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/apache/commons/jexl3/PragmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,13 @@ public void testStatictNamespacePragmaCtl() {
final Object result = script.execute(jc);
Assert.assertEquals(42, result);
}

@Test
public void testIssue416() {
final JexlEngine jexl = new JexlBuilder().create();
JexlScript script = jexl.createScript("#pragma myNull null\n");
Map<String, Object> pragmas = script.getPragmas();
Assert.assertTrue("pragma key present?", pragmas.containsKey("myNull"));
Assert.assertNull("expected null value", pragmas.get("myNull"));
}
}

0 comments on commit 0a2e943

Please sign in to comment.