fix: guard against JsonNull when parsing response error field by RainYuY · Pull Request #858 · a2aproject/a2a-java · GitHub
Skip to content

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

Merged
kabir merged 3 commits into
a2aproject:mainfrom
RainYuY:RainYuY-patch-1
May 14, 2026
Merged

kabir merged 3 commits into
a2aproject:mainfrom
RainYuY:RainYuY-patch-1

Conversation

@RainYuY

@RainYuY RainYuY commented May 7, 2026

Copy link
Copy Markdown
Contributor

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

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

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.

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

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.

@kabir
kabir merged commit 87bd30f into a2aproject:main May 14, 2026
8 checks passed
@kabir

kabir commented May 14, 2026

Copy link
Copy Markdown
Collaborator

salaboy pushed a commit to salaboy/a2a-java that referenced this pull request Jun 19, 2026
…ject#858)

* fix: guard against JsonNull when parsing JSONRPC error field

* Refactor error handling in JSONRPCUtils
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