Skip to content

fix: accept spec-valid null/string JSON-RPC response ids - #159

Open
sainathr19 wants to merge 2 commits into
software-mansion:masterfrom
sainathr19:fix/jsonrpc-response-nullable-id
Open

fix: accept spec-valid null/string JSON-RPC response ids#159
sainathr19 wants to merge 2 commits into
software-mansion:masterfrom
sainathr19:fix/jsonrpc-response-nullable-id

Conversation

@sainathr19

Copy link
Copy Markdown
Contributor

Fixes #158.

Problem

JsonRpcResponse typed the response id as u64, but JSON-RPC 2.0 allows an id to be a
string, a number, or null (a server sends null when it can't determine the request id,
e.g. parse / server errors). An error response with "id": null failed to deserialize the
#[serde(untagged)] enum, so the server's error.message was discarded and callers only saw:

data did not match any variant of untagged enum JsonRpcResponse

Change

  • Add a JsonRpcId enum (Number(u64) | String(String)) and type both JsonRpcResponse
    variants' id as Option<JsonRpcId> (null → None).
  • The HTTP batch transport correlates responses to requests by numeric id; it now extracts
    JsonRpcId::Number and treats a non-numeric id as out of range (routed through the existing
    UnexpectedResponseId check). No new error variant.
  • Regression tests for null / string / numeric ids.

Note: this changes the public id field type from u64 to Option<JsonRpcId>.

Verification

  • cargo build -p starknet-rust-providers
  • cargo test -p starknet-rust-providers --lib → 39 passed, 0 failed (incl. 2 new tests) ✓
  • cargo fmt --check ✓ · cargo clippy

@sainathr19
sainathr19 requested a review from a team as a code owner August 21, 2026 11:52
JsonRpcResponse typed the response id as u64, but JSON-RPC 2.0 allows an id to be a string, number, or null (servers send null for errors raised before the id is known). An error response with "id": null failed to deserialize the untagged JsonRpcResponse, so the server's error message was discarded and callers only saw "data did not match any variant of untagged enum JsonRpcResponse".

Introduce a JsonRpcId enum (Number | String) and type the field as Option<JsonRpcId> (null -> None), with a JsonRpcId::as_u64 helper. The HTTP batch transport and the WebSocket transport both correlate responses to requests by numeric id; each now recovers the number via as_u64 and treats a non-numeric id as unmatched. Adds regression tests.

Fixes software-mansion#158
@sainathr19
sainathr19 force-pushed the fix/jsonrpc-response-nullable-id branch from cb66888 to 7ea3606 Compare August 22, 2026 04:26
Comment thread starknet-rust-providers/src/jsonrpc/transports/http.rs Outdated
@franciszekjob

Copy link
Copy Markdown
Contributor

Hey @sainathr19 👋 , did you have a chance to see my comment?

@franciszekjob franciszekjob 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.

@sainathr19 thanks for reply, pls take a look at the comment.
Also, let's update changelog.

Comment thread starknet-rust-providers/src/jsonrpc/mod.rs Outdated
Comment thread starknet-rust-providers/src/jsonrpc/transports/http.rs Outdated
…-numeric batch id

Replace the JsonRpcId enum with Option<u64> deserialized via a small helper
(Value::as_u64), so a null or non-numeric response id maps to None instead of
adding a public type. In the batch transport, a non-numeric id now returns a
dedicated NonNumericResponseId error rather than being routed through the
range check with a fabricated id. Update the changelog.
@sainathr19

Copy link
Copy Markdown
Contributor Author

Done, pushed. Switched to Option<u64> with the deserialize_response_id helper, so no separate JsonRpcId type. Batch path now returns a dedicated NonNumericResponseId error, and the changelog is updated.

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.

JsonRpcResponse.id: u64 rejects spec-valid null / string ids and discards the server's error message

2 participants