Skip to content

fix: guard against JsonNull when parsing response error field #858

Open
RainYuY wants to merge 1 commit intoa2aproject:mainfrom
RainYuY:RainYuY-patch-1
Open

fix: guard against JsonNull when parsing response error field #858
RainYuY wants to merge 1 commit intoa2aproject:mainfrom
RainYuY:RainYuY-patch-1

Conversation

@RainYuY
Copy link
Copy Markdown

@RainYuY RainYuY commented May 7, 2026

Description

Root Cause

The serializer and deserializer are inconsistent:

  • A2AErrorTypeAdapter.write() emits "error": null for null errors
  • parseResponseBody() assumes the key's presence implies a non-null object

The correct long-term fix is to also stop emitting "error": null in
A2AErrorTypeAdapter.write(), but this client-side guard is sufficient to
prevent the crash.

Impact

Without this fix, every non-streaming A2A call fails with ClassCastException
regardless of whether the response is actually an error.


Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Follow the CONTRIBUTING Guide.
  • Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
    • Important Prefixes for release-please:
      • fix: which represents bug fixes, and correlates to a SemVer patch.
      • feat: represents a new feature, and correlates to a SemVer minor.
      • feat!:, or fix!:, refactor!:, etc., which represent a breaking change (indicated by the !) and will result in a SemVer major.
  • Ensure the tests pass
  • Appropriate READMEs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@RainYuY RainYuY changed the title fix: guard against JsonNull when parsing JSONRPC error field fix: guard against JsonNull when parsing response error field May 7, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the JSON-RPC response parsing in JSONRPCUtils.java to handle cases where the 'error' field is present but null. The review feedback suggests optimizing this logic by retrieving the 'error' element once to avoid multiple lookups and recommends applying this pattern consistently to other similar methods like parseResponseEvent.

Comment on lines +309 to 311
if (jsonRpc.has("error") && !jsonRpc.get("error").isJsonNull()) {
return parseError(jsonRpc.getAsJsonObject("error"), id, method);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation performs multiple lookups for the 'error' field. A more efficient approach is to retrieve the element once. Since the JSON-RPC specification defines the error field as an object, we can rely on this constraint and avoid additional defensive type checks for primitives. This same pattern should be applied to parseResponseEvent at line 293.

        JsonElement errorNode = jsonRpc.get("error");
        if (errorNode != null && !errorNode.isJsonNull()) {
            return parseError(errorNode.getAsJsonObject(), id, method);
        }
References
  1. When handling return types constrained by a formal specification, it is acceptable to rely on the specification's constraints rather than adding defensive checks for types that the specification explicitly excludes.

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.

1 participant