Repository navigation
fix(jsonrpc): avoid NumberFormatException on every SSE event for string request IDs - #1254
Conversation
|
I think this needs two changes before merging:
Please use the primitive type check proposed in the issue: JsonPrimitive primitive = jsonRpc.getAsJsonPrimitive("id");
id = primitive.isNumber() ? primitive.getAsInt() : primitive.getAsString();This removes the exception cost for UUID IDs and preserves all string IDs without parsing them as integers. Please update the numeric-string test to expect a The PR's 52 tests passed when compiled and run against local dependencies; the additional probes exposed the regression above. |
|
Also you need to rebase on the current state of the main branch |
…ng request IDs JSONRPCUtils.getAndValidateId() called getAsInt() on every JSON-RPC response ID, which threw NumberFormatException for string IDs (the UUID-based client default) before falling back to getAsString(). For a streaming session this exception was thrown and caught on every single SSE event, allocating a full stack trace each time. Fix: check the JSON primitive type before attempting integer parsing. JSON number literals are read with getAsInt() directly. String primitives — UUIDs, numeric strings, any string — are returned as String without parsing or exception. Updated existing tests that asserted numeric string IDs (e.g. "1", "2", "5") returned Integer — those now correctly expect String. Added four regression tests covering UUID string ID, numeric string ID, out-of-range numeric string, and JSON number literal. This fixes a2aproject#1253.
1377f81 to
9c52142
Compare
|
Thanks @kabir — both issues addressed in the updated commit:
Also rebased onto current |
|
The code changes look good now, thanks. Could you also update the PR description? It still describes the regex approach and says numeric string IDs are converted to integers. Please describe the primitive type check, clarify that all string IDs remain strings, and update the test summary to include the out-of-range numeric string case and the 54 passing tests. |
|
Done @kabir — PR description updated to reflect the primitive type check, clarify that all string IDs remain strings, and include the out-of-range numeric string test case and the 54 passing tests. |
|
Thanks @sumitvairagar ! |
What
JSONRPCUtils.getAndValidateId()calledgetAsInt()on every JSON-RPC response ID, which threwNumberFormatExceptionfor string IDs (the UUID-based client default) before falling back togetAsString(). For a streaming session this exception was thrown and caught on every single SSE event, allocating a full stack trace each time.Why it matters
The client generates a UUID string as the request ID by default (
UUID.randomUUID().toString()intoJsonRPCRequest). The server echoes that ID on each streamed response. For every event in a streaming session,parseResponseEvent→getAndValidateIdthrewNumberFormatExceptionbefore reading the ID correctly. The result was correct, but the cost was one stack-trace allocation per event.Fix
Check the JSON primitive type before attempting integer parsing:
123) →IntegerviagetAsInt(), no exception."123","2147483648", or any other string) → returned asStringwithout parsing. No exception, no overflow risk.Tests
Updated existing tests that asserted numeric string IDs (e.g.
"1","2","5") returnedInteger— those now correctly expectString.Four regression tests added to
JSONRPCUtilsTest:testGetAndValidateId_UuidStringId_ReturnsString— UUID string returnsString, no exceptiontestGetAndValidateId_NumericStringId_ReturnsString—"123"returnsString(not parsed as Integer)testGetAndValidateId_NumericStringIdOutsideIntRange_ReturnsString—"2147483648"returnsStringsafely (no overflow)testGetAndValidateId_NumericId_ReturnsInteger— JSON number literal42returnsInteger54 tests pass.
Fixes #1253.