Spaces:
Running
Running
File size: 11,908 Bytes
8c5bdf2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | """Tests to ensure new OpenAPI implementation matches legacy behavior exactly."""
import pytest
from fastmcp.experimental.utilities.openapi.models import (
HTTPRoute,
ParameterInfo,
RequestBodyInfo,
)
from fastmcp.experimental.utilities.openapi.schemas import (
_combine_schemas_and_map_params,
)
from fastmcp.utilities.openapi import HTTPRoute as LegacyHTTPRoute
from fastmcp.utilities.openapi import ParameterInfo as LegacyParameterInfo
from fastmcp.utilities.openapi import RequestBodyInfo as LegacyRequestBodyInfo
from fastmcp.utilities.openapi import _combine_schemas as legacy_combine_schemas
class TestLegacyCompatibility:
"""Test that new implementation produces identical schemas to legacy."""
def test_optional_parameter_nullable_behavior(self):
"""Test that optional parameters get anyOf with null, required don't."""
# Legacy route
legacy_route = LegacyHTTPRoute(
method="GET",
path="/test",
parameters=[
LegacyParameterInfo(
name="required_param",
location="query",
required=True,
schema={"type": "string"},
),
LegacyParameterInfo(
name="optional_param",
location="query",
required=False,
schema={"type": "string"},
),
],
request_body=None,
responses={},
summary="Test endpoint",
schema_definitions={},
)
# New route (equivalent)
new_route = HTTPRoute(
method="GET",
path="/test",
operation_id="test_op",
parameters=[
ParameterInfo(
name="required_param",
location="query",
required=True,
schema={"type": "string"},
),
ParameterInfo(
name="optional_param",
location="query",
required=False,
schema={"type": "string"},
),
],
)
# Generate schemas
legacy_schema = legacy_combine_schemas(legacy_route)
new_schema, _ = _combine_schemas_and_map_params(new_route)
# Required parameter should have simple type
assert legacy_schema["properties"]["required_param"]["type"] == "string"
assert new_schema["properties"]["required_param"]["type"] == "string"
assert "anyOf" not in legacy_schema["properties"]["required_param"]
assert "anyOf" not in new_schema["properties"]["required_param"]
# Both implementations now correctly preserve original schema
# Neither should make optional parameters nullable - they can simply be omitted
assert "anyOf" not in legacy_schema["properties"]["optional_param"]
assert "anyOf" not in new_schema["properties"]["optional_param"]
assert legacy_schema["properties"]["optional_param"]["type"] == "string"
assert new_schema["properties"]["optional_param"]["type"] == "string"
# Required lists should match
assert set(legacy_schema["required"]) == set(new_schema["required"])
assert "required_param" in legacy_schema["required"]
assert "optional_param" not in legacy_schema["required"]
def test_parameter_collision_handling(self):
"""Test that parameter collisions are handled identically."""
# Legacy route with collision (path param 'id' and body property 'id')
legacy_route = LegacyHTTPRoute(
method="PUT",
path="/users/{id}",
parameters=[
LegacyParameterInfo(
name="id",
location="path",
required=True,
schema={"type": "integer"},
)
],
request_body=LegacyRequestBodyInfo(
required=True,
content_schema={
"application/json": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
},
"required": ["name"],
}
},
),
responses={},
summary="Update user",
schema_definitions={},
)
# New route (equivalent)
new_route = HTTPRoute(
method="PUT",
path="/users/{id}",
operation_id="update_user",
parameters=[
ParameterInfo(
name="id",
location="path",
required=True,
schema={"type": "integer"},
)
],
request_body=RequestBodyInfo(
required=True,
content_schema={
"application/json": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
},
"required": ["name"],
}
},
),
)
# Generate schemas
legacy_schema = legacy_combine_schemas(legacy_route)
new_schema, param_map = _combine_schemas_and_map_params(new_route)
# Should have path parameter with suffix
assert "id__path" in legacy_schema["properties"]
assert "id__path" in new_schema["properties"]
# Should have body parameter without suffix
assert "id" in legacy_schema["properties"]
assert "id" in new_schema["properties"]
# Should have name parameter from body
assert "name" in legacy_schema["properties"]
assert "name" in new_schema["properties"]
# Required should include path param (suffixed) and required body params
legacy_required = set(legacy_schema["required"])
new_required = set(new_schema["required"])
assert "id__path" in legacy_required
assert "id__path" in new_required
assert "name" in legacy_required # required in body
assert "name" in new_required
# Parameter map should correctly map suffixed parameter
assert param_map["id__path"]["location"] == "path"
assert param_map["id__path"]["openapi_name"] == "id"
assert param_map["id"]["location"] == "body"
assert param_map["name"]["location"] == "body"
@pytest.mark.parametrize(
"param_type",
[
{"type": "integer"},
{"type": "number"},
{"type": "boolean"},
{"type": "array", "items": {"type": "string"}},
{"type": "object", "properties": {"name": {"type": "string"}}},
],
)
def test_nullable_behavior_different_types(self, param_type):
"""Test nullable behavior works for all parameter types."""
# Legacy route
legacy_route = LegacyHTTPRoute(
method="GET",
path="/test",
parameters=[
LegacyParameterInfo(
name="optional_param",
location="query",
required=False,
schema=param_type,
)
],
request_body=None,
responses={},
summary="Test endpoint",
schema_definitions={},
)
# New route
new_route = HTTPRoute(
method="GET",
path="/test",
operation_id="test_op",
parameters=[
ParameterInfo(
name="optional_param",
location="query",
required=False,
schema=param_type,
)
],
)
# Generate schemas
legacy_schema = legacy_combine_schemas(legacy_route)
new_schema, _ = _combine_schemas_and_map_params(new_route)
# Both implementations now correctly preserve original schema
legacy_param = legacy_schema["properties"]["optional_param"]
new_param = new_schema["properties"]["optional_param"]
# Both should preserve original schema without making it nullable
assert "anyOf" not in legacy_param
assert "anyOf" not in new_param
# Both should match the original parameter schema (plus description in legacy)
for key, value in param_type.items():
assert legacy_param[key] == value
assert new_param[key] == value
def test_no_parameters_no_body(self):
"""Test schema generation when there are no parameters or body."""
# Legacy route
legacy_route = LegacyHTTPRoute(
method="GET",
path="/health",
parameters=[],
request_body=None,
responses={},
summary="Health check",
schema_definitions={},
)
# New route
new_route = HTTPRoute(
method="GET",
path="/health",
operation_id="health_check",
)
# Generate schemas
legacy_schema = legacy_combine_schemas(legacy_route)
new_schema, param_map = _combine_schemas_and_map_params(new_route)
# Both should have empty object schemas
assert legacy_schema["type"] == "object"
assert new_schema["type"] == "object"
assert legacy_schema["properties"] == {}
assert new_schema["properties"] == {}
assert legacy_schema["required"] == []
assert new_schema["required"] == []
assert param_map == {}
def test_body_only_no_parameters(self):
"""Test schema generation with only request body, no parameters."""
body_schema = {
"application/json": {
"type": "object",
"properties": {
"title": {"type": "string"},
"description": {"type": "string"},
},
"required": ["title"],
}
}
# Legacy route
legacy_route = LegacyHTTPRoute(
method="POST",
path="/items",
parameters=[],
request_body=LegacyRequestBodyInfo(
required=True,
content_schema=body_schema,
),
responses={},
summary="Create item",
schema_definitions={},
)
# New route
new_route = HTTPRoute(
method="POST",
path="/items",
operation_id="create_item",
request_body=RequestBodyInfo(
required=True,
content_schema=body_schema,
),
)
# Generate schemas
legacy_schema = legacy_combine_schemas(legacy_route)
new_schema, param_map = _combine_schemas_and_map_params(new_route)
# Should have body properties
assert "title" in legacy_schema["properties"]
assert "description" in legacy_schema["properties"]
assert "title" in new_schema["properties"]
assert "description" in new_schema["properties"]
# Required should match body requirements
assert "title" in legacy_schema["required"]
assert "title" in new_schema["required"]
assert "description" not in legacy_schema["required"]
assert "description" not in new_schema["required"]
# Parameter map should map body properties
assert param_map["title"]["location"] == "body"
assert param_map["description"]["location"] == "body"
|