Fix test operation to respect JSON types (boolean vs number) - #187
Open
cognis-digital wants to merge 1 commit into
Open
Fix test operation to respect JSON types (boolean vs number)#187cognis-digital wants to merge 1 commit into
cognis-digital wants to merge 1 commit into
Conversation
RFC 6902 requires the test operation to only consider values equal when they are of the same JSON type. Because Python treats bool as a subclass of int (True == 1, False == 0), a test against the boolean true/false literal wrongly succeeded against the numbers 1/0 (and vice versa). Introduce a small recursive equality helper that keeps booleans distinct from numbers while still comparing numbers numerically (1 == 1.0) and recursing into arrays and objects. Wire it into TestOperation.apply and add regression tests.
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.
What
The
testoperation currently compares values with a plain!=. Because Python treatsboolas asubclass of
int(True == 1,False == 0), atestagainst the JSONtrue/falseliteralincorrectly succeeds against the numbers
1/0, and vice versa.Why
RFC 6902 §4.6 defines
testequality as requiring the two values to be "of the same JSON type". Aboolean literal and a number are different JSON types and must not compare equal. This mirrors the
intent already present in
DiffBuilder._compare_values, which usesjson.dumpsspecifically so itcan "recognize the difference between 1 and True".
How
Adds a small recursive helper,
_compare_json_values, that keeps booleans distinct from numbers,still compares numbers numerically (
1 == 1.0), and recurses into arrays and objects so the rulealso holds for nested values.
TestOperation.applyuses it in place of!=. The error type andmessage are unchanged; there is no public API change.
Tests
Adds regression tests covering scalar
bool/number mismatches in both directions, the0/falsecase,
1/1.0numeric equality (still passes), same-typed booleans (still pass), and nested[1]vs[true]/{"q": 1}vs{"q": true}cases. Full suite: 114 passing.