chore(deps): update schemars requirement from 0.8 to 1.0 (#258)

* chore(deps): update schemars requirement from 0.8 to 0.9

* fix: adapt the new openApi structure

* fix : fix the shemar deps in example
This commit is contained in:
jokemanfire 2025-07-11 11:15:45 +08:00 committed by GitHub
parent e615300fea
commit 3196c95f1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 1102 additions and 988 deletions

View file

@ -28,7 +28,7 @@ paste = { version = "1", optional = true }
oauth2 = { version = "5.0", optional = true }
# for auto generate schema
schemars = { version = "0.8", optional = true, features = ["chrono"] }
schemars = { version = "1.0", optional = true, features = ["chrono04"] }
# for image encoding
base64 = { version = "0.22", optional = true }
@ -138,7 +138,7 @@ schemars = ["dep:schemars"]
[dev-dependencies]
tokio = { version = "1", features = ["full"] }
schemars = { version = "0.8" }
schemars = { version = "1.0", features = ["chrono04"] }
anyhow = "1.0"
tracing-subscriber = { version = "0.3", features = [

View file

@ -3,7 +3,7 @@ use std::{
};
use futures::future::{BoxFuture, FutureExt};
use schemars::JsonSchema;
use schemars::{JsonSchema, transform::AddNullable};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use tokio_util::sync::CancellationToken;
@ -11,17 +11,16 @@ pub use super::router::tool::{ToolRoute, ToolRouter};
use crate::{
RoleServer,
model::{CallToolRequestParam, CallToolResult, IntoContents, JsonObject},
schemars::generate::SchemaSettings,
service::RequestContext,
};
/// A shortcut for generating a JSON schema for a type.
pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
// explicitly to align json schema version to official specifications.
// https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.json
let mut settings = schemars::r#gen::SchemaSettings::draft07();
settings.option_nullable = true;
settings.option_add_null_type = false;
settings.visitors = Vec::default();
// TODO: update to 2020-12 waiting for the mcp spec update
let mut settings = SchemaSettings::draft07();
settings.transforms = vec![Box::new(AddNullable::default())];
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<T>();
let object = serde_json::to_value(schema).expect("failed to serialize schema");
@ -185,11 +184,11 @@ pub type DynCallToolHandler<S> = dyn for<'s> Fn(ToolCallContext<'s, S>) -> BoxFu
pub struct Parameters<P>(pub P);
impl<P: JsonSchema> JsonSchema for Parameters<P> {
fn schema_name() -> String {
fn schema_name() -> Cow<'static, str> {
P::schema_name()
}
fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
P::json_schema(generator)
}
}

View file

@ -98,18 +98,19 @@ macro_rules! const_string {
#[cfg(feature = "schemars")]
impl schemars::JsonSchema for $name {
fn schema_name() -> String {
stringify!($name).to_string()
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed(stringify!($name))
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
// Create a schema for a constant value of type String
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
format: Some("const".to_string()),
const_value: Some(serde_json::Value::String($value.into())),
..Default::default()
})
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
use serde_json::{Map, json};
let mut schema_map = Map::new();
schema_map.insert("type".to_string(), json!("string"));
schema_map.insert("format".to_string(), json!("const"));
schema_map.insert("const".to_string(), json!($value));
schemars::Schema::from(schema_map)
}
}
};
@ -233,27 +234,23 @@ impl<'de> Deserialize<'de> for NumberOrString {
#[cfg(feature = "schemars")]
impl schemars::JsonSchema for NumberOrString {
fn schema_name() -> String {
"NumberOrString".to_string()
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("NumberOrString")
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
one_of: Some(vec![
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::Number.into()),
..Default::default()
}),
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
..Default::default()
}),
]),
..Default::default()
})),
..Default::default()
})
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
use serde_json::{Map, json};
let mut number_schema = Map::new();
number_schema.insert("type".to_string(), json!("number"));
let mut string_schema = Map::new();
string_schema.insert("type".to_string(), json!("string"));
let mut schema_map = Map::new();
schema_map.insert("oneOf".to_string(), json!([number_schema, string_schema]));
schemars::Schema::from(schema_map)
}
}

View file

@ -1,6 +1,6 @@
mod tests {
use rmcp::model::{ClientJsonRpcMessage, ServerJsonRpcMessage};
use schemars::schema_for;
use schemars::generate::SchemaSettings;
fn compare_schemas(name: &str, actual: &str, expected_file: &str) {
let expected = match std::fs::read_to_string(expected_file) {
@ -48,7 +48,10 @@ mod tests {
#[test]
fn test_client_json_rpc_message_schema() {
let schema = schema_for!(ClientJsonRpcMessage);
let settings = SchemaSettings::draft07();
let schema = settings
.into_generator()
.into_root_schema_for::<ClientJsonRpcMessage>();
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
compare_schemas(
@ -60,7 +63,10 @@ mod tests {
#[test]
fn test_server_json_rpc_message_schema() {
let schema = schema_for!(ServerJsonRpcMessage);
let settings = SchemaSettings::draft07();
let schema = settings
.into_generator()
.into_root_schema_for::<ServerJsonRpcMessage>();
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
compare_schemas(

View file

@ -33,7 +33,7 @@ tracing-subscriber = { version = "0.3", features = [
futures = "0.3"
rand = { version = "0.9", features = ["std"] }
axum = { version = "0.8", features = ["macros"] }
schemars = { version = "0.8", optional = true }
schemars = { version = "1.0", optional = true }
reqwest = { version = "0.12", features = ["json"] }
chrono = "0.4"
uuid = { version = "1.6", features = ["v4", "serde"] }

View file

@ -37,7 +37,7 @@ tracing-subscriber = { version = "0.3", features = [
] }
futures = "0.3"
rand = { version = "0.9" }
schemars = { version = "0.8", optional = true }
schemars = { version = "1.0", optional = true }
hyper = { version = "1", features = ["client", "server", "http1"] }
hyper-util = { version = "0.1", features = ["tokio"] }
tokio-tungstenite = "0.27.0"