Skip to content

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

Open
nyxst4ck wants to merge 1 commit into
python-jsonschema:mainfrom
nyxst4ck:fix/relative-json-pointer-leading-zero
Open

Fix relative-json-pointer rejecting multi-digit integers containing a zero#1503
nyxst4ck wants to merge 1 commit into
python-jsonschema:mainfrom
nyxst4ck:fix/relative-json-pointer-leading-zero

Conversation

@nyxst4ck

Copy link
Copy Markdown

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.)

The relative-json-pointer format checker disallowed a leading zero by checking
the *previous* character (int(instance[i - 1]) == 0), so any non-negative
integer prefix containing a 0 followed by another digit -- '100', '205',
'1000', '100/foo', '100#' -- was wrongly rejected. Per
draft-handrews-relative-json-pointer-01 section 3 only a leading zero is
forbidden ('0' alone is valid; '01' is not).

Reject only when the accumulated integer is exactly '0' and another digit
follows.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 python-jsonschema | 🛠️ Build #33172853 | 📁 Comparing 7987ad1 against latest (1a77549)

  🔍 Preview build  

1 file changed
± _modules/jsonschema/_format/index.html

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant