Jeremiah Lowin commited on
Commit
a7bd2ea
·
1 Parent(s): 112de41

Add test for enum property

Browse files
Files changed (1) hide show
  1. tests/server/test_openapi.py +73 -0
tests/server/test_openapi.py CHANGED
@@ -1,6 +1,7 @@
1
  import base64
2
  import json
3
  import re
 
4
 
5
  import httpx
6
  import pytest
@@ -1817,3 +1818,75 @@ class TestReprMethods:
1817
  assert f"name={template.name!r}" in template_repr
1818
  assert "uri_template=" in template_repr
1819
  assert "path=" in template_repr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import base64
2
  import json
3
  import re
4
+ from enum import Enum
5
 
6
  import httpx
7
  import pytest
 
1818
  assert f"name={template.name!r}" in template_repr
1819
  assert "uri_template=" in template_repr
1820
  assert "path=" in template_repr
1821
+
1822
+
1823
+ class TestEnumHandling:
1824
+ """Tests for handling enum parameters in OpenAPI schemas."""
1825
+
1826
+ async def test_enum_parameter_schema(self):
1827
+ """Test that enum parameters are properly handled in tool parameter schemas."""
1828
+
1829
+ # Define an enum just like in example.py
1830
+ class QueryEnum(str, Enum):
1831
+ foo = "foo"
1832
+ bar = "bar"
1833
+ baz = "baz"
1834
+
1835
+ # Create a minimal FastAPI app with an endpoint using the enum
1836
+ app = FastAPI()
1837
+
1838
+ @app.post("/items/{item_id}")
1839
+ def read_item(
1840
+ item_id: int,
1841
+ query: QueryEnum | None = None,
1842
+ ):
1843
+ return {"item_id": item_id, "query": query}
1844
+
1845
+ # Create a client for the app
1846
+ client = AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
1847
+
1848
+ # Create the FastMCPOpenAPI server from the app
1849
+ openapi_spec = app.openapi()
1850
+ server = FastMCPOpenAPI(
1851
+ openapi_spec=openapi_spec,
1852
+ client=client,
1853
+ name="Enum Test",
1854
+ )
1855
+
1856
+ # Get the tools from the server
1857
+ tools = server._tool_manager.list_tools()
1858
+
1859
+ # Find the read_item tool
1860
+ read_item_tool = next(
1861
+ (t for t in tools if t.name == "read_item_items__item_id__post"), None
1862
+ )
1863
+
1864
+ # Verify the tool exists
1865
+ assert read_item_tool is not None, "read_item tool wasn't created"
1866
+
1867
+ # Check that the parameters include the enum reference
1868
+ assert "properties" in read_item_tool.parameters
1869
+ assert "query" in read_item_tool.parameters["properties"]
1870
+
1871
+ # Check for the anyOf with $ref to the enum definition
1872
+ query_param = read_item_tool.parameters["properties"]["query"]
1873
+ assert "anyOf" in query_param
1874
+
1875
+ # Find the ref in the anyOf list
1876
+ ref_found = False
1877
+ for option in query_param["anyOf"]:
1878
+ if "$ref" in option and option["$ref"].startswith("#/$defs/QueryEnum"):
1879
+ ref_found = True
1880
+ break
1881
+
1882
+ assert ref_found, "Reference to enum definition not found in query parameter"
1883
+
1884
+ # Check that the $defs section exists and contains the enum definition
1885
+ assert "$defs" in read_item_tool.parameters
1886
+ assert "QueryEnum" in read_item_tool.parameters["$defs"]
1887
+
1888
+ # Verify the enum definition
1889
+ enum_def = read_item_tool.parameters["$defs"]["QueryEnum"]
1890
+ assert "enum" in enum_def
1891
+ assert enum_def["enum"] == ["foo", "bar", "baz"]
1892
+ assert enum_def["type"] == "string"