Skip to content

Commit 26ecf80

Browse files
committed
test: add test for a schema contains oneof
1 parent 6b0a8ee commit 26ecf80

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

mcp-core/src/test/java/io/modelcontextprotocol/spec/McpSchemaTests.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,60 @@ void testToolWithComplexSchema() throws Exception {
831831
assertThatJson(serializedAgain).when(Option.IGNORING_ARRAY_ORDER).isEqualTo(json(serialized));
832832

833833
// Just verify the basic structure was preserved
834-
assertThat(deserializedTool.inputSchema().containsKey("defs"));
835-
// assertThat(deserializedTool.inputSchema().defs()).containsKey("Address");
834+
Map<String, Object> defs = ((Map<String, Object>) deserializedTool.inputSchema().get("$defs"));
835+
Map<String, Object> address = ((Map<String, Object>) defs.get("Address"));
836+
837+
assertThat(deserializedTool.inputSchema().containsKey("$defs")).isTrue();
838+
assertThat(address.get("type")).isEqualTo("object");
839+
}
840+
841+
@Test
842+
void testToolWithSchemaThatDoesNotHavePropertiesAtTopLevel() throws Exception {
843+
String schemaJson = """
844+
{
845+
"type": "object",
846+
"oneOf": [
847+
{
848+
"properties": {
849+
"name": {"type": "string"}
850+
},
851+
"required":["name"]
852+
},
853+
{
854+
"properties": {
855+
"id": {"type": "integer"}
856+
},
857+
"required":["id"]
858+
}
859+
]
860+
}
861+
""";
862+
863+
McpSchema.Tool tool = McpSchema.Tool.builder()
864+
.name("addressTool")
865+
.title("Handles addresses")
866+
.inputSchema(JSON_MAPPER, schemaJson)
867+
.build();
868+
869+
// Serialize the tool to a string
870+
String serialized = JSON_MAPPER.writeValueAsString(tool);
871+
872+
// Deserialize back to a Tool object
873+
McpSchema.Tool deserializedTool = JSON_MAPPER.readValue(serialized, McpSchema.Tool.class);
874+
875+
// Serialize again and compare with first serialization
876+
String serializedAgain = JSON_MAPPER.writeValueAsString(deserializedTool);
877+
878+
// The two serialized strings should be the same
879+
assertThatJson(serializedAgain).when(Option.IGNORING_ARRAY_ORDER).isEqualTo(json(serialized));
880+
881+
Map<String, Object> nameInput = Map.of("properties", Map.of("name", Map.of("type", "string")), "required",
882+
List.of("name"));
883+
Map<String, Object> idInput = Map.of("properties", Map.of("id", Map.of("type", "integer")), "required",
884+
List.of("id"));
885+
886+
List<Object> oneOf = (List<Object>) deserializedTool.inputSchema().get("oneOf");
887+
assertThat(oneOf).isEqualTo(List.of(nameInput, idInput));
836888
}
837889

838890
@Test

0 commit comments

Comments
 (0)