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

Remove leading 0s before parsing into serde_json::Number #6766

Merged

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Feb 11, 2025

We previously decided to allow leading 0s, per this discussion.

However, since we use the parsing logic of serde_json::Number to produce the internal representation, and that logic fails given leading zeros, we need to normalize the input &str a bit before calling number.parse(). We already perform some similar normalizations, e.g. ensuring the fractional part is at least a 0 (not empty after the .), so there's precedent.

Thanks to @nicholascioli for finding this problem using fuzz testing!

@benjamn benjamn self-assigned this Feb 11, 2025
@benjamn benjamn requested a review from a team as a code owner February 11, 2025 17:30
@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Feb 11, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: 42b09e789001199f80796a4e

This comment has been minimized.

benjamn added a commit that referenced this pull request Feb 11, 2025
@benjamn benjamn requested a review from a team as a code owner February 11, 2025 17:33
We previously decided to allow leading 0s, per this discussion:
#5762 (comment)

However, since we use the parsing logic of `serde_json::Number` to
produce the internal representation, and that logic fails given leading
zeros, we need to normalize the input `&str` a bit before calling
`number.parse()`. We already perform some similar normalizations, e.g.
ensuring the fractional part is at least a 0 (not empty after the `.`),
so there's precedent.
@benjamn benjamn force-pushed the benjamn/avoid-error-on-leading-0s-in-LitExpr-parse_number branch from 0510cb5 to 7a76bec Compare February 11, 2025 17:34
@benjamn benjamn enabled auto-merge (squash) February 11, 2025 17:34
Comment on lines +329 to +332
check_parse(
"-00",
LitExpr::Number(serde_json::Number::from_f64(-0.0).unwrap()),
);
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess negative zero has to be a float, since an integer wouldn't be able to represent the negativity?

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds right!

Copy link
Contributor

@nicholascioli nicholascioli left a comment

Choose a reason for hiding this comment

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

LGTM! I added a few nits, but feel free to ignore.

Comment on lines +329 to +332
check_parse(
"-00",
LitExpr::Number(serde_json::Number::from_f64(-0.0).unwrap()),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds right!

@benjamn benjamn merged commit a676ae0 into dev Feb 11, 2025
15 checks passed
@benjamn benjamn deleted the benjamn/avoid-error-on-leading-0s-in-LitExpr-parse_number branch February 11, 2025 21:20
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.

3 participants