Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324"""Test handling of direct array schemas in request bodies (FastAPI list parameters)."""
from fastmcp.utilities.openapi.models import ( HTTPRoute, ParameterInfo, RequestBodyInfo,)from fastmcp.utilities.openapi.schemas import ( _combine_schemas_and_map_params,)
class TestDirectArraySchemas: """Test handling of direct array schemas like those generated by FastAPI for list[str] parameters."""
def test_simple_direct_array_schema(self): """Test route with direct array request body schema.""" route = HTTPRoute( path="/simple_list", method="POST", operation_id="simple_list_tool", summary="Simple List Tool", description="A simple tool that takes a list of strings.", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "items": {"type": "string"}, "type": "array", "title": "Values", } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should create a single parameter from the title assert combined_schema["type"] == "object" assert "values" in combined_schema["properties"] assert "values" in combined_schema["required"]
# Check the array schema is preserved values_schema = combined_schema["properties"]["values"] assert values_schema["type"] == "array" assert values_schema["items"]["type"] == "string" assert values_schema["title"] == "Values"
# Check parameter mapping assert "values" in param_map assert param_map["values"]["location"] == "body" assert param_map["values"]["openapi_name"] == "values"
def test_int_array_schema(self): """Test route with integer array request body schema.""" route = HTTPRoute( path="/int_list", method="POST", operation_id="int_list_tool", summary="Integer List Tool", description="A simple tool that takes a list of integers.", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "items": {"type": "integer"}, "type": "array", "title": "Numbers", } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should create a single parameter from the title assert combined_schema["type"] == "object" assert "numbers" in combined_schema["properties"] assert "numbers" in combined_schema["required"]
# Check the array schema is preserved numbers_schema = combined_schema["properties"]["numbers"] assert numbers_schema["type"] == "array" assert numbers_schema["items"]["type"] == "integer" assert numbers_schema["title"] == "Numbers"
def test_mixed_params_with_direct_array(self): """Test route with both URL parameters and direct array request body.""" route = HTTPRoute( path="/mixed_params", method="POST", operation_id="mixed_params_tool", summary="Mixed Params Tool", description="A tool with both list and simple parameters.", parameters=[ ParameterInfo( name="prefix", location="query", required=True, schema={"type": "string", "title": "Prefix"}, ) ], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "type": "array", "items": {"type": "string"}, "title": "Values", } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should have both parameters assert combined_schema["type"] == "object" assert "prefix" in combined_schema["properties"] # From query param assert "values" in combined_schema["properties"] # From body array assert "prefix" in combined_schema["required"] assert "values" in combined_schema["required"]
# Check query parameter prefix_schema = combined_schema["properties"]["prefix"] assert prefix_schema["type"] == "string"
# Check body array parameter values_schema = combined_schema["properties"]["values"] assert values_schema["type"] == "array" assert values_schema["items"]["type"] == "string"
# Check parameter mappings assert param_map["prefix"]["location"] == "query" assert param_map["values"]["location"] == "body"
def test_direct_array_no_title(self): """Test direct array schema without title.""" route = HTTPRoute( path="/no_title", method="POST", operation_id="no_title_tool", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "type": "array", "items": {"type": "string"}, # No title } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should fall back to "body" (default title falls back) assert combined_schema["type"] == "object" assert "body" in combined_schema["properties"] assert "body" in combined_schema["required"]
# Check parameter mapping assert param_map["body"]["location"] == "body" assert param_map["body"]["openapi_name"] == "body"
def test_direct_primitive_schema(self): """Test direct primitive (non-array) request body schema.""" route = HTTPRoute( path="/primitive", method="POST", operation_id="primitive_tool", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": {"type": "string", "title": "Message"} }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should create a single parameter from the title assert combined_schema["type"] == "object" assert "message" in combined_schema["properties"] assert "message" in combined_schema["required"]
# Check the primitive schema is preserved message_schema = combined_schema["properties"]["message"] assert message_schema["type"] == "string" assert message_schema["title"] == "Message"
def test_direct_array_optional(self): """Test direct array schema that's not required.""" route = HTTPRoute( path="/optional_list", method="POST", operation_id="optional_list_tool", parameters=[], request_body=RequestBodyInfo( required=False, # Not required content_schema={ "application/json": { "type": "array", "items": {"type": "string"}, "title": "OptionalValues", } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should create parameter but not mark as required assert combined_schema["type"] == "object" assert "optionalvalues" in combined_schema["properties"] assert "optionalvalues" not in combined_schema["required"]
def test_direct_array_title_sanitization(self): """Test that titles with special characters are sanitized.""" route = HTTPRoute( path="/special_chars", method="POST", operation_id="special_chars_tool", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "type": "array", "items": {"type": "string"}, "title": "Special-Chars & Spaces!", } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should sanitize the title to a valid parameter name (& becomes multiple underscores) assert combined_schema["type"] == "object" assert "special_chars___spaces_" in combined_schema["properties"]
def test_direct_array_title_starting_with_number(self): """Test that titles starting with numbers are handled.""" route = HTTPRoute( path="/number_start", method="POST", operation_id="number_start_tool", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "type": "array", "items": {"type": "string"}, "title": "123Values", } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should fall back to "body_data" since "123values" starts with a number assert combined_schema["type"] == "object" assert "body_data" in combined_schema["properties"]
def test_preserve_existing_behavior_object_body(self): """Test that existing object-based request bodies still work.""" route = HTTPRoute( path="/object_body", method="POST", operation_id="object_body_tool", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"}, }, "required": ["name"], } }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should handle object bodies as before assert combined_schema["type"] == "object" assert "name" in combined_schema["properties"] assert "age" in combined_schema["properties"] assert "name" in combined_schema["required"] assert "age" not in combined_schema["required"]
def test_preserve_existing_behavior_ref_body(self): """Test that $ref-based request bodies still work.""" route = HTTPRoute( path="/ref_body", method="POST", operation_id="ref_body_tool", parameters=[], request_body=RequestBodyInfo( required=True, content_schema={ "application/json": {"$ref": "#/components/schemas/UserCreate"} }, ), )
combined_schema, param_map = _combine_schemas_and_map_params(route)
# Should handle $ref bodies as before (refs get converted to $defs) assert combined_schema["type"] == "object" assert "body" in combined_schema["properties"] assert combined_schema["properties"]["body"]["$ref"] == "#/$defs/UserCreate"