Skip to content

Fix join helper for arrays of primitives - #1196

Merged
jknack merged 1 commit into
jknack:masterfrom
Shaisolaris:fix-join-primitive-array
Oct 19, 2026
Merged

jknack merged 1 commit into
jknack:masterfrom
Shaisolaris:fix-join-primitive-array

Conversation

@Shaisolaris

Copy link
Copy Markdown
Contributor

The join helper fails for arrays of primitives:

handlebars.registerHelper("join", StringHelpers.join);
handlebars.compileInline("{{{join this \", \"}}}").apply(new int[] {6, 7, 8});
// java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object;

StringHelpers.join casts every array to Object[], which only works for arrays of objects. This change copies the elements with java.lang.reflect.Array, so int[], long[], double[] and so on are joined like String[] already is.

Tests: added StringHelpersTest.joinPrimitiveArray. It fails before the change (ClassCastException) and passes after. mvn test in the handlebars module passes locally (1039 tests, 3 skipped). spotless:check lists the same files with format violations as on master. StringHelpers.java was already in that list on master. I did not run the other modules.

The join helper cast any array to Object[], so {{join this ", "}} with
an int[] (or any other primitive array) failed with a
ClassCastException. Copy the elements with java.lang.reflect.Array
instead.
@jknack jknack added this to the 4.5.6 milestone Oct 16, 2026
@jknack jknack added the bug label Oct 16, 2026
@jknack
jknack merged commit 2153c46 into jknack:master Oct 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants