Repository navigation
Serialize decimals that round to a whole number with a fractional digit - #86
Merged
Merged
Conversation
serializeDecimal rounds with toFixed(3) and strips trailing zeros, so 1.0001 became "1." and 9.9999 became "10.". RFC 9651 section 4.1.5 requires a 0 after the period when the fractional component is zero, and parseItem rejects both strings with "Decimal cannot end on a period".
Owner
|
Dang, thank you! |
evert
approved these changes
Oct 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decimals that round to a whole number serialize to a string that ends in a period:
That isn't a valid Decimal, and this library's own parser rejects it:
RFC 9651 section 4.1.5 (same as RFC 8941) says "If input_decimal's fractional component is zero, append '0' to output", so the expected values are
1.0,10.0and-0.0.serializeDecimalrounds withtoFixed(3)and then strips trailing zeros withreplace(/0+$/, ''). When all three fractional digits are zero, that also leaves nothing after the period. The fix appends a0in that case. Everything else inserializeDecimalis unchanged, including the 12-digit check, which already runs on the rounded value.The httpwg tests don't catch this because their serialization inputs that round to a whole number are integers in JavaScript.
Testing
Added a case to the
serializeItemtests intest/serializer-tests.jsthat serializes1.0001,9.9999and-0.0001, checks the output, and parses it back.make test(lint, build, httpwg tests and the serializer tests): 2823 pass, 0 fail.src/serializer.ts, the new test fails and everything else passes.I also added a changelog entry under an unreleased 2.1.1 heading. Happy to change or drop that if you'd rather write it yourself.