Skip to content

Add leniency to missing array values in mustache #126550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/126550.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 126550
summary: Add leniency to missing array values in mustache
area: Infra/Scripting
type: bug
issues:
- 55200
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ public Object get(Object key) {
if ("size".equals(key)) {
return size();
} else if (key instanceof Number number) {
return Array.get(array, number.intValue());
return number.intValue() >= 0 && number.intValue() < length ? Array.get(array, number.intValue()) : null;
}
try {
int index = Integer.parseInt(key.toString());
return Array.get(array, index);
return index >= 0 && index < length ? Array.get(array, index) : null;
} catch (NumberFormatException nfe) {
// if it's not a number it is as if the key doesn't exist
return null;
Expand Down Expand Up @@ -169,11 +169,11 @@ public Object get(Object key) {
if ("size".equals(key)) {
return col.size();
} else if (key instanceof Number number) {
return Iterables.get(col, number.intValue());
return number.intValue() >= 0 && number.intValue() < col.size() ? Iterables.get(col, number.intValue()) : null;
}
try {
int index = Integer.parseInt(key.toString());
return Iterables.get(col, index);
return index >= 0 && index < col.size() ? Iterables.get(col, index) : null;
} catch (NumberFormatException nfe) {
// if it's not a number it is as if the key doesn't exist
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ public void testMapInArrayAccess() throws Exception {
assertThat(output, both(containsString("foo")).and(containsString("bar")));
}

public void testMapInArrayBadAccess() throws Exception {
String template = "{{data.0.key}} {{data.2.key}}";
TemplateScript.Factory factory = engine.compile(null, template, TemplateScript.CONTEXT, Collections.emptyMap());
Map<String, Object> vars = new HashMap<>();
vars.put("data", new Object[] { singletonMap("key", "foo"), singletonMap("key", "bar") });
// assertThat(factory.newInstance(vars).execute(), equalTo("foo "));

vars.put("data", Arrays.asList(singletonMap("key", "foo"), singletonMap("key", "bar")));
factory.newInstance(vars);
assertThat(factory.newInstance(vars).execute(), equalTo("foo "));

// HashSet iteration order isn't fixed
Set<Object> setData = new HashSet<>();
setData.add(singletonMap("key", "foo"));
setData.add(singletonMap("key", "bar"));
vars.put("data", setData);
String output = factory.newInstance(vars).execute();
assertThat(output, both(containsString("foo")).and(not(containsString("bar"))));
}

public void testSizeAccessForCollectionsAndArrays() throws Exception {
String[] randomArrayValues = generateRandomStringArray(10, 20, false);
List<String> randomList = Arrays.asList(generateRandomStringArray(10, 20, false));
Expand Down
Loading