Repository navigation
Reject invalid patch document containers - #236
Open
JackyHsu-X wants to merge 1 commit into
Open
JackyHsu-X wants to merge 1 commit into
JackyHsu-X wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused validation change has relevant regression coverage and no identified blocking issues.
No open issues 🎉
What changed in this PR
This PR makes invalid JSON Patch document containers consistently raise InvalidJsonPatch, while preserving existing list and tuple behavior.
Changes:
- Reject mappings, raw text, and non-iterable constructor inputs before validating operations.
- Add regression and property tests for invalid containers and valid sequences.
| File | Description |
|---|---|
| tests.py | Covers invalid inputs, unchanged documents, and sequence preservation. |
| property_tests.py | Checks rejection of generated non-array JSON documents. |
| jsonpatch.py | Validates document containers before iterating over operations. |
Review effort: Balanced
This branch has not been deployed
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.
JsonPatch.from_string('{}')andJsonPatch.from_string('""')currently create a successful no-op patch. Other non-array JSON documents, such asnull, booleans and numbers, leakTypeErrorduring iteration. RFC 6902 section 3 defines a patch document as an array of operation objects, so these inputs should raiseInvalidJsonPatchconsistently.Validate the document container before iterating over operations. Reject mappings and raw text in the Python constructor, and translate non-iterable inputs to
InvalidJsonPatch. Existing list and tuple inputs retain their container identity and application behavior.Regression tests cover direct construction, string and byte JSON input, unchanged documents when invalid input is applied in place, and valid empty/nonempty sequences. A Hypothesis property checks rejection of generated non-array JSON documents. The new regressions produce 9 failing subtests and 18 errors against the original implementation.
Validation on Windows / CPython 3.12.8:
python -m coverage run tests.py: 216 tests, OK (1 platform-specific skip), including doctests.python -m coverage run --append property_tests.py: 21 property tests, OK (the data files were collected separately and combined locally).git diff --checkpass.Related: #132 handles strings encountered while iterating over operations; this also handles empty mappings/text and non-iterable document inputs. Iterator exhaustion is tracked separately in #234.
AI disclosure: OpenAI Codex assisted with investigation, implementation, tests and this description. The reported checks were executed locally. Other Python versions and platforms have not been tested locally.