Skip to content

Add tests for empty, truncated and malformed queries - #124

Open
iStefo wants to merge 1 commit into
jsonpath-standard:mainfrom
iStefo:invalid-truncated-queries
Open

iStefo wants to merge 1 commit into
jsonpath-standard:mainfrom
iStefo:invalid-truncated-queries

Conversation

@iStefo

@iStefo iStefo commented Oct 24, 2026

Copy link
Copy Markdown

Adds 11 invalid_selector tests for queries that end early or have a token where the grammar doesn't allow one. We found these while fixing an implementation (IgnacioGoldchluk/json_path#1) that raised internal exceptions on them instead of returning a syntax error. It passed the whole suite, so other hand-written parsers may have the same gap.

File Name Selector Grammar rule violated
basic empty query "" jsonpath-query = root-identifier segments, root-identifier = "$" (§2.2.1)
basic root identifier followed by number $1 segment
basic wildcard shorthand without dot $* child-segment needs "." before *
basic repeated root identifier $$ segment
basic unclosed bracketed selection $[* bracketed-selection
basic function call outside filter selector $.a.length() function-expr is only allowed inside filters
filter unclosed filter selector, no expression $[? filter-selector, bracketed-selection
filter unclosed filter selector $[?@.a==1 bracketed-selection
filter empty filter expression $[?] filter-selector = "?" S logical-expr
filter unclosed parenthesized expression $[?(@.a==1] paren-expr
functions/match missing closing parenthesis $[?match(@.a, 'a.*'] function-expr

Each test breaks exactly one rule. The corrected forms ($[*], $[?@.a==1], $[?(@.a==1)], $[?match(@.a, 'a.*')]) are valid. I avoided cases that also run into another rule (for example $[?length(@.a, since length() must be compared anyway) and parenthesized comparables like $[?@.a==(1.

All 11 are rejected by jsonpath-rfc9535 and by python-jsonpath in strict mode. cts.json was regenerated with build.sh and only adds lines (715 → 726 tests).

🤖 Generated with Claude Code

Eleven invalid-selector tests for queries that stop early or put a
token where the grammar does not allow one. A hand-written parser can
crash on these instead of reporting a syntax error, and the suite did
not cover them yet:

- basic: the empty query (RFC 9535 section 2.2.1 requires "$"), a
  token directly after the root ($1, $*, $$), an unclosed bracketed
  selection ($[*) and function call syntax outside a filter
  ($.a.length())
- filter: an unclosed filter selector with and without an expression
  ($[?, $[?@.a==1), an empty filter expression ($[?]) and an unclosed
  parenthesized expression ($[?(@.a==1])
- match: a function call missing its closing parenthesis

Each query is rejected by jsonpath-rfc9535 and by python-jsonpath in
strict mode, and the corrected form of each is accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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