Skip to content

test operation considers 1 and true (0 and false) equal #216

Description

@stefankoegl

RFC 6902 §4.6 compares numbers by value, but literals (true, false, null) are only equal to themselves. TestOperation compares with Python's !=, and in Python 1 == True and 0 == False. So these tests pass, although the values differ in JSON:

>>> import jsonpatch
>>> jsonpatch.apply_patch({'a': 1}, [{'op': 'test', 'path': '/a', 'value': True}])
{'a': 1}
>>> jsonpatch.apply_patch({'a': [1]}, [{'op': 'test', 'path': '/a', 'value': [True]}])
{'a': [1]}
>>> jsonpatch.apply_patch({'a': {'a': 0}}, [{'op': 'test', 'path': '/a', 'value': {'a': False}}])
{'a': {'a': 0}}

Expected: JsonPatchTestFailed. 1 against 1.0 should keep passing, since the RFC considers numerically equal numbers equal.

make_patch had the same problem (#180), fixed in #181 and 80086cd. test can't reuse _serialized_key unchanged, though: it compares json.dumps output, so it would also tell 1 and 1.0 apart. A recursive comparison would do: booleans only equal booleans, numbers compare by value, and arrays and objects compare member by member.

Related: JsonPatch.__eq__ also uses Python equality. It considers [{'op': 'test', 'path': '/a', 'value': 1}] and the same patch with True equal, although the two behave differently.

Test: OperationProperties.test_test_operation_uses_json_equality in property_tests.py is marked @unittest.expectedFailure because of this bug.

Activity

  1. stefankoegl commented on Oct 10, 2026

    @stefankoegl
    OwnerAuthor

    Fixed by 67c44eb ("Compare values in 'test' by JSON equality"), merged in #221.

    On current master (077b4f4), testing 1 against true, 0 against false, [1] against [true] or {"a": 0} against {"a": false} raises JsonPatchTestFailed, while 1 against 1.0 still passes. test_test_operation_uses_json_equality in property_tests.py no longer carries expectedFailure and passes, at 5,000 examples on two hash seeds as well.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions