| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi import FastAPI |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture(name="client") |
| 10 |
def get_client(): |
| 11 |
from pydantic import ( |
| 12 |
BaseModel, |
| 13 |
ConfigDict, |
| 14 |
PlainSerializer, |
| 15 |
TypeAdapter, |
| 16 |
WithJsonSchema, |
| 17 |
) |
| 18 |
|
| 19 |
class FakeNumpyArray: |
| 20 |
def __init__(self): |
| 21 |
self.data = [1.0, 2.0, 3.0] |
| 22 |
|
| 23 |
FakeNumpyArrayPydantic = Annotated[ |
| 24 |
FakeNumpyArray, |
| 25 |
WithJsonSchema(TypeAdapter(list[float]).json_schema()), |
| 26 |
PlainSerializer(lambda v: v.data), |
| 27 |
] |
| 28 |
|
| 29 |
class MyModel(BaseModel): |
| 30 |
model_config = ConfigDict(arbitrary_types_allowed=True) |
| 31 |
custom_field: FakeNumpyArrayPydantic |
| 32 |
|
| 33 |
app = FastAPI() |
| 34 |
|
| 35 |
@app.get("/") |
| 36 |
def test() -> MyModel: |
| 37 |
return MyModel(custom_field=FakeNumpyArray()) |
| 38 |
|
| 39 |
client = TestClient(app) |
| 40 |
return client |
| 41 |
|
| 42 |
|
| 43 |
def test_get(client: TestClient): |
| 44 |
response = client.get("/") |
| 45 |
assert response.json() == {"custom_field": [1.0, 2.0, 3.0]} |
| 46 |
|
| 47 |
|
| 48 |
def test_typeadapter(): |
| 49 |
# This test is only to confirm that Pydantic alone is working as expected |
| 50 |
from pydantic import ( |
| 51 |
BaseModel, |
| 52 |
ConfigDict, |
| 53 |
PlainSerializer, |
| 54 |
TypeAdapter, |
| 55 |
WithJsonSchema, |
| 56 |
) |
| 57 |
|
| 58 |
class FakeNumpyArray: |
| 59 |
def __init__(self): |
| 60 |
self.data = [1.0, 2.0, 3.0] |
| 61 |
|
| 62 |
FakeNumpyArrayPydantic = Annotated[ |
| 63 |
FakeNumpyArray, |
| 64 |
WithJsonSchema(TypeAdapter(list[float]).json_schema()), |
| 65 |
PlainSerializer(lambda v: v.data), |
| 66 |
] |
| 67 |
|
| 68 |
class MyModel(BaseModel): |
| 69 |
model_config = ConfigDict(arbitrary_types_allowed=True) |
| 70 |
custom_field: FakeNumpyArrayPydantic |
| 71 |
|
| 72 |
ta = TypeAdapter(MyModel) |
| 73 |
assert ta.dump_python(MyModel(custom_field=FakeNumpyArray())) == { |
| 74 |
"custom_field": [1.0, 2.0, 3.0] |
| 75 |
} |
| 76 |
assert ta.json_schema() == snapshot( |
| 77 |
{ |
| 78 |
"properties": { |
| 79 |
"custom_field": { |
| 80 |
"items": {"type": "number"}, |
| 81 |
"title": "Custom Field", |
| 82 |
"type": "array", |
| 83 |
} |
| 84 |
}, |
| 85 |
"required": ["custom_field"], |
| 86 |
"title": "MyModel", |
| 87 |
"type": "object", |
| 88 |
} |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def test_openapi_schema(client: TestClient): |
| 93 |
response = client.get("openapi.json") |
| 94 |
assert response.json() == snapshot( |
| 95 |
{ |
| 96 |
"openapi": "3.1.0", |
| 97 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 98 |
"paths": { |
| 99 |
"/": { |
| 100 |
"get": { |
| 101 |
"summary": "Test", |
| 102 |
"operationId": "test__get", |
| 103 |
"responses": { |
| 104 |
"200": { |
| 105 |
"description": "Successful Response", |
| 106 |
"content": { |
| 107 |
"application/json": { |
| 108 |
"schema": { |
| 109 |
"$ref": "#/components/schemas/MyModel" |
| 110 |
} |
| 111 |
} |
| 112 |
}, |
| 113 |
} |
| 114 |
}, |
| 115 |
} |
| 116 |
} |
| 117 |
}, |
| 118 |
"components": { |
| 119 |
"schemas": { |
| 120 |
"MyModel": { |
| 121 |
"properties": { |
| 122 |
"custom_field": { |
| 123 |
"items": {"type": "number"}, |
| 124 |
"type": "array", |
| 125 |
"title": "Custom Field", |
| 126 |
} |
| 127 |
}, |
| 128 |
"type": "object", |
| 129 |
"required": ["custom_field"], |
| 130 |
"title": "MyModel", |
| 131 |
} |
| 132 |
} |
| 133 |
}, |
| 134 |
} |
| 135 |
) |