fix: handle invalid JSON fields in cryptor plugin - #6960
Merged
Conversation
Aias00
approved these changes
Aug 20, 2026
Aias00
left a comment
Contributor
There was a problem hiding this comment.
PR #6960 Review — fix: handle invalid JSON fields in cryptor plugin
Verdict: APPROVE
Scope
JsonUtil.parser(String json, String fieldName) in shenyu-plugin-cryptor. Adds null/type
safety when extracting a (possibly nested) field from a JSON body.
Verification
import java.util.Objectsis already present (head file L18) — compile is fine.- Top-level guard:
GsonUtils.getInstance().toObjectMap(json)returnsnullfor non-object
JSON (array"[...]", bare string, malformed). Now returnsnullinstead of propagating. - Nested branch rewrite:
- First segment cast to
JsonObjectis now guarded byinstanceof(was an unchecked cast
that could throwClassCastException). - Every intermediate node is checked for
nullandisJsonObject()before descending. - Final node is checked for
isJsonPrimitive()beforegetAsString()(was
getAsJsonPrimitive()which threw on objects/arrays). - All early-exit paths return
nullconsistently.
- First segment cast to
- Non-nested
elsebranch is unchanged:map.get(fieldName) == null ? null : .toString().
Callers (e.g. theSet<String>overload inparser(json, fieldNames)) already filter out
blank/null results, so returningnullhere is safe and matches prior missing-field behavior. - Logic traced against every new test case; all 8 negative cases resolve to
nulland both
positive cases (nameanddata.nested.name) return the expected value.
Tests
JsonUtilTest added (49 lines): positive + non-object body + 6 invalid-nested-path cases.
Covers the exact exception paths this PR eliminates (NPE / CCE / IllegalStateException).
Notes
- Behavioral change: a nested path whose final node is a JSON object/array now returns
null
instead of throwing. In the cryptor contextnullalready means "no field to process", so
this is the intended safe fallback, not a silent data-loss. - No public API/signature change; no new dependency.
Clean, minimal, well-tested null-safety fix. Approving.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #6878
Return
nullwhen the request or response body is not a JSON object.Validate every node in a nested field path before accessing it.
Prevent
NullPointerException,ClassCastException, andIllegalStateExceptionwhen a configured field is missing or has an unexpected JSON type.Add unit tests covering non-object bodies and invalid nested paths.
You have read the contribution guidelines.
You submit test cases (unit or integration tests) that back your changes.
Your local test passed
./mvnw clean install -Dmaven.javadoc.skip=true.