Skip to content

Commit 0b94900

Browse files
committed
Recursively resolve JSON Object nodes during entry iteration (jknack#969)
1 parent 881bd24 commit 0b94900

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

handlebars-jackson2/src/main/java/com/github/jknack/handlebars/JsonNodeValueResolver.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,27 @@ public Set<Map.Entry<String, Object>> entrySet() {
160160
Iterator<Map.Entry<String, JsonNode>> it = node.fields();
161161
Set set = new LinkedHashSet();
162162
while (it.hasNext()) {
163-
set.add(it.next());
163+
Map.Entry<String, JsonNode> current = it.next();
164+
165+
set.add(
166+
new Map.Entry<String, Object>() {
167+
168+
@Override
169+
public String getKey() {
170+
return current.getKey();
171+
}
172+
173+
@Override
174+
public Object getValue() {
175+
return resolve(current.getValue());
176+
}
177+
178+
@Override
179+
public Object setValue(Object value) {
180+
throw new UnsupportedOperationException();
181+
}
182+
}
183+
);
164184
}
165185
return set;
166186
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.jknack.handlebars;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.github.jknack.handlebars.context.MapValueResolver;
8+
import com.github.jknack.handlebars.helper.StringHelpers;
9+
10+
import org.junit.Test;
11+
12+
public class Issue969 extends AbstractTest {
13+
14+
@Override
15+
protected Object configureContext(final Object model) {
16+
return Context.newBuilder(model)
17+
.resolver(MapValueResolver.INSTANCE, JsonNodeValueResolver.INSTANCE)
18+
.build();
19+
}
20+
21+
@Override
22+
protected Handlebars newHandlebars() {
23+
return super.newHandlebars().with(EscapingStrategy.NOOP);
24+
}
25+
26+
@Test
27+
public void shouldRecursivelyResolveEntries() throws IOException {
28+
Hash helpers = $("join", StringHelpers.join);
29+
JsonNode tree = new ObjectMapper().readTree("{\"pets\":[{\"type\":\"cat\",\"name\":\"alice\"},{\"type\":\"bird\",\"name\":\"bob\"}]}");
30+
shouldCompileTo("{{join this.pets \", \"}}", tree, helpers, "{type=cat, name=alice}, {type=bird, name=bob}");
31+
}
32+
33+
}

0 commit comments

Comments
 (0)