Skip to content

Support validation for method fields in *Request #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zhongyi51
Copy link

Closes: #75

The Problem

As identified in issue #75, the individual *Request structs (e.g., PingRequest, ListResourcesRequest) do not currently validate that their method field matches the constant value defined in the JSON schema. This creates ambiguity. For example, a JSON payload like:

{
  "method": "wrong/method",
  "params": { /* ... valid params for ListResourcesRequest ... */ }
}

could be incorrectly deserialized as a ListResourcesRequest. The deserialization process should fail fast if the method name is incorrect for the target struct.

The Solution

This pull request proposes a robust pattern to validate the method field by leveraging Serde's #[serde(deserialize_with = "...")]:

  1. Deserializes the incoming value as a string.
  2. Compares it against the expected constant value for that specific request type.

Note for Maintainers

⚠️ This pull request should NOT be merged directly. It serves as a working example for the code generator.

@zhongyi51 zhongyi51 changed the title Support validation for subtypes of *Request enums (ServerRequest as an example) Support validation for method field in *Request Jul 19, 2025
@zhongyi51 zhongyi51 changed the title Support validation for method field in *Request Support validation for method fields in *Request Jul 19, 2025
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.

1 participant