feat(model): add json schema generation support for all model types (#176)
* feat(model): add json schema generation support for all model types 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. * fix(model): add manual json schema implementation for `NumberOrString` 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. * fix(model): skip extensions field in json schema generation 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.
This commit is contained in:
parent
5d92061ece
commit
e9a5ae9901
3 changed files with 2790 additions and 1 deletions
40
crates/rmcp/tests/test_message_schema.rs
Normal file
40
crates/rmcp/tests/test_message_schema.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
mod tests {
|
||||
use rmcp::model::{ClientJsonRpcMessage, ServerJsonRpcMessage};
|
||||
use schemars::schema_for;
|
||||
|
||||
#[test]
|
||||
fn test_client_json_rpc_message_schema() {
|
||||
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();
|
||||
|
||||
// Parse both strings to JSON values for more robust comparison
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_server_json_rpc_message_schema() {
|
||||
let schema = schema_for!(ServerJsonRpcMessage);
|
||||
let schema_str = serde_json::to_string_pretty(&schema).unwrap();
|
||||
let expected = std::fs::read_to_string(
|
||||
"tests/test_message_schema/server_json_rpc_message_schema.json",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Parse both strings to JSON values for more robust comparison
|
||||
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 ServerJsonRpcMessage should match expected output"
|
||||
);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue