Skip to content

[Bug]: JSONRPCUtils.getAndValidateId throws and catches NumberFormatException for every SSE event when id is a string (client default) #1253

Closed

Description

@akshita-gupta

What happened?

On the JSON-RPC client transport, every streamed SSE event triggers a thrown-and-caught NumberFormatException in JSONRPCUtils.getAndValidateId.

The client generates a string id by default:

  • JSONRPCTransport.createPostBuilder calls JSONRPCUtils.toJsonRPCRequest(null, method, payload)
  • toJsonRPCRequest uses UUID.randomUUID().toString() when requestId is null

The server echoes that id on each streamed response. For every event: SSEEventListener.onMessage -> parseAndHandleMessage -> JSONRPCUtils.parseResponseEvent -> getAndValidateId

if (jsonRpc.get("id").isJsonPrimitive()) {
    try {
        id = jsonRpc.get("id").getAsInt();
    } catch (UnsupportedOperationException | NumberFormatException | IllegalStateException e) {
        id = jsonRpc.get("id").getAsString();
    }
}

getAsInt() on a UUID string always throws, so the exception path is the normal path.
The result is correct, but each event allocates an exception and fills its stack trace.

Version: 1.4.0.Final

Suggested fix: check the primitive type before parsing, e.g.

JsonPrimitive p = jsonRpc.getAsJsonPrimitive("id");
id = p.isNumber() ? p.getAsInt() : p.getAsString();

Optionally, allow callers to supply a request id / id generator in JSONRPCTransport instead of hardcoding null in createPostBuilder.

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. sumitvairagar commented on Oct 31, 2026

    @sumitvairagar
    Contributor

    Hi @ehsavoie @kabir 👋

    I'd like to work on this one.

    The fix is in getAndValidateId() in JSONRPCUtils — replace the try/catch with a type check before parsing:

    com.google.gson.JsonPrimitive p = jsonRpc.getAsJsonPrimitive("id");
    id = p.isNumber() ? p.getAsInt() : p.getAsString();

    This eliminates the NumberFormatException on every SSE event for UUID-based string IDs without changing the result. Will include a regression test. Let me know if you'd prefer a different approach.

  2. kabir commented on Nov 2, 2026

    @kabir
    Collaborator

    Thanks @sumitvairagar, your proposed approach makes sense. I’ve confirmed that the default UUID request IDs trigger a caught NumberFormatException on each streamed response. I’ll assign this to you.

    One detail to account for: the change will preserve numeric string IDs such as "123" as strings, whereas the current code converts them to integers. Please update the existing tests accordingly and include coverage for UUID strings, numeric strings, and numeric IDs.

    Let’s keep this focused on the parsing fix; a configurable request-ID generator can be considered separately.

  3. sumitvairagar commented on Nov 2, 2026

    @sumitvairagar
    Contributor

    PR raised: #1254

    Addressed Kabir's feedback — numeric string IDs like "123" are still converted to Integer via a precompiled pattern check, so existing behaviour is preserved. Regression tests cover all three cases: UUID string ID, numeric string ID, and JSON number literal.

  4. added a commit that references this issue on Nov 2, 2026
    9c52142
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions