Skip to content

Commit d419550

Browse files
committed
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.
1 parent 1e2cf8b commit d419550

File tree

3 files changed

+30
-45
lines changed

3 files changed

+30
-45
lines changed

crates/rmcp/src/model.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ impl<'de> Deserialize<'de> for ProtocolVersion {
150150
}
151151

152152
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
153-
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
154153
pub enum NumberOrString {
155154
Number(u32),
156155
String(Arc<str>),
@@ -203,6 +202,32 @@ impl<'de> Deserialize<'de> for NumberOrString {
203202
}
204203
}
205204

205+
#[cfg(feature = "schemars")]
206+
impl schemars::JsonSchema for NumberOrString {
207+
fn schema_name() -> String {
208+
"NumberOrString".to_string()
209+
}
210+
211+
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
212+
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
213+
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
214+
one_of: Some(vec![
215+
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
216+
instance_type: Some(schemars::schema::InstanceType::Number.into()),
217+
..Default::default()
218+
}),
219+
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
220+
instance_type: Some(schemars::schema::InstanceType::String.into()),
221+
..Default::default()
222+
}),
223+
]),
224+
..Default::default()
225+
})),
226+
..Default::default()
227+
})
228+
}
229+
}
230+
206231
pub type RequestId = NumberOrString;
207232

208233
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, Eq)]

crates/rmcp/tests/test_message_schema/client_json_rpc_message_schema.json

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -691,30 +691,10 @@
691691
"NumberOrString": {
692692
"oneOf": [
693693
{
694-
"type": "object",
695-
"required": [
696-
"Number"
697-
],
698-
"properties": {
699-
"Number": {
700-
"type": "integer",
701-
"format": "uint32",
702-
"minimum": 0.0
703-
}
704-
},
705-
"additionalProperties": false
694+
"type": "number"
706695
},
707696
{
708-
"type": "object",
709-
"required": [
710-
"String"
711-
],
712-
"properties": {
713-
"String": {
714-
"type": "string"
715-
}
716-
},
717-
"additionalProperties": false
697+
"type": "string"
718698
}
719699
]
720700
},

crates/rmcp/tests/test_message_schema/server_json_rpc_message_schema.json

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -966,30 +966,10 @@
966966
"NumberOrString": {
967967
"oneOf": [
968968
{
969-
"type": "object",
970-
"required": [
971-
"Number"
972-
],
973-
"properties": {
974-
"Number": {
975-
"type": "integer",
976-
"format": "uint32",
977-
"minimum": 0.0
978-
}
979-
},
980-
"additionalProperties": false
969+
"type": "number"
981970
},
982971
{
983-
"type": "object",
984-
"required": [
985-
"String"
986-
],
987-
"properties": {
988-
"String": {
989-
"type": "string"
990-
}
991-
},
992-
"additionalProperties": false
972+
"type": "string"
993973
}
994974
]
995975
},

0 commit comments

Comments
 (0)