|
|
@ -1,4 +1,5 @@ |
|
|
"""Test case for issue #13056: Can't use `Annotated` with `ForwardRef`""" |
|
|
"""Test case for issue #13056: Can't use `Annotated` with `ForwardRef`""" |
|
|
|
|
|
|
|
|
from __future__ import annotations |
|
|
from __future__ import annotations |
|
|
|
|
|
|
|
|
from dataclasses import dataclass |
|
|
from dataclasses import dataclass |
|
|
@ -11,12 +12,12 @@ app = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_potato() -> Potato: |
|
|
def get_potato() -> Potato: |
|
|
return Potato(color='red', size=10) |
|
|
return Potato(color="red", size=10) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/') |
|
|
@app.get("/") |
|
|
async def read_root(potato: Annotated[Potato, Depends(get_potato)]): |
|
|
async def read_root(potato: Annotated[Potato, Depends(get_potato)]): |
|
|
return {'color': potato.color, 'size': potato.size} |
|
|
return {"color": potato.color, "size": potato.size} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
@dataclass |
|
|
@ -30,24 +31,26 @@ client = TestClient(app) |
|
|
|
|
|
|
|
|
def test_annotated_forward_ref(): |
|
|
def test_annotated_forward_ref(): |
|
|
"""Test that forward references work correctly with Annotated dependencies.""" |
|
|
"""Test that forward references work correctly with Annotated dependencies.""" |
|
|
response = client.get('/') |
|
|
response = client.get("/") |
|
|
assert response.status_code == 200 |
|
|
assert response.status_code == 200 |
|
|
data = response.json() |
|
|
data = response.json() |
|
|
assert data == {'color': 'red', 'size': 10} |
|
|
assert data == {"color": "red", "size": 10} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_openapi_schema(): |
|
|
def test_openapi_schema(): |
|
|
"""Test that OpenAPI schema is generated correctly.""" |
|
|
"""Test that OpenAPI schema is generated correctly.""" |
|
|
response = client.get('/openapi.json') |
|
|
response = client.get("/openapi.json") |
|
|
assert response.status_code == 200 |
|
|
assert response.status_code == 200 |
|
|
schema = response.json() |
|
|
schema = response.json() |
|
|
# The root path should NOT have query parameters for potato |
|
|
# The root path should NOT have query parameters for potato |
|
|
# It should only be a dependency |
|
|
# It should only be a dependency |
|
|
root_path = schema['paths']['/']['get'] |
|
|
root_path = schema["paths"]["/"]["get"] |
|
|
# Check that potato is not a query parameter |
|
|
# Check that potato is not a query parameter |
|
|
parameters = root_path.get('parameters', []) |
|
|
parameters = root_path.get("parameters", []) |
|
|
potato_params = [p for p in parameters if 'potato' in p.get('name', '').lower()] |
|
|
potato_params = [p for p in parameters if "potato" in p.get("name", "").lower()] |
|
|
assert len(potato_params) == 0, f"Potato should not appear as a query parameter: {potato_params}" |
|
|
assert len(potato_params) == 0, ( |
|
|
|
|
|
f"Potato should not appear as a query parameter: {potato_params}" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
|