Skip to content

Fix join helper for arrays of primitives - #1196

Open
Shaisolaris wants to merge 1 commit into
jknack:masterfrom
Shaisolaris:fix-join-primitive-array
Open

Shaisolaris wants to merge 1 commit into
jknack:masterfrom
Shaisolaris:fix-join-primitive-array

Conversation

@Shaisolaris

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant