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 non-string template source #1775

Merged
merged 2 commits into from Jan 12, 2024
Merged

Conversation

ggmichaelgo
Copy link
Contributor

What are you trying to solve?

#1774 introduced a bug that doesn't allow non-string values such as nil or Integer as a template source.

Expected behaviour:

require 'liquid'

Liquid::Template.parse(nil).render # => ""
Liquid::Template.parse(1).render # => "1"

How are you solving this?

This PR updates the Template#parse to only check the template's validity if the template is a String

@@ -108,7 +108,7 @@ def initialize
def parse(source, options = {})
parse_context = configure_options(options)

unless source.valid_encoding?
if source.is_a?(String) && !source.valid_encoding?
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the to_s value is a string with an invalid encoding?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch... That is possible...

class ByteString
  def to_s
    "\xC0"
  end
end

Liquid::Template.parse(ByteString.new) # this should raise an error

I have updated the PR to convert the source to a String object from the Template#parse function instead of Tokenize#initialize.

@ggmichaelgo ggmichaelgo changed the title allow nil template source allow non-string template source Jan 10, 2024
@ggmichaelgo ggmichaelgo requested review from samdoiron and a team January 11, 2024 15:43
@gmalette
Copy link
Contributor

Wait what? How/when does this happen?

Copy link
Contributor

@gmalette gmalette left a comment

Choose a reason for hiding this comment

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

Code lgtm but man... why 😞

@ggmichaelgo ggmichaelgo merged commit cf76c0b into main Jan 12, 2024
9 checks passed
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

4 participants