Allow replace filter on non-strings #603
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 callingtoString()
to allow it to be called on non-string objects.Since the
String
class in Java isfinal
, andString.toString()
returnsthis
, 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.