Fix: Correctly parse JSON-RPC batch requests#47
Merged
Conversation
The previous implementation incorrectly handled JSON-RPC batch requests, leading to parsing errors when an array of requests was received. This commit addresses the issue by: 1. Modifying `StdioTransport::receive()` to ensure that when a batch request (an array of JSON objects) is detected, it's correctly identified. The raw JSON string is then passed to `JsonRpcMessage`. 2. Modifying `JsonRpcMessage::fromJsonArray()` to first decode the incoming JSON string into an array of `stdClass` objects. It then iterates through this array, calling a new method `fromJsonObject()` for each item. This avoids inefficient re-encoding/decoding. 3. Introducing a new public static method `JsonRpcMessage::fromJsonObject()`. This method takes a `stdClass` object (a single decoded JSON-RPC message) and performs the same validation and object construction logic as the original `fromJson()` method, but operates directly on the object's properties. This includes casting nested structures like `params`, `result`, and `error` to arrays where appropriate to maintain type consistency. 4. Adding comprehensive unit tests for the modified `StdioTransport::receive()` and for the new and modified methods in `JsonRpcMessage`. These tests cover various scenarios, including valid and invalid batch requests, requests, notifications, and responses. These changes ensure that the server can now correctly parse and process JSON-RPC batch requests according to the specification, resolving the "Expected object, received array" ZodError. All linters and unit tests pass with these changes.
🧪 Test Results SummaryPHPUnit Tests✅ All tests passed! Details: OK (260 tests, 1037 assertions)
Code Quality
📁 Detailed Reports
This comment will update automatically when you push new commits. Code Coverage
|
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The previous implementation incorrectly handled JSON-RPC batch requests, leading to parsing errors when an array of requests was received.
This commit addresses the issue by:
Modifying
StdioTransport::receive()to ensure that when a batch request (an array of JSON objects) is detected, it's correctly identified. The raw JSON string is then passed toJsonRpcMessage.Modifying
JsonRpcMessage::fromJsonArray()to first decode the incoming JSON string into an array ofstdClassobjects. It then iterates through this array, calling a new methodfromJsonObject()for each item. This avoids inefficient re-encoding/decoding.Introducing a new public static method
JsonRpcMessage::fromJsonObject(). This method takes astdClassobject (a single decoded JSON-RPC message) and performs the same validation and object construction logic as the originalfromJson()method, but operates directly on the object's properties. This includes casting nested structures likeparams,result, anderrorto arrays where appropriate to maintain type consistency.Adding comprehensive unit tests for the modified
StdioTransport::receive()and for the new and modified methods inJsonRpcMessage. These tests cover various scenarios, including valid and invalid batch requests, requests, notifications, and responses.These changes ensure that the server can now correctly parse and process JSON-RPC batch requests according to the specification, resolving the "Expected object, received array" ZodError. All linters and unit tests pass with these changes.