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

CGI powered standard filters to handle non string inputs #898

Merged

Conversation

tjoyal
Copy link
Member

@tjoyal tjoyal commented May 25, 2017

Problem

Some standard filters to not properly handle unexpected argument.

Liquid::Template.parse("{{ 1 | url_encode }}").render
NoMethodError: undefined method `encoding' for 1:Fixnum
    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/cgi/util.rb:7:in `escape'
    /Users/tjoyal/projects/liquid/lib/liquid/standardfilters.rb:45:in `url_encode'

Solution

I'm usually aim to raise exceptions when the received parameters is not of the proper type(eg.: Liquid::StandardError), but most of the standard filters do explicit to_s before processing the input (for the same reason we also do an explicit check for nil?).

I ended up adding explicit to_s.

Thoughts?
Is this going in the right direction or should all non string parameters raise an exception?
Why is nil treated differently here compared to other filters? Seems to be a behaviour split introduced by 704937b#diff-844406140a08cdeddd843cfbdf0222d6L45

@tjoyal tjoyal requested a review from dylanahsmith May 25, 2017 15:54
Copy link
Contributor

@dylanahsmith dylanahsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

liquid tries to automatically coerce data types so designers don't need to think about them.

I don't really like the using .to_s to do that coercion, since it means even non-scalars would get coerced to a string. I would prefer we used some Liquid::Utils.to_string method that raised a Liquid::ArgumentError exception if a non-scalar was provided, but that would be a backwards incompatible change. Although you could do that for new coercions like this if you want to be more strict.

Since we do .to_s elsewhere, this is also fine for consistency reasons.

@dylanahsmith
Copy link
Contributor

Why is nil treated differently here compared to other filters? Seems to be a behaviour split introduced by 704937b#diff-844406140a08cdeddd843cfbdf0222d6L45

That commit didn't cause a behaviour split, previously it would rescue input which would return nil if the input was nil.

@tjoyal tjoyal requested a review from fw42 May 25, 2017 19:51
@tjoyal tjoyal merged commit c582b86 into master May 26, 2017
@tjoyal tjoyal deleted the cgi-powered-standard-filters-to-handle-non-string-inputs branch May 26, 2017 18:05
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.

None yet

3 participants