Skip to content

Commit cf53e16

Browse files
committed
IllegalArgumentException in MethodHelper for function that does not return a CharSequence #606
1 parent 30759a5 commit cf53e16

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/helper/DefaultHelperRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private void registerDynamicHelper(final Object source, final Class<?> clazz) {
206206
for (Method method : methods) {
207207
boolean isPublic = Modifier.isPublic(method.getModifiers());
208208
String helperName = method.getName();
209-
if (isPublic && CharSequence.class.isAssignableFrom(method.getReturnType())) {
209+
if (isPublic) {
210210
boolean isStatic = Modifier.isStatic(method.getModifiers());
211211
if (source != null || isStatic) {
212212
isTrue(overloaded.add(helperName), "name conflict found: " + helperName);

handlebars/src/main/java/com/github/jknack/handlebars/helper/MethodHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Object apply(final Object context, final Options options) throws IOExcept
7676
}
7777
}
7878
}
79-
return (CharSequence) method.invoke(source, args);
79+
return method.invoke(source, args);
8080
} catch (ArrayIndexOutOfBoundsException ex) {
8181
throw new IllegalArgumentException(
8282
"could not execute helper: " + toString(method) + ", with the given arguments: "

handlebars/src/test/java/com/github/jknack/handlebars/ReflectiveHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testBlogTitle() throws IOException {
7171
}
7272

7373
@Test
74-
public void params() throws IOException {
74+
public void testParams() throws IOException {
7575
shouldCompileTo("{{params this l d f c b s}}",
7676
$("l", 1L, "d", 2.0D, "f", 3.0f, "c", '4', "b", (byte) 5, "s", (short) 6),
7777
"1, 2.0, 3.0, 4, 5, 6");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.Handlebars;
4+
import com.github.jknack.handlebars.v4Test;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
public class Issue606 extends v4Test {
10+
11+
public static class Helpers {
12+
13+
public int subtract(int a, int b) {
14+
return a - b;
15+
}
16+
}
17+
18+
@Override protected void configure(Handlebars handlebars) {
19+
super.configure(handlebars);
20+
handlebars.registerHelpers(new Helpers());
21+
}
22+
23+
@Test
24+
public void shouldSupportNoneCharSequenceReturnsTypeFromHelperClass() throws Exception {
25+
shouldCompileTo("{{#if (subtract value 1)}}OK{{/if}}", $("hash", $("value", 2)), "OK");
26+
shouldCompileTo("{{#if (subtract value 1)}}OK{{/if}}", $("hash", $("value", 1)), "");
27+
}
28+
}

0 commit comments

Comments
 (0)