|
| 1 | +"""Test FAST002 ellipsis handling.""" |
| 2 | + |
| 3 | +from fastapi import Body, Cookie, FastAPI, Header, Query |
| 4 | + |
| 5 | +app = FastAPI() |
| 6 | + |
| 7 | + |
| 8 | +# Cases that should be fixed - ellipsis should be removed |
| 9 | + |
| 10 | + |
| 11 | +@app.get("/test1") |
| 12 | +async def test_ellipsis_query( |
| 13 | + # This should become: param: Annotated[str, Query(description="Test param")] |
| 14 | + param: str = Query(..., description="Test param"), |
| 15 | +) -> str: |
| 16 | + return param |
| 17 | + |
| 18 | + |
| 19 | +@app.get("/test2") |
| 20 | +async def test_ellipsis_header( |
| 21 | + # This should become: auth: Annotated[str, Header(description="Auth header")] |
| 22 | + auth: str = Header(..., description="Auth header"), |
| 23 | +) -> str: |
| 24 | + return auth |
| 25 | + |
| 26 | + |
| 27 | +@app.post("/test3") |
| 28 | +async def test_ellipsis_body( |
| 29 | + # This should become: data: Annotated[dict, Body(description="Request body")] |
| 30 | + data: dict = Body(..., description="Request body"), |
| 31 | +) -> dict: |
| 32 | + return data |
| 33 | + |
| 34 | + |
| 35 | +@app.get("/test4") |
| 36 | +async def test_ellipsis_cookie( |
| 37 | + # This should become: session: Annotated[str, Cookie(description="Session ID")] |
| 38 | + session: str = Cookie(..., description="Session ID"), |
| 39 | +) -> str: |
| 40 | + return session |
| 41 | + |
| 42 | + |
| 43 | +@app.get("/test5") |
| 44 | +async def test_simple_ellipsis( |
| 45 | + # This should become: id: Annotated[str, Query()] |
| 46 | + id: str = Query(...), |
| 47 | +) -> str: |
| 48 | + return id |
| 49 | + |
| 50 | + |
| 51 | +@app.get("/test6") |
| 52 | +async def test_multiple_kwargs_with_ellipsis( |
| 53 | + # This should become: param: Annotated[str, Query(description="Test", min_length=1, max_length=10)] |
| 54 | + param: str = Query(..., description="Test", min_length=1, max_length=10), |
| 55 | +) -> str: |
| 56 | + return param |
| 57 | + |
| 58 | + |
| 59 | +# Cases with actual default values - these should preserve the default |
| 60 | + |
| 61 | + |
| 62 | +@app.get("/test7") |
| 63 | +async def test_with_default_value( |
| 64 | + # This should become: param: Annotated[str, Query(description="Test")] = "default" |
| 65 | + param: str = Query("default", description="Test"), |
| 66 | +) -> str: |
| 67 | + return param |
| 68 | + |
| 69 | + |
| 70 | +@app.get("/test8") |
| 71 | +async def test_with_default_none( |
| 72 | + # This should become: param: Annotated[str | None, Query(description="Test")] = None |
| 73 | + param: str | None = Query(None, description="Test"), |
| 74 | +) -> str: |
| 75 | + return param or "empty" |
| 76 | + |
| 77 | + |
| 78 | +@app.get("/test9") |
| 79 | +async def test_mixed_parameters( |
| 80 | + # First param should be fixed with default preserved |
| 81 | + optional_param: str = Query("default", description="Optional"), |
| 82 | + # Second param should not be fixed because of the preceding default |
| 83 | + required_param: str = Query(..., description="Required"), |
| 84 | + # Third param should be fixed with default preserved |
| 85 | + another_optional_param: int = Query(42, description="Another optional"), |
| 86 | +) -> str: |
| 87 | + return f"{required_param}-{optional_param}-{another_optional_param}" |
0 commit comments