json_patch: compare integer and double test values numerically

RFC 6902 section 4.6 compares JSON numbers by value. The test operation
used json_object_equal(), which treats json_type_int and json_type_double
as different types, so a test of 1.0 against 1 failed.

Use a private comparison for JSON Patch, including numbers nested inside
objects and arrays. Check floating-point range before converting, and
require both integer equality and an exact round trip to reject fractional
values and large integers that would round to the same double.

Add regression cases for both argument orders, nested values, signed and
unsigned limits, fractional values, precision loss and non-number types.

https://www.rfc-editor.org/rfc/rfc6902.txt section 4.6
This commit is contained in:
Treston Cuzzort authored and Eric Hawicz committed 2026-11-08 21:22:38 -05:00
1 parent d3c1ab9495
commit ea3756e561
3 files changed
+167 -1

No files matched your search

+24
View File
@@ -54,6 +54,30 @@ Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/0",
Testing 'push item to array via last index + 1', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/2", "value": "bar" } ]' : OK
Testing 'add item to array at index > length should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/3", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'test integer against equal double', doc '{ "n": 1 }' patch '[ { "op": "test", "path": "\/n", "value": 1.0 } ]' : OK
Testing 'test double against equal integer', doc '{ "n": 1.0 }' patch '[ { "op": "test", "path": "\/n", "value": 1 } ]' : OK
Testing 'test numeric equality inside arrays and objects', doc '{ "a": [ 1, { "n": -2.0 } ], "b": null }' patch '[ { "op": "test", "path": "", "value": { "b": null, "a": [ 1.0, { "n": -2 } ] } } ]' : OK
Testing 'test integer against negative zero', doc '0' patch '[ { "op": "test", "path": "", "value": -0.0 } ]' : OK
Testing 'test unsigned integer against equal double', doc '9223372036854775808' patch '[ { "op": "test", "path": "", "value": 9223372036854775808.0 } ]' : OK
Testing 'test minimum signed integer against equal double', doc '-9223372036854775808' patch '[ { "op": "test", "path": "", "value": -9223372036854775808.0 } ]' : OK
Testing 'test does not round integers to doubles', doc '9007199254740993' patch '[ { "op": "test", "path": "", "value": 9007199254740992.0 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not round negative integers to doubles', doc '-9007199254740993' patch '[ { "op": "test", "path": "", "value": -9007199254740992.0 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not truncate fractional numbers', doc '1' patch '[ { "op": "test", "path": "", "value": 1.5 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not truncate negative fractional numbers', doc '-1' patch '[ { "op": "test", "path": "", "value": -1.5 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not clamp out of range unsigned numbers', doc '18446744073709551615' patch '[ { "op": "test", "path": "", "value": 18446744073709551616.0 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not clamp out of range negative numbers', doc '-9223372036854775808' patch '[ { "op": "test", "path": "", "value": -18446744073709551616.0 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not coerce boolean values to numbers', doc 'true' patch '[ { "op": "test", "path": "", "value": 1.0 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test does not coerce strings to numbers', doc '1' patch '[ { "op": "test", "path": "", "value": 1.0 } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test checks numeric equality recursively', doc '{ "a": [ 1, { "n": -2 } ] }' patch '[ { "op": "test", "path": "", "value": { "a": [ 1.0, { "n": -3.0 } ] } } ]' : OK
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
Testing 'test against implementation-specific numeric parsing', doc '{ "1e0": "foo" }' patch '[ { "op": "test", "path": "\/1e0", "value": "foo" } ]' : OK
Testing 'test with bad number should fail', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/1e0", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field