File tree Expand file tree Collapse file tree 3 files changed +58
-2
lines changed
handlebars/src/main/java/com/github/jknack/handlebars
tests/src/test/java/com/github/jknack/handlebars/jackson Expand file tree Collapse file tree 3 files changed +58
-2
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,21 @@ public static class Utils {
167167 */
168168 @ SuppressWarnings ("rawtypes" )
169169 public static boolean isEmpty (final Object value ) {
170+ return isEmpty (value , false );
171+ }
172+
173+ /**
174+ * Evaluate the given object and return true is the object is considered empty. Nulls, empty
175+ * list or array and false values are considered empty. If includeZero is false, zero values are
176+ * considered empty.
177+ *
178+ * @param value The object value.
179+ * @param includeZero If true, zero values are considered empty.
180+ * @return Return true is the object is considered empty. Nulls, empty list or array and false
181+ * values are considered empty.
182+ */
183+ @ SuppressWarnings ("rawtypes" )
184+ public static boolean isEmpty (final Object value , Boolean includeZero ) {
170185 if (value == null ) {
171186 return true ;
172187 }
@@ -185,7 +200,7 @@ public static boolean isEmpty(final Object value) {
185200 if (value .getClass ().isArray ()) {
186201 return Array .getLength (value ) == 0 ;
187202 }
188- if (value instanceof Number ) {
203+ if (! includeZero && value instanceof Number ) {
189204 return ((Number ) value ).doubleValue () == 0 ;
190205 }
191206 return false ;
Original file line number Diff line number Diff line change @@ -670,7 +670,7 @@ public <T> T hash(final String name, final Object defaultValue) {
670670 * @return False if its argument is false, null or empty list/array (a "falsy" value).
671671 */
672672 public boolean isFalsy (final Object value ) {
673- return Handlebars .Utils .isEmpty (value );
673+ return Handlebars .Utils .isEmpty (value , ( Boolean ) this . hash . getOrDefault ( "includeZero" , false ) );
674674 }
675675
676676 /**
Original file line number Diff line number Diff line change 1+ /*
2+ * Handlebars.java: https://github.com/jknack/handlebars.java
3+ * Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
4+ * Copyright (c) 2012 Edgar Espina
5+ */
6+ package com .github .jknack .handlebars .jackson ;
7+
8+ import java .io .IOException ;
9+
10+ import org .junit .jupiter .api .Test ;
11+
12+ import com .fasterxml .jackson .databind .JsonNode ;
13+ import com .fasterxml .jackson .databind .ObjectMapper ;
14+ import com .github .jknack .handlebars .AbstractTest ;
15+ import com .github .jknack .handlebars .Context ;
16+ import com .github .jknack .handlebars .context .MapValueResolver ;
17+
18+ public class Issue996 extends AbstractTest {
19+
20+ @ Override
21+ protected Object configureContext (final Object model ) {
22+ return Context .newBuilder (model )
23+ .resolver (MapValueResolver .INSTANCE , JsonNodeValueResolver .INSTANCE )
24+ .build ();
25+ }
26+
27+ @ Test
28+ public void shouldSupportIncludeZeroOptionIfHelper () throws IOException {
29+ JsonNode tree = new ObjectMapper ().readTree ("[1, 2, 3]" );
30+ shouldCompileTo (
31+ "{{#if 0}}true condition{{else}}false condition{{/if}}" , tree , "false condition" );
32+ shouldCompileTo (
33+ "{{#if 0 includeZero=true}}true condition{{else}}false condition{{/if}}" ,
34+ tree ,
35+ "true condition" );
36+ shouldCompileTo (
37+ "{{#if 0 includeZero=false}}true condition{{else}}false condition{{/if}}" ,
38+ tree ,
39+ "false condition" );
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments