Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow replace filter on non-strings #603

Merged
merged 1 commit into from
Feb 25, 2021
Merged

Allow replace filter on non-strings #603

merged 1 commit into from
Feb 25, 2021

Conversation

jasmith-hs
Copy link
Contributor

Unlike other filters, the ReplaceFilter does not check if var instanceof String before casting it to a String. This results in a FATAL error if something besides a string is passed into the filter. I have seen a template that actually makes use of this logic unintentionally with code like {{ my_list.append(obj)|replace(true, '') }}. So, in order to fix this bug while maintaining the same output for legacy templates, this adds the functionality to the replace filter of calling toString() to allow it to be called on non-string objects.

Since the String class in Java is final, and String.toString() returns this, this will not change the functionality for any replace filtering on strings as the filter is intended for. However, using this filter on something like a boolean or a number will call toString on the object to convert it to a String, rather than throwing a ClassCastException and resulting in a FATAL error.

@jasmith-hs jasmith-hs merged commit 9a3e3a1 into master Feb 25, 2021
@jasmith-hs jasmith-hs deleted the replace-string branch February 25, 2021 22:13
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.

2 participants