Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Send the whole body when a body $ref with siblings cannot be flattened (#5893)

Asem ahmedcad607472858
diff --git a/fastmcp_slim/fastmcp/utilities/openapi/schemas.py b/fastmcp_slim/fastmcp/utilities/openapi/schemas.py
index f1dc29af8..b0da4c473 100644
--- a/fastmcp_slim/fastmcp/utilities/openapi/schemas.py
+++ b/fastmcp_slim/fastmcp/utilities/openapi/schemas.py
@@ -625,14 +625,20 @@ def _combine_schemas_and_map_params(
# A kept $ref is paired with its siblings through allOf: resolving a
# $ref that has siblings lets the siblings overwrite the target's keywords.
+ # The allOf is built from a copy: with convert_refs=False body_schema is
+ # the route's stored schema, and the director reads its $ref at call time
+ # to send the whole body.
if not ref_is_object:
- ref = body_schema.pop("$ref")
- if not body_schema.get("properties"):
- body_schema.pop("properties", None)
+ ref = body_schema["$ref"]
+ siblings = {
+ keyword: value
+ for keyword, value in body_schema.items()
+ if keyword != "$ref"
+ }
+ if not siblings.get("properties"):
+ siblings.pop("properties", None)
body_schema = (
- {"allOf": [{"$ref": ref}, body_schema]}
- if body_schema
- else {"$ref": ref}
+ {"allOf": [{"$ref": ref}, siblings]} if siblings else {"$ref": ref}
)
# Merge discriminated subtype fields in as optional. The discriminator
diff --git a/tests/server/providers/openapi/test_body_ref_siblings.py b/tests/server/providers/openapi/test_body_ref_siblings.py
index ed54b8289..19303c96a 100644
--- a/tests/server/providers/openapi/test_body_ref_siblings.py
+++ b/tests/server/providers/openapi/test_body_ref_siblings.py
@@ -277,12 +277,12 @@ async def test_ref_to_one_of_target_keeps_the_ref():
],
}
spec = build_spec(SIBLING_BODY, {"Base": base})
- schema, _ = await call_make_thing(
- spec, {"body": {"name": "n", "local": "l", "a": "x"}}
- )
+ payload = {"name": "n", "local": "l", "a": "x"}
+ schema, sent = await call_make_thing(spec, {"body": payload})
dumped = json.dumps(schema)
assert '"a"' in dumped
assert '"name"' in dumped
+ assert sent == payload
async def test_ref_to_any_of_target_keeps_the_ref():
@@ -291,19 +291,34 @@ async def test_ref_to_any_of_target_keeps_the_ref():
"anyOf": [{"required": ["a"]}, {"required": ["b"]}],
}
spec = build_spec(SIBLING_BODY, {"Base": base})
- schema, _ = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
+ schema, sent = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
assert "anyOf" in json.dumps(schema)
+ assert sent == {"name": "n", "local": "l"}
async def test_ref_to_pattern_properties_target_keeps_the_ref():
base = {**BASE, "patternProperties": {"^x-": {"type": "string"}}}
spec = build_spec(SIBLING_BODY, {"Base": base})
- schema, _ = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
+ schema, sent = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
assert "patternProperties" in json.dumps(schema)
+ assert sent == {"name": "n", "local": "l"}
async def test_ref_to_additional_properties_target_keeps_the_ref():
base = {**BASE, "additionalProperties": False}
spec = build_spec(SIBLING_BODY, {"Base": base})
- schema, _ = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
+ schema, sent = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
+ assert "additionalProperties" in json.dumps(schema)
+ assert sent == {"name": "n", "local": "l"}
+
+
+async def test_ref_with_all_of_to_additional_properties_target_keeps_the_ref():
+ body = {
+ "$ref": "#/components/schemas/Base",
+ "allOf": [{"properties": {"local": {"type": "string"}}}],
+ }
+ base = {**BASE, "additionalProperties": False}
+ spec = build_spec(body, {"Base": base})
+ schema, sent = await call_make_thing(spec, {"body": {"name": "n", "local": "l"}})
assert "additionalProperties" in json.dumps(schema)
+ assert sent == {"name": "n", "local": "l"}