Repository navigation
MOD-21463 , Fix JSON.SET responses for unmatched dynamic paths and invalid object… - #1721
TalBarYakar wants to merge 2 commits into
Conversation
… 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
left a comment
There was a problem hiding this comment.
Reviewed at 543777e. Verdict: comment. One existing flow test breaks with the new reply.
tests/pytest/test_keyspace_notifications.py,test_keyspace_set: line 49 still expectsNonefromJSON.SET test_key '$.foo.a' '"nono"'where$.foois a string. Withvalidate_set_parentinjson_set_command_impl, that command now returnsWRONGTYPE wrong type of path value - expected object, so the test errors with an unhandled exception. This is the failure in bothlinux-sanitizer / clang-sanitizer(1 of 375 tests failed) andlinux-coverage / coverage. Fix: change that assertion to expect the error, and keep the followingpubsub.get_messagecheck 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' 1errors, whileJSON.MSET k '$.s.a.b' 1andJSON.MERGE k '$.s.a.b' 1still return nil. Likewise a dynamic selector with no match is nil for SET but stillERR wrong static pathfor 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 2on{"a":1}changes reply for legacy-path clients too. A test case for the legacy form would pin it down. validate_set_parentruns beforeprepare_set, so for an over-deep static path that crosses a scalar, the reply is nowexpected objectinstead of whatever depth errorprepare_setwould give. That ordering seems fine, but it isn't tested.validate_set_parentadds one more prefix evaluation for every static SET on an existing key. It should be cheap. Thecommand-benchmarkjobs 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_pathalready rejected dynamic paths before any final-key addition, sofind_existing_targetsloses 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
left a comment
There was a problem hiding this comment.
Reviewed at 797b528. Verdict: comment. The flow-test failure from my previous review is still there, and it now fails every test job.
tests/pytest/test_keyspace_notifications.py,test_keyspace_set: line 49 still expectsNonefromJSON.SET test_key '$.foo.a' '"nono"'while$.foois the string"gogo".validate_set_parentinjson_set_command_implnow returnsWRONGTYPE wrong type of path value - expected object, so the test raises. It is the only failed test (1 of 376) inlinux-sanitizer,linux-coverage,linux-valgrind, everybuild-linux-x64andbuild-linux-arm64job I opened, andmacos / Build for macos-15. Fix: assert the error withenv.expect(...).raiseError().contains('expected object'), and keep thepubsub.get_messagecheck after it so the no-event guarantee stays covered.
Non-blocking:
- The new intermediate-index loop in
validate_set_parentlooks right. I traced$[0].a[0].b.cwitha=[1]: the index exists, so the loop passes and the object check returnsexpected object. With$.a.b[5].canda= 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.MSETandJSON.MERGEdo not callvalidate_set_parent, so$[5].aon 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
Queryonce 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_writehelpers inauto_create::testswould make one easy, for example$[5].aand$[0].a[5].bon[{"a":[1,2]}]. - The second commit's subject starts with "ix" instead of "Fix".
- The branch is one commit behind master.
jsonpath-benchmark / benchmarkreports +40% on$..absent,$..uidand similar recursive-descent cases. This PR does not touchjson_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
… 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:
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
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
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
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
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
nilinstead of blanketERR wrong static pathin several cases.For static, non-projection paths (excluding XX),
validate_set_parentruns 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.bunder"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(newlypub(crate)) instead ofprepare_set, so existing matches remain writable but new fields cannot be invented. If there are no updates and no planned additions, the command returnsnilfor dynamic paths, XX, or NX cases that only skip existing targets—rather thanwrong static pathor 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.bunder scalars.Reviewed by Cursor Bugbot for commit 797b528. Bugbot is set up for automated code reviews on this repo. Configure here.