Skip to content

Don't decode explicit strings as bools, regardless of content #440

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

czechboy0
Copy link
Contributor

Summary

A scalar with the text true, for example, can be decoded as a Bool today - that's expected.

However, when using a YAMLDecoder, the scalars !!str true, "true", and 'true' also successfully decode as Bools - that's unexpected, especially with Decodable implementations often trying multiple different types to decode as, and use the first one that works.

The suggested change checks the scalar's tag property, and if it's explicitly marked as a string, doesn't allow creating a Bool value from it.

This fixes AnyCodable-style decoding of YAML values, which in turn affects correctness of round-tripping YAML through Yams and back into YAML.

Test Plan

Added unit tests, verified they failed before this change and pass with the change.

@czechboy0
Copy link
Contributor Author

The Windows CI fail doesn't seem to be caused by this PR, I don't think?

@lynchsft
Copy link
Collaborator

Thank you for the PR!

This change looks great! 😊 I'd like to request one small addition to the test you've added. See review comments. 🙂

I'm unsure what's causing the windows failure. It looks like VSCode isn't setup properly. Will spend some time Investigating.

Copy link
Collaborator

@lynchsft lynchsft left a comment

Choose a reason for hiding this comment

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

I very much approve, just wanting to make the test as explicit as possible.

XCTAssertEqual(desired.explicitStringNotBool, "true")
XCTAssertEqual(desired.singleQuotedStringNotBool, "true")
XCTAssertEqual(desired.doubleQuotedStringNotBool, "true")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a great test, thank you.

I think your point/intention would be made even more clearly and strongly if we add a call to _testRoundTrip here.
Particularly, I think it would be helpful to show that the decoded form has XCTAssertNotEqual(desired.doubleQuotedStringNotBool, .bool(true))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried _testRoundTrip, but the out YAML doesn't include the three different ways of encoding the string, so it failed.

I did update the equality assertions though - I agree, it's much clearer when not using ExpressibleBy... protocols: 3b15363

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see your point. We'd have to introduce a funny type that conforms to YamlTagProviding and RawRepresentable and basically it's weird.
I've approved. ✅😁

The windows failures are not due to this change. I re-ran the checks on another branch and they are failing the same way. I'm not sure what the solution is right now.

@czechboy0
Copy link
Contributor Author

Thanks @lynchsft, I'll let you merge when you see fit.

@lynchsft
Copy link
Collaborator

lynchsft commented Apr 2, 2025

@jpsim Do you understand the CI failure on Windows? The matrix runs in parallel, I've seen at lease three different swift versions fail the same way so I don't think it's related to any swift version.
If you recognize the issue can you suggest a course of action?

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