Skip to content

Commit

Permalink
Recursively resolve JSON Object nodes during entry iteration (jknack#969
Browse files Browse the repository at this point in the history
)
  • Loading branch information
carusology committed Apr 21, 2022
1 parent 881bd24 commit 0b94900
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,27 @@ public Set<Map.Entry<String, Object>> entrySet() {
Iterator<Map.Entry<String, JsonNode>> it = node.fields();
Set set = new LinkedHashSet();
while (it.hasNext()) {
set.add(it.next());
Map.Entry<String, JsonNode> current = it.next();

set.add(
new Map.Entry<String, Object>() {

@Override
public String getKey() {
return current.getKey();
}

@Override
public Object getValue() {
return resolve(current.getValue());
}

@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}
}
);
}
return set;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.jknack.handlebars;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.context.MapValueResolver;
import com.github.jknack.handlebars.helper.StringHelpers;

import org.junit.Test;

public class Issue969 extends AbstractTest {

@Override
protected Object configureContext(final Object model) {
return Context.newBuilder(model)
.resolver(MapValueResolver.INSTANCE, JsonNodeValueResolver.INSTANCE)
.build();
}

@Override
protected Handlebars newHandlebars() {
return super.newHandlebars().with(EscapingStrategy.NOOP);
}

@Test
public void shouldRecursivelyResolveEntries() throws IOException {
Hash helpers = $("join", StringHelpers.join);
JsonNode tree = new ObjectMapper().readTree("{\"pets\":[{\"type\":\"cat\",\"name\":\"alice\"},{\"type\":\"bird\",\"name\":\"bob\"}]}");
shouldCompileTo("{{join this.pets \", \"}}", tree, helpers, "{type=cat, name=alice}, {type=bird, name=bob}");
}

}

0 comments on commit 0b94900

Please sign in to comment.