jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Fix relative-json-pointer rejecting multi-digit integers containing a zero

Open nyxst4ck opened this issue 5 months ago โ€ข 2 comments

The relative-json-pointer format checker rejects valid pointers whose non-negative-integer prefix contains a 0 after the first digit.

The leading-zero guard checked the previous character rather than the first digit:

# jsonschema/_format.py, is_relative_json_pointer
if i > 0 and int(instance[i - 1]) == 0:
    return False

So any integer prefix with a 0 followed by another digit was wrongly rejected, while the bundled conformance suite only exercises "120" (where the 0 is last) and so never caught it:

>>> from jsonschema import FormatChecker
>>> FormatChecker().conforms("100", "relative-json-pointer")
False        # should be True
>>> FormatChecker().conforms("100/foo", "relative-json-pointer")
False        # should be True

Per draft-handrews-relative-json-pointer-01 ยง3, only a leading zero is forbidden: "0" by itself is valid, "01" is not, and "100" / "205" / "1000" are valid.

Fix

Reject only when the accumulated integer is exactly "0" and another digit follows (i.e. a genuine leading zero), instead of rejecting any digit that follows a 0.

Tests

Added test_relative_json_pointer_allows_multidigit_integers to jsonschema/tests/test_format.py: 0/1/120/100/205/1000/100/foo/100# are valid and 01/00/007/01/a/01# are not. It fails on main and passes with the fix. Full test_format.py + the JSON Schema test suite stay green (7628 passed); ruff clean.

(Happy to add a CHANGELOG.rst entry if you'd like.)

nyxst4ck avatar Jun 16 '26 23:06 nyxst4ck

Documentation build overview

๐Ÿ“š python-jsonschema | ๐Ÿ› ๏ธ Build #33172853 | ๐Ÿ“ Comparing 7987ad18d9cd2667ea99646aa18dc3efb72395ca against latest (1a77549ded95c324ce0d22fe00c0f4d77141c481)

ย  ๐Ÿ” Preview build ย 

1 file changed
ยฑ _modules/jsonschema/_format/index.html

I'd encourage you to submit this test to the upstream official test suite!

Julian avatar Jun 19 '26 15:06 Julian

Thanks for the pointer. I checked the official test suite before opening anything new, and this gap is already covered by json-schema-org/JSON-Schema-Test-Suite#942 (100 as a valid relative-json-pointer prefix with an interior zero). Since that upstream PR predates this one and its CI is green, I will not duplicate it. If maintainers there ask for additional cases beyond 100, I can help there instead.

nyxst4ck avatar Jun 30 '26 01:06 nyxst4ck

Rebased this PR on current main to clear the merge conflict.

main already contains the implementation change for the relative-json-pointer prefix parsing, so the rebased PR now keeps the added regression coverage for multi-digit integer prefixes with internal/trailing zeroes.

Validation:

  • python -m pytest jsonschema/tests/test_format.py -q -> 8 passed, 1 skipped

nyxst4ck avatar Jul 14 '26 02:07 nyxst4ck