Skip to content

Commit 7777440

Browse files
committed
length property not working in handlebars-jackson2 fix #598
1 parent cf53e16 commit 7777440

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public Object resolve(final Object context, final String name) {
5858
Object value = null;
5959
if (context instanceof ArrayNode) {
6060
try {
61+
if (name.equals("length")) {
62+
return ((ArrayNode) context).size();
63+
}
6164
value = resolve(((ArrayNode) context).get(Integer.parseInt(name)));
6265
} catch (NumberFormatException ex) {
6366
// ignore undefined key and move on, see https://github.com/jknack/handlebars.java/pull/280
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.jknack.handlebars;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.github.jknack.handlebars.context.MapValueResolver;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
10+
public class Issue598 extends AbstractTest {
11+
12+
@Override
13+
protected Object configureContext(final Object model) {
14+
return Context.newBuilder(model)
15+
.resolver(MapValueResolver.INSTANCE, JsonNodeValueResolver.INSTANCE).build();
16+
}
17+
18+
@Test
19+
public void shouldExtendJsonArrayWithLenghtProperty() throws IOException {
20+
JsonNode tree = new ObjectMapper().readTree("[1, 2, 3]");
21+
shouldCompileTo("{{this.length}}", tree, "3");
22+
}
23+
24+
}

0 commit comments

Comments
 (0)