fix: prevent panic on nil intermediate values during Set operations #49
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.
Fix panic on nil intermediate values during Set operations
Problem
The library panics with
reflect: call of reflect.Value.Type on zero Value
when setting values through JSON paths containing nil intermediate values. This violates RFC 6901 Section 7, which requires implementations to raise error conditions for unresolvable paths.Reproduction
Solution
Added
isNil()
checks in two locations:setSingleImpl()
(line 183): Before callingrValue.Type()
on potentially zero reflect.Valueset()
traversal loop (line 294): Before reflection operations during path traversalReturns descriptive errors like
"cannot set field X on nil value"
instead of panicking.RFC 6901 Compliance
Per RFC 6901 Section 7: implementations must "raise an error condition" for unresolvable paths rather than crash.
Testing
Added comprehensive test cases covering direct nil traversal, multi-level paths, and path creation scenarios. All tests pass with proper error handling.
Impact