-
Notifications
You must be signed in to change notification settings - Fork 401
feat(model): add json schema generation support for all model types #176
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
feat(model): add json schema generation support for all model types #176
Conversation
crates/rmcp/src/model/extension.rs
Outdated
| } | ||
|
|
||
| fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::schema::Schema { | ||
| generator.subschema_for::<serde_json::Value>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure about this, feel free to suggest improvements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to implement JsonSchema for extensions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I adjusted some models like so:
#[schemars(skip)]
pub extensions: Extensions,Ok with this ?
3c6ebb4 to
330dfc4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements JSON Schema support for all model types by adding the schemars::JsonSchema derive where applicable and updating tests to verify schema generation.
- Adds JSON Schema derivations on all model structs and enums via cfg_attr.
- Introduces new tests that compare generated schema against expected JSON files.
- Updates Cargo.toml to include schemars with additional features and test configuration.
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| crates/rmcp/tests/test_message_schema.rs | Adds tests for JSON schema generation of client/server JSON-RPC messages. |
| crates/rmcp/src/model/* | Updates model definitions to derive JsonSchema when the schemars feature is enabled. |
| crates/rmcp/Cargo.toml | Adds and configures the schemars dependency with extra features. |
| README.md | Documents the newly added schemars feature. |
| let schema = schema_for!(ClientJsonRpcMessage); | ||
| let schema_str = serde_json::to_string_pretty(&schema).unwrap(); | ||
| let expected = std::fs::read_to_string("tests/test_message_schema/client_json_rpc_message_schema.json").unwrap(); | ||
| assert_eq!(schema_str, expected, "Schema generation for ClientJsonRpcMessage should match expected output"); |
Copilot
AI
May 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comparing the formatted JSON schema as a string can be brittle due to potential whitespace or key ordering differences. Instead, parse both 'schema_str' and 'expected' into JSON values and compare those for a more robust test.
| assert_eq!(schema_str, expected, "Schema generation for ClientJsonRpcMessage should match expected output"); | |
| let schema_json: serde_json::Value = serde_json::from_str(&schema_str).unwrap(); | |
| let expected_json: serde_json::Value = serde_json::from_str(&expected).unwrap(); | |
| assert_eq!(schema_json, expected_json, "Schema generation for ClientJsonRpcMessage should match expected output"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's reasonable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
|
Looks good to me. Just need to fix CI and suggestions from coplilot, please. |
This commit adds JSON Schema support for all model types by implementing `schemars::JsonSchema` trait. The feature is gated behind the new `schemars` feature flag. This enables automatic schema generation for API documentation and validation purposes. Added tests to verify schema generation for client and server JSON-RPC messages.
This commit adds a manual implementation of `JsonSchema` trait for the `NumberOrString` enum to properly represent its union type nature in JSON Schema. The schema now correctly specifies that the type can be either a number or a string using the `oneOf` validation keyword.
f7c895a to
35976a6
Compare
The `Extensions` type was incorrectly included in JSON schema generation, which could lead to confusing API documentation. This commit adds `#[schemars(skip)]` attribute to all `extensions` fields in request and notification structs, and removes the manual `JsonSchema` implementation for the `Extensions` type since it's an internal implementation detail that shouldn't be exposed in the schema.
35976a6 to
e211254
Compare
|
Merged, thank you for PR! |
This commit adds JSON Schema support for all model types by implementing
schemars::JsonSchematrait. The feature is gated behind the newschemarsfeature flag. This enables automatic schema generation for API documentation and validation purposes. Added tests to verify schema generation for client and server JSON-RPC messages.Motivation and Context
This change facilitates schema-driven tooling, enabling better API validation, documentation generation, and integration with OpenAPI-based systems. It addresses the need for structured metadata across all model types.
How Has This Been Tested?
Breaking Changes
No breaking changes. The new feature is opt-in and does not affect default builds.
Types of changes
Checklist
Additional context
The
schemarsfeature is isolated for minimal impact. Feel free to suggest improvements or additional test scenarios.