Skip to content

Add negation support to JsonPathMatcher filter expressions#7164

Merged
steve-aom-elliott merged 1 commit intomainfrom
jsonpath-negation-support
Mar 27, 2026
Merged

Add negation support to JsonPathMatcher filter expressions#7164
steve-aom-elliott merged 1 commit intomainfrom
jsonpath-negation-support

Conversation

@steve-aom-elliott
Copy link
Copy Markdown
Contributor

Summary

  • Uncomments the NOT (!) token in the ANTLR JsonPath grammar and adds a negationExpression parser rule supporting [?(!@.prop)] and [?(!(@.key == 'val'))] syntax
  • Implements visitNegationExpression in the JsonPathMatcher visitor for all three modules (rewrite-json, rewrite-yaml, rewrite-hcl), handling both list/array iteration (collecting non-matching elements) and single-scope boolean flip
  • Adds negation as an operand in &&/|| logical expressions so filters like [?(!@.x && @.y == 'z')] work
  • Adds 12 new tests across the three modules covering negated existence, negated equality, negation matching none, and negation combined with logical AND

Test plan

  • ./gradlew :rewrite-json:test --tests "org.openrewrite.json.JsonPathMatcherTest" — all pass
  • ./gradlew :rewrite-yaml:test --tests "org.openrewrite.yaml.JsonPathMatcherTest" — all pass
  • ./gradlew :rewrite-hcl:test --tests "org.openrewrite.hcl.JsonPathMatcherTest" — all pass
  • Full CI

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 26, 2026
@steve-aom-elliott steve-aom-elliott added the enhancement New feature or request label Mar 26, 2026
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Mar 26, 2026
Copy link
Copy Markdown
Member

@sambsnyd sambsnyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

The `!` (NOT) operator was previously stubbed out in the ANTLR grammar.
This enables negation in filter expressions across the JSON, YAML, and
HCL modules, supporting syntax like `[?(!@.property)]` for negating
existence checks and `[?(!(@.key == 'value'))]` for negating equality,
regex, and contains expressions. Negation also composes with `&&`/`||`
logical operators.
@steve-aom-elliott steve-aom-elliott force-pushed the jsonpath-negation-support branch from 7c4085f to 965ea48 Compare March 27, 2026 20:15
@steve-aom-elliott steve-aom-elliott merged commit 12d3ff1 into main Mar 27, 2026
1 check passed
@steve-aom-elliott steve-aom-elliott deleted the jsonpath-negation-support branch March 27, 2026 20:44
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants