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

Fix handling of quoted boolean values in YAML::Any #13546

Merged
merged 1 commit into from Jun 14, 2023

Conversation

willhbr
Copy link
Contributor

@willhbr willhbr commented Jun 3, 2023

"yes" and "no" should be treated as strings (since they're quoted) whereas yes and no should be booleans (since they're plain). This is already handled in most places, but YAML::Any passes the node value, rather than the node itself when handling a scalar and so the quoting type is lost, and everything is parsed as though it is unquoted.

This is a breaking change.

spec/std/yaml/any_spec.cr Outdated Show resolved Hide resolved
Comment on lines 102 to 105
YAML.parse("yes").as_bool?.should be_true
YAML.parse("no").as_bool?.should be_false
YAML.parse("'yes'").as_bool?.should be_nil
YAML.parse("'no'").as_bool?.should be_nil
Copy link
Contributor

Choose a reason for hiding this comment

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

These specs pass even without this PR. Instead this PR seems to only affect users of YAML::Serializable::Unmapped:

require "yaml"

class Foo
  include YAML::Serializable
  include YAML::Serializable::Unmapped
end

# before
Foo.from_yaml("x: yes")      # => #<Foo:0x13732ca0ce0 @yaml_unmapped={"x" => true}>
Foo.from_yaml("x: 'yes'")    # => #<Foo:0x13732ca0bc0 @yaml_unmapped={"x" => true}>
YAML.parse("'yes'").as_bool? # => nil

# after
Foo.from_yaml("x: yes")   # => #<Foo:0x24f0b1d0cc0 @yaml_unmapped={"x" => true}>
Foo.from_yaml("x: 'yes'") # => #<Foo:0x24f0b1d0ba0 @yaml_unmapped={"x" => "yes"}>
YAML.parse("'yes'").as_bool? # => nil

Copy link
Contributor Author

@willhbr willhbr Jun 4, 2023

Choose a reason for hiding this comment

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

Ah YAML.parse and YAML::Any.from_yaml seem to go through different codepaths?

I can't see any specs specifically for YAML::Any.from_yaml—should I add some in this file?

And yeah this will change the parsing of YAML::Unmapped or if you have a field of type YAML::Any (which is what I was doing).

So:

require "yaml"

# Before
YAML::Any.from_yaml "'yes'" # => true
YAML::Any.from_yaml "yes" # => true
YAML.parse "'yes'" # => "yes"
YAML.parse "yes" # => true

# After
YAML::Any.from_yaml "'yes'" # => "yes"
YAML::Any.from_yaml "yes" # => true
YAML.parse "'yes'" # => "yes"
YAML.parse "yes" # => true

Copy link
Contributor

Choose a reason for hiding this comment

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

Please do add the necessary specs

Copy link
Member

Choose a reason for hiding this comment

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

Ah YAML.parse and YAML::Any.from_yaml seem to go through different codepaths?

Yeah, that's all a bit weird and hard to follow through, what's actually going on. YAML parsing could probably use some refactoring just to get the code clarified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests added :)

`"yes"` and `"no"` should be treated as strings (since they're quoted)
whereas `yes` and `no` should be booleans (since they're plain). This is
already handled in most places, but `YAML::Any` passes the node value,
rather than the node itself when handling a scalar and so the quoting
type is lost, and everything is parsed as though it is unquoted.
@straight-shoota straight-shoota added this to the 1.9.0 milestone Jun 6, 2023
@straight-shoota straight-shoota merged commit 601aa1f into crystal-lang:master Jun 14, 2023
44 of 46 checks passed
@straight-shoota
Copy link
Member

straight-shoota commented Jul 5, 2023

Removed the breaking label. This is a bug fix and only breaks code that relied on that pretty obvious bug. It's not necessary to point that out explicitly.

ref: https://forum.crystal-lang.org/t/breaking-changes/5805/2?u=straight-shoota

straight-shoota added a commit that referenced this pull request Jul 10, 2023
Blacksmoke16 pushed a commit to Blacksmoke16/crystal that referenced this pull request Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants