Skip to content

MOD-21463 , Fix JSON.SET responses for unmatched dynamic paths and invalid object… - #1721

Open
TalBarYakar wants to merge 2 commits into
masterfrom
tal.ba/bug/json_set_missmatch
Open

TalBarYakar wants to merge 2 commits into
masterfrom
tal.ba/bug/json_set_missmatch

Conversation

@TalBarYakar

@TalBarYakar TalBarYakar commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

… parents

JSON.SET previously returned "ERR wrong static path" for valid dynamic paths that matched nothing. With auto-create disabled, NX also returned this error when a filter matched an existing target. Static paths that attempted to add an object field beneath a number returned nil instead of reporting the incompatible parent type.

Update JSON.SET preparation and response handling:

  • Validate the trailing object-field chain of static, non-projection paths for SET and NX. Return the existing expected-object type error when traversal encounters a non-object parent. Stop validation at missing fields so existing creation rules continue to apply. XX retains its existing conditional-write behavior.

  • With auto-create disabled, resolve dynamic paths through find_existing_targets instead of attempting to prepare field creation. Existing matches remain writable for ordinary SET and XX. NX omits existing targets from updates.

  • Make find_existing_targets crate-visible so JSON.SET can reuse it. Its implementation is unchanged.

  • Return nil when a dynamic path produces no updates and an empty, successful additions list. Preserve preparation errors rather than treating them as empty results.

  • Keep prepare_set for auto-create-enabled paths. Preserve its tracking of existing targets omitted by NX, so unmet NX conditions return nil.

Response changes:

  1. Intermediate value is a number Document: {"a":1} Command: JSON.SET doc '$.a.b' 2 Before, auto-create OFF and ON: nil After, auto-create OFF and ON: WRONGTYPE wrong type of path value - expected object

  2. Filter matches nothing Document: [{"age":31}] Command: JSON.SET doc '$[?(@.age==30)].age' 32 Before, auto-create OFF and ON: ERR wrong static path After, auto-create OFF and ON: nil

  3. Wildcard matches nothing Document: {} Command: JSON.SET doc '$.*' 1 Before, auto-create OFF and ON: ERR wrong static path After, auto-create OFF and ON: nil

  4. NX with a matching filter Document: [{"age":30}] Command: JSON.SET doc '$[?(@.age==30)].age' 31 NX Before, auto-create OFF: ERR wrong static path Before, auto-create ON: nil After, auto-create OFF and ON: nil

  5. NX with a filter that matches nothing Document: [{"age":31}] Command: JSON.SET doc '$[?(@.age==30)].age' 32 NX Before, auto-create OFF and ON: ERR wrong static path After, auto-create OFF and ON: nil

All five examples leave the document unchanged.

A valid dynamic selector with zero targets represents a no-op. An existing target excluded by NX also represents a no-op. Neither requires a static-path error. Attempting to traverse a number as an object is instead a type mismatch and should report an error.

The empty-result handling also covers other dynamic selectors, including recursive descent and slices. Parent validation covers non-object values in the trailing object-field chain; it is not a general validation of every possible JSONPath traversal.

Missing-key handling, static out-of-bounds index handling, and MSET/MERGE behavior remain unchanged.

Update regression coverage for both auto-create modes, unchanged-document expectations, conditional-write behavior, and no-write notifications.


Note

Medium Risk
Changes core JSON.SET preparation, validation, and reply semantics for static and dynamic paths across auto-create modes, which affects client-visible behavior and write notifications.

Overview
JSON.SET now distinguishes no-op dynamic paths, type errors on static paths, and conditional-write nil instead of blanket ERR wrong static path in several cases.

For static, non-projection paths (excluding XX), validate_set_parent runs before preparation: it rejects out-of-range intermediate array indexes and returns the existing expected object error when the trailing object-field chain would traverse a non-object parent (e.g. $.a.b under "a":1). Missing fields still defer to auto-create rules.

When auto-create is off and the path is dynamic, SET uses find_existing_targets (newly pub(crate)) instead of prepare_set, so existing matches remain writable but new fields cannot be invented. If there are no updates and no planned additions, the command returns nil for dynamic paths, XX, or NX cases that only skip existing targets—rather than wrong static path or silent nil on type mismatch.

Regression tests cover both auto-create modes, intermediate index errors, keyspace events on rejected SETs, and updated expectations for wildcards, filters, and $.a.b under scalars.

Reviewed by Cursor Bugbot for commit 797b528. Bugbot is set up for automated code reviews on this repo. Configure here.

… parents

JSON.SET previously returned "ERR wrong static path" for valid dynamic
paths that matched nothing. With auto-create disabled, NX also returned
this error when a filter matched an existing target. Static paths that
attempted to add an object field beneath a number returned nil instead
of reporting the incompatible parent type.

Update JSON.SET preparation and response handling:

- Validate the trailing object-field chain of static, non-projection
  paths for SET and NX. Return the existing expected-object type error
  when traversal encounters a non-object parent. Stop validation at
  missing fields so existing creation rules continue to apply.
  XX retains its existing conditional-write behavior.

- With auto-create disabled, resolve dynamic paths through
  find_existing_targets instead of attempting to prepare field creation.
  Existing matches remain writable for ordinary SET and XX.
  NX omits existing targets from updates.

- Make find_existing_targets crate-visible so JSON.SET can reuse it.
  Its implementation is unchanged.

- Return nil when a dynamic path produces no updates and an empty,
  successful additions list. Preserve preparation errors rather than
  treating them as empty results.

- Keep prepare_set for auto-create-enabled paths. Preserve its tracking
  of existing targets omitted by NX, so unmet NX conditions return nil.

Response changes:

1. Intermediate value is a number
   Document: {"a":1}
   Command: JSON.SET doc '$.a.b' 2
   Before, auto-create OFF and ON: nil
   After, auto-create OFF and ON:
     WRONGTYPE wrong type of path value - expected object

2. Filter matches nothing
   Document: [{"age":31}]
   Command: JSON.SET doc '$[?(@.age==30)].age' 32
   Before, auto-create OFF and ON: ERR wrong static path
   After, auto-create OFF and ON: nil

3. Wildcard matches nothing
   Document: {}
   Command: JSON.SET doc '$.*' 1
   Before, auto-create OFF and ON: ERR wrong static path
   After, auto-create OFF and ON: nil

4. NX with a matching filter
   Document: [{"age":30}]
   Command: JSON.SET doc '$[?(@.age==30)].age' 31 NX
   Before, auto-create OFF: ERR wrong static path
   Before, auto-create ON: nil
   After, auto-create OFF and ON: nil

5. NX with a filter that matches nothing
   Document: [{"age":31}]
   Command: JSON.SET doc '$[?(@.age==30)].age' 32 NX
   Before, auto-create OFF and ON: ERR wrong static path
   After, auto-create OFF and ON: nil

All five examples leave the document unchanged.

A valid dynamic selector with zero targets represents a no-op.
An existing target excluded by NX also represents a no-op.
Neither requires a static-path error. Attempting to traverse a number
as an object is instead a type mismatch and should report an error.

The empty-result handling also covers other dynamic selectors, including
recursive descent and slices. Parent validation covers non-object values
in the trailing object-field chain; it is not a general validation of
every possible JSONPath traversal.

Missing-key handling, static out-of-bounds index handling, and MSET/MERGE
behavior remain unchanged.

Update regression coverage for both auto-create modes, unchanged-document
expectations, conditional-write behavior, and no-write notifications.

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 543777e. Verdict: comment. One existing flow test breaks with the new reply.

  1. tests/pytest/test_keyspace_notifications.py, test_keyspace_set: line 49 still expects None from JSON.SET test_key '$.foo.a' '"nono"' where $.foo is a string. With validate_set_parent in json_set_command_impl, that command now returns WRONGTYPE wrong type of path value - expected object, so the test errors with an unhandled exception. This is the failure in both linux-sanitizer / clang-sanitizer (1 of 375 tests failed) and linux-coverage / coverage. Fix: change that assertion to expect the error, and keep the following pubsub.get_message check so the no-event guarantee stays covered.

Non-blocking:

  • SET, MSET and MERGE now disagree on the same input. With {"s":3}, JSON.SET k '$.s.a.b' 1 errors, while JSON.MSET k '$.s.a.b' 1 and JSON.MERGE k '$.s.a.b' 1 still return nil. Likewise a dynamic selector with no match is nil for SET but still ERR wrong static path for MSET and MERGE (test_impossible_suffix_preserves_depth_and_no_write_replies). The description says this is intended; worth a follow-up ticket so the commands converge.
  • Legacy paths also go through the new check, since compile(path.get_path()) normalizes them. JSON.SET k .a.b 2 on {"a":1} changes reply for legacy-path clients too. A test case for the legacy form would pin it down.
  • validate_set_parent runs before prepare_set, so for an over-deep static path that crosses a scalar, the reply is now expected object instead of whatever depth error prepare_set would give. That ordering seems fine, but it isn't tested.
  • validate_set_parent adds one more prefix evaluation for every static SET on an existing key. It should be cheap. The command-benchmark jobs were still running when I looked.
  • I checked the dynamic-path branch for auto-create off against find_paths: before this change, validate_legacy_creation_path already rejected dynamic paths before any final-key addition, so find_existing_targets loses no creation behavior. Only the empty-result reply changes.

I built the module at 543777e and at master with cargo build --release -p redis_json (no warnings). I could not run the flow tests: the local redis-server is 7.0 and crashes on module load. The finding above comes from the CI logs and from reading the diff and the surrounding code.

CI: linux-sanitizer / clang-sanitizer and linux-coverage / coverage fail on test_keyspace_notifications:test_keyspace_set (code). build-linux-x64 / x64-amazonlinux2023 fails fetching the ciborium crate with an SSL error (infrastructure). Most build and valgrind jobs were still running.


Generated by Claude Code

…1464)

JSON.SET returned nil for '$[5].a' but an array-index error for
'$[5].a[0]' when element 5 did not exist. The response depended on
the final selector even though both paths failed at the same index.

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 797b528. Verdict: comment. The flow-test failure from my previous review is still there, and it now fails every test job.

  1. tests/pytest/test_keyspace_notifications.py, test_keyspace_set: line 49 still expects None from JSON.SET test_key '$.foo.a' '"nono"' while $.foo is the string "gogo". validate_set_parent in json_set_command_impl now returns WRONGTYPE wrong type of path value - expected object, so the test raises. It is the only failed test (1 of 376) in linux-sanitizer, linux-coverage, linux-valgrind, every build-linux-x64 and build-linux-arm64 job I opened, and macos / Build for macos-15. Fix: assert the error with env.expect(...).raiseError().contains('expected object'), and keep the pubsub.get_message check after it so the no-event guarantee stays covered.

Non-blocking:

  • The new intermediate-index loop in validate_set_parent looks right. I traced $[0].a[0].b.c with a = [1]: the index exists, so the loop passes and the object check returns expected object. With $.a.b[5].c and a = 1, the parent of [5] is not an array, so behavior is unchanged. XX skips the check and still returns nil, as the new test asserts.
  • JSON.MSET and JSON.MERGE do not call validate_set_parent, so $[5].a on an array now errors for SET but not for MSET and MERGE. This adds to the SET/MSET/MERGE divergence I noted before. A follow-up ticket would cover it.
  • The loop clones the Query once per token and evaluates up to two prefixes per numeric token, on every static SET to an existing key. That is cheap for normal paths. A short comment saying the cost scales with path depth would help.
  • There is no Rust unit test for the new branch. The no_write helpers in auto_create::tests would make one easy, for example $[5].a and $[0].a[5].b on [{"a":[1,2]}].
  • The second commit's subject starts with "ix" instead of "Fix".
  • The branch is one commit behind master. jsonpath-benchmark / benchmark reports +40% on $..absent, $..uid and similar recursive-descent cases. This PR does not touch json_path. Master has the recursive-descent optimization from #1717 and this branch does not, so the gate is comparing the old code against the new. A rebase should clear it.

I ran cargo test -p redis_json --lib at 797b528: 121 passed. I did not run the flow tests. The finding above comes from the CI logs and from reading the code.

CI: 39 jobs fail. All the test jobs I checked fail only on test_keyspace_notifications:test_keyspace_set (code). The new test_set_out_of_range_intermediate_index passes. jsonpath-benchmark / benchmark fails because the branch is behind master. 4 arm64 jobs were still running.


Generated by Claude Code

This branch has not been deployed

No deployments
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.

2 participants