| 1 |
import sys |
| 2 |
import warnings |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from tests.utils import skip_module_if_py_gte_314 |
| 7 |
|
| 8 |
if sys.version_info >= (3, 14): |
| 9 |
skip_module_if_py_gte_314() |
| 10 |
|
| 11 |
from fastapi import FastAPI |
| 12 |
from fastapi.exceptions import PydanticV1NotSupportedError |
| 13 |
|
| 14 |
with warnings.catch_warnings(): |
| 15 |
warnings.simplefilter("ignore", UserWarning) |
| 16 |
from pydantic.v1 import BaseModel |
| 17 |
|
| 18 |
|
| 19 |
def test_raises_pydantic_v1_model_in_endpoint_param() -> None: |
| 20 |
class ParamModelV1(BaseModel): |
| 21 |
name: str |
| 22 |
|
| 23 |
app = FastAPI() |
| 24 |
|
| 25 |
with pytest.raises(PydanticV1NotSupportedError): |
| 26 |
|
| 27 |
@app.post("/param") |
| 28 |
def endpoint(data: ParamModelV1): # pragma: no cover |
| 29 |
return data |
| 30 |
|
| 31 |
|
| 32 |
def test_raises_pydantic_v1_model_in_return_type() -> None: |
| 33 |
class ReturnModelV1(BaseModel): |
| 34 |
name: str |
| 35 |
|
| 36 |
app = FastAPI() |
| 37 |
|
| 38 |
with pytest.raises(PydanticV1NotSupportedError): |
| 39 |
|
| 40 |
@app.get("/return") |
| 41 |
def endpoint() -> ReturnModelV1: # pragma: no cover |
| 42 |
return ReturnModelV1(name="test") |
| 43 |
|
| 44 |
|
| 45 |
def test_raises_pydantic_v1_model_in_response_model() -> None: |
| 46 |
class ResponseModelV1(BaseModel): |
| 47 |
name: str |
| 48 |
|
| 49 |
app = FastAPI() |
| 50 |
|
| 51 |
with pytest.raises(PydanticV1NotSupportedError): |
| 52 |
|
| 53 |
@app.get("/response-model", response_model=ResponseModelV1) |
| 54 |
def endpoint(): # pragma: no cover |
| 55 |
return {"name": "test"} |
| 56 |
|
| 57 |
|
| 58 |
def test_raises_pydantic_v1_model_in_additional_responses_model() -> None: |
| 59 |
class ErrorModelV1(BaseModel): |
| 60 |
detail: str |
| 61 |
|
| 62 |
app = FastAPI() |
| 63 |
|
| 64 |
with pytest.raises(PydanticV1NotSupportedError): |
| 65 |
|
| 66 |
@app.get( |
| 67 |
"/responses", response_model=None, responses={400: {"model": ErrorModelV1}} |
| 68 |
) |
| 69 |
def endpoint(): # pragma: no cover |
| 70 |
return {"ok": True} |
| 71 |
|
| 72 |
|
| 73 |
def test_raises_pydantic_v1_model_in_union() -> None: |
| 74 |
class ModelV1A(BaseModel): |
| 75 |
name: str |
| 76 |
|
| 77 |
app = FastAPI() |
| 78 |
|
| 79 |
with pytest.raises(PydanticV1NotSupportedError): |
| 80 |
|
| 81 |
@app.post("/union") |
| 82 |
def endpoint(data: dict | ModelV1A): # pragma: no cover |
| 83 |
return data |
| 84 |
|
| 85 |
|
| 86 |
def test_raises_pydantic_v1_model_in_sequence() -> None: |
| 87 |
class ModelV1A(BaseModel): |
| 88 |
name: str |
| 89 |
|
| 90 |
app = FastAPI() |
| 91 |
|
| 92 |
with pytest.raises(PydanticV1NotSupportedError): |
| 93 |
|
| 94 |
@app.post("/sequence") |
| 95 |
def endpoint(data: list[ModelV1A]): # pragma: no cover |
| 96 |
return data |