Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
a4d699c
1
Parent(s): 5057c7c
Fix test to properly expect ValidationError with pytest.raises
Browse files
tests/server/openapi/test_openapi_compatibility.py
CHANGED
|
@@ -2,11 +2,13 @@ import json
|
|
| 2 |
|
| 3 |
import httpx
|
| 4 |
import pytest
|
|
|
|
| 5 |
from pydantic.networks import AnyUrl
|
| 6 |
|
| 7 |
from fastmcp import FastMCP
|
| 8 |
from fastmcp.client import Client
|
| 9 |
from fastmcp.server.openapi import FastMCPOpenAPI
|
|
|
|
| 10 |
|
| 11 |
from .conftest import GET_ROUTE_MAPS
|
| 12 |
|
|
@@ -80,8 +82,6 @@ class TestOpenAPI30Compatibility:
|
|
| 80 |
],
|
| 81 |
)
|
| 82 |
elif request.url.path == "/products" and request.method == "POST":
|
| 83 |
-
import json
|
| 84 |
-
|
| 85 |
data = json.loads(request.content)
|
| 86 |
return httpx.Response(
|
| 87 |
201, json={"id": "p3", "name": data["name"], "price": data["price"]}
|
|
@@ -257,8 +257,6 @@ class TestOpenAPI31Compatibility:
|
|
| 257 |
],
|
| 258 |
)
|
| 259 |
elif request.url.path == "/orders" and request.method == "POST":
|
| 260 |
-
import json
|
| 261 |
-
|
| 262 |
data = json.loads(request.content)
|
| 263 |
return httpx.Response(
|
| 264 |
201,
|
|
@@ -418,8 +416,6 @@ class TestOpenAPIVersionDifferences:
|
|
| 418 |
}
|
| 419 |
|
| 420 |
# This should not raise a ValidationError
|
| 421 |
-
from fastmcp.utilities.openapi import parse_openapi_to_http_routes
|
| 422 |
-
|
| 423 |
routes = parse_openapi_to_http_routes(spec_with_exclusive_max)
|
| 424 |
assert len(routes) == 1
|
| 425 |
assert routes[0].operation_id == "createLoan"
|
|
@@ -467,8 +463,6 @@ class TestOpenAPIVersionDifferences:
|
|
| 467 |
}
|
| 468 |
|
| 469 |
# This should not raise a ValidationError
|
| 470 |
-
from fastmcp.utilities.openapi import parse_openapi_to_http_routes
|
| 471 |
-
|
| 472 |
routes = parse_openapi_to_http_routes(spec_with_exclusive_max)
|
| 473 |
assert len(routes) == 1
|
| 474 |
assert routes[0].operation_id == "createLoan"
|
|
@@ -508,8 +502,6 @@ class TestOpenAPIVersionDifferences:
|
|
| 508 |
}
|
| 509 |
|
| 510 |
# This should not raise a ValidationError
|
| 511 |
-
from fastmcp.utilities.openapi import parse_openapi_to_http_routes
|
| 512 |
-
|
| 513 |
routes = parse_openapi_to_http_routes(spec_with_nullable)
|
| 514 |
assert len(routes) == 1
|
| 515 |
assert routes[0].operation_id == "createUser"
|
|
@@ -551,8 +543,6 @@ class TestOpenAPIVersionDifferences:
|
|
| 551 |
}
|
| 552 |
|
| 553 |
# This should not raise a ValidationError
|
| 554 |
-
from fastmcp.utilities.openapi import parse_openapi_to_http_routes
|
| 555 |
-
|
| 556 |
routes = parse_openapi_to_http_routes(spec_with_type_array)
|
| 557 |
assert len(routes) == 1
|
| 558 |
assert routes[0].operation_id == "createUser"
|
|
@@ -620,15 +610,8 @@ class TestOpenAPIVersionDifferences:
|
|
| 620 |
}
|
| 621 |
|
| 622 |
# This should reproduce the validation error from GitHub issue #1021
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
try:
|
| 626 |
-
routes = parse_openapi_to_http_routes(spec_with_defs)
|
| 627 |
-
assert len(routes) == 1
|
| 628 |
-
assert routes[0].operation_id == "createComplexLoan"
|
| 629 |
-
except ValueError as e:
|
| 630 |
-
# If this fails, it's reproducing the issue
|
| 631 |
-
pytest.fail(f"OpenAPI 3.0 validation failed: {e}")
|
| 632 |
|
| 633 |
def test_openapi_30_edge_case_with_multiple_exclusive_constraints(self):
|
| 634 |
"""Test edge case with multiple exclusive constraints that might trigger validation issues."""
|
|
@@ -673,8 +656,6 @@ class TestOpenAPIVersionDifferences:
|
|
| 673 |
}
|
| 674 |
|
| 675 |
# This might trigger validation issues with multiple exclusive constraints
|
| 676 |
-
from fastmcp.utilities.openapi import parse_openapi_to_http_routes
|
| 677 |
-
|
| 678 |
routes = parse_openapi_to_http_routes(spec_edge_case)
|
| 679 |
assert len(routes) == 1
|
| 680 |
assert routes[0].operation_id == "validateData"
|
|
|
|
| 2 |
|
| 3 |
import httpx
|
| 4 |
import pytest
|
| 5 |
+
from pydantic import ValidationError
|
| 6 |
from pydantic.networks import AnyUrl
|
| 7 |
|
| 8 |
from fastmcp import FastMCP
|
| 9 |
from fastmcp.client import Client
|
| 10 |
from fastmcp.server.openapi import FastMCPOpenAPI
|
| 11 |
+
from fastmcp.utilities.openapi import parse_openapi_to_http_routes
|
| 12 |
|
| 13 |
from .conftest import GET_ROUTE_MAPS
|
| 14 |
|
|
|
|
| 82 |
],
|
| 83 |
)
|
| 84 |
elif request.url.path == "/products" and request.method == "POST":
|
|
|
|
|
|
|
| 85 |
data = json.loads(request.content)
|
| 86 |
return httpx.Response(
|
| 87 |
201, json={"id": "p3", "name": data["name"], "price": data["price"]}
|
|
|
|
| 257 |
],
|
| 258 |
)
|
| 259 |
elif request.url.path == "/orders" and request.method == "POST":
|
|
|
|
|
|
|
| 260 |
data = json.loads(request.content)
|
| 261 |
return httpx.Response(
|
| 262 |
201,
|
|
|
|
| 416 |
}
|
| 417 |
|
| 418 |
# This should not raise a ValidationError
|
|
|
|
|
|
|
| 419 |
routes = parse_openapi_to_http_routes(spec_with_exclusive_max)
|
| 420 |
assert len(routes) == 1
|
| 421 |
assert routes[0].operation_id == "createLoan"
|
|
|
|
| 463 |
}
|
| 464 |
|
| 465 |
# This should not raise a ValidationError
|
|
|
|
|
|
|
| 466 |
routes = parse_openapi_to_http_routes(spec_with_exclusive_max)
|
| 467 |
assert len(routes) == 1
|
| 468 |
assert routes[0].operation_id == "createLoan"
|
|
|
|
| 502 |
}
|
| 503 |
|
| 504 |
# This should not raise a ValidationError
|
|
|
|
|
|
|
| 505 |
routes = parse_openapi_to_http_routes(spec_with_nullable)
|
| 506 |
assert len(routes) == 1
|
| 507 |
assert routes[0].operation_id == "createUser"
|
|
|
|
| 543 |
}
|
| 544 |
|
| 545 |
# This should not raise a ValidationError
|
|
|
|
|
|
|
| 546 |
routes = parse_openapi_to_http_routes(spec_with_type_array)
|
| 547 |
assert len(routes) == 1
|
| 548 |
assert routes[0].operation_id == "createUser"
|
|
|
|
| 610 |
}
|
| 611 |
|
| 612 |
# This should reproduce the validation error from GitHub issue #1021
|
| 613 |
+
with pytest.raises(ValidationError):
|
| 614 |
+
parse_openapi_to_http_routes(spec_with_defs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
def test_openapi_30_edge_case_with_multiple_exclusive_constraints(self):
|
| 617 |
"""Test edge case with multiple exclusive constraints that might trigger validation issues."""
|
|
|
|
| 656 |
}
|
| 657 |
|
| 658 |
# This might trigger validation issues with multiple exclusive constraints
|
|
|
|
|
|
|
| 659 |
routes = parse_openapi_to_http_routes(spec_edge_case)
|
| 660 |
assert len(routes) == 1
|
| 661 |
assert routes[0].operation_id == "validateData"
|