Preserve leading-zero JSON keys in nested items - #1956
Closed
harsh-thakkar7 wants to merge 2 commits into
Closed
harsh-thakkar7 wants to merge 2 commits into
harsh-thakkar7 wants to merge 2 commits into
Conversation
Media types are case-insensitive per RFC 2045 section 5.1, but the auto-streaming check for Server-Sent Events compared the parsed Content-Type header literally, so a server responding with e.g. 'Text/Event-Stream' silently fell back to buffered output.
A root literal such as 01=x was tokenized as a NUMBER (int coerces
'01' to 1) and emitted as the key '1', silently rewriting the key and
colliding with a real 1=x item:
$ http pie.dev/post '01=x' '1=y'
{1: y} # '01' became '1' and overwrote '1'!
Only canonical integers (whose str() form matches, i.e. no leading
zeros) are now treated as numbers; JSON keys like '01' are preserved.
Author
|
Closing this β I don't think this change is mature enough to land right now. Thanks for the project! |
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.
Summary
A nested JSON item with a leading-zero literal, e.g.
01=x, was tokenized as aNUMBER(Python'sint('01')β1) and emitted as the key"1"β silently rewriting the key and colliding with a real1=xitem, losing data:01is a valid JSON key; JSON numbers cannot have leading zeros, so coercing it to an integer is lossy.Fix
In
httpie/cli/nested_json/parse.py, only treat a literal as a number when it is canonical β i.e. itsstr()form round-trips exactly (int(value)without leading zeros / missing sign). Non-canonical literals remainTEXTkeys:Canonical integers are unaffected (
kids[0],kids[10], ... still produce numeric array indices).Tests
['01=x', '1=y']β{'01': 'x', '1': 'y'}β fails before this change (key collapsed to1).['kids[0]=Thelma', 'kids[10]=Ashley']β indices still numeric.pytest -q tests/test_json.py tests/test_cli_utils.py # 298 passed