Coverage for tests / test_arbitrary_types.py: 100%
39 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Annotated 1lmno
3import pytest 1lmno
4from fastapi import FastAPI 1lmno
5from fastapi.testclient import TestClient 1lmno
6from inline_snapshot import snapshot 1lmno
9@pytest.fixture(name="client") 1lmno
10def get_client(): 1lmno
11 from pydantic import ( 1abghcdef
12 BaseModel,
13 ConfigDict,
14 PlainSerializer,
15 TypeAdapter,
16 WithJsonSchema,
17 )
19 class FakeNumpyArray: 1abghcdef
20 def __init__(self): 1abghcdef
21 self.data = [1.0, 2.0, 3.0] 1qrst
23 FakeNumpyArrayPydantic = Annotated[ 1abghcdef
24 FakeNumpyArray,
25 WithJsonSchema(TypeAdapter(list[float]).json_schema()),
26 PlainSerializer(lambda v: v.data),
27 ]
29 class MyModel(BaseModel): 1abghcdef
30 model_config = ConfigDict(arbitrary_types_allowed=True) 1abghcdef
31 custom_field: FakeNumpyArrayPydantic 1abcdef
33 app = FastAPI() 1abghcdef
35 @app.get("/") 1abghcdef
36 def test() -> MyModel: 1abghcdef
37 return MyModel(custom_field=FakeNumpyArray()) 1qrst
39 client = TestClient(app) 1abghcdef
40 return client 1abghcdef
43def test_get(client: TestClient): 1lmno
44 response = client.get("/") 1qrst
45 assert response.json() == {"custom_field": [1.0, 2.0, 3.0]} 1qrst
48def test_typeadapter(): 1lmno
49 # This test is only to confirm that Pydantic alone is working as expected
50 from pydantic import ( 1ipjk
51 BaseModel,
52 ConfigDict,
53 PlainSerializer,
54 TypeAdapter,
55 WithJsonSchema,
56 )
58 class FakeNumpyArray: 1ipjk
59 def __init__(self): 1ipjk
60 self.data = [1.0, 2.0, 3.0] 1ipjk
62 FakeNumpyArrayPydantic = Annotated[ 1ipjk
63 FakeNumpyArray,
64 WithJsonSchema(TypeAdapter(list[float]).json_schema()),
65 PlainSerializer(lambda v: v.data),
66 ]
68 class MyModel(BaseModel): 1ipjk
69 model_config = ConfigDict(arbitrary_types_allowed=True) 1ipjk
70 custom_field: FakeNumpyArrayPydantic 1ijk
72 ta = TypeAdapter(MyModel) 1ipjk
73 assert ta.dump_python(MyModel(custom_field=FakeNumpyArray())) == { 1ipjk
74 "custom_field": [1.0, 2.0, 3.0]
75 }
76 assert ta.json_schema() == snapshot( 1ipjk
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 )
92def test_openapi_schema(client: TestClient): 1lmno
93 response = client.get("openapi.json") 1uvwx
94 assert response.json() == snapshot( 1uvwx
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 )