1
from typing import List, Optional
3
from fastapi import FastAPI
4
from fastapi.testclient import TestClient
5
from pydantic import BaseModel
7
from .utils import PYDANTIC_V2, needs_pydanticv2
10
class SubItem(BaseModel):
12
sub_description: Optional[str] = None
15
model_config = {"json_schema_serialization_defaults_required": True}
20
description: Optional[str] = None
21
sub: Optional[SubItem] = None
23
model_config = {"json_schema_serialization_defaults_required": True}
26
def get_app_client(separate_input_output_schemas: bool = True) -> TestClient:
27
app = FastAPI(separate_input_output_schemas=separate_input_output_schemas)
30
def create_item(item: Item):
33
@app.post("/items-list/")
34
def create_item_list(item: List[Item]):
38
def read_items() -> List[Item]:
42
description="Device to travel through the multi-rick-verse",
43
sub=SubItem(subname="subname"),
48
client = TestClient(app)
52
def test_create_item():
53
client = get_app_client()
54
client_no = get_app_client(separate_input_output_schemas=False)
55
response = client.post("/items/", json={"name": "Plumbus"})
56
response2 = client_no.post("/items/", json={"name": "Plumbus"})
57
assert response.status_code == response2.status_code == 200, response.text
61
== {"name": "Plumbus", "description": None, "sub": None}
65
def test_create_item_with_sub():
66
client = get_app_client()
67
client_no = get_app_client(separate_input_output_schemas=False)
70
"sub": {"subname": "SubPlumbus", "sub_description": "Sub WTF"},
72
response = client.post("/items/", json=data)
73
response2 = client_no.post("/items/", json=data)
74
assert response.status_code == response2.status_code == 200, response.text
81
"sub": {"subname": "SubPlumbus", "sub_description": "Sub WTF", "tags": []},
86
def test_create_item_list():
87
client = get_app_client()
88
client_no = get_app_client(separate_input_output_schemas=False)
93
"description": "Device to travel through the multi-rick-verse",
96
response = client.post("/items-list/", json=data)
97
response2 = client_no.post("/items-list/", json=data)
98
assert response.status_code == response2.status_code == 200, response.text
103
{"name": "Plumbus", "description": None, "sub": None},
105
"name": "Portal Gun",
106
"description": "Device to travel through the multi-rick-verse",
113
def test_read_items():
114
client = get_app_client()
115
client_no = get_app_client(separate_input_output_schemas=False)
116
response = client.get("/items/")
117
response2 = client_no.get("/items/")
118
assert response.status_code == response2.status_code == 200, response.text
124
"name": "Portal Gun",
125
"description": "Device to travel through the multi-rick-verse",
126
"sub": {"subname": "subname", "sub_description": None, "tags": []},
128
{"name": "Plumbus", "description": None, "sub": None},
134
def test_openapi_schema():
135
client = get_app_client()
136
response = client.get("/openapi.json")
137
assert response.status_code == 200, response.text
138
assert response.json() == {
140
"info": {"title": "FastAPI", "version": "0.1.0"},
144
"summary": "Read Items",
145
"operationId": "read_items_items__get",
148
"description": "Successful Response",
150
"application/json": {
153
"$ref": "#/components/schemas/Item-Output"
156
"title": "Response Read Items Items Get",
164
"summary": "Create Item",
165
"operationId": "create_item_items__post",
168
"application/json": {
169
"schema": {"$ref": "#/components/schemas/Item-Input"}
176
"description": "Successful Response",
177
"content": {"application/json": {"schema": {}}},
180
"description": "Validation Error",
182
"application/json": {
184
"$ref": "#/components/schemas/HTTPValidationError"
194
"summary": "Create Item List",
195
"operationId": "create_item_list_items_list__post",
198
"application/json": {
201
"$ref": "#/components/schemas/Item-Input"
212
"description": "Successful Response",
213
"content": {"application/json": {"schema": {}}},
216
"description": "Validation Error",
218
"application/json": {
220
"$ref": "#/components/schemas/HTTPValidationError"
231
"HTTPValidationError": {
234
"items": {"$ref": "#/components/schemas/ValidationError"},
240
"title": "HTTPValidationError",
244
"name": {"type": "string", "title": "Name"},
246
"anyOf": [{"type": "string"}, {"type": "null"}],
247
"title": "Description",
251
{"$ref": "#/components/schemas/SubItem-Input"},
257
"required": ["name"],
262
"name": {"type": "string", "title": "Name"},
264
"anyOf": [{"type": "string"}, {"type": "null"}],
265
"title": "Description",
269
{"$ref": "#/components/schemas/SubItem-Output"},
275
"required": ["name", "description", "sub"],
280
"subname": {"type": "string", "title": "Subname"},
282
"anyOf": [{"type": "string"}, {"type": "null"}],
283
"title": "Sub Description",
286
"items": {"type": "string"},
293
"required": ["subname"],
298
"subname": {"type": "string", "title": "Subname"},
300
"anyOf": [{"type": "string"}, {"type": "null"}],
301
"title": "Sub Description",
304
"items": {"type": "string"},
311
"required": ["subname", "sub_description", "tags"],
318
"anyOf": [{"type": "string"}, {"type": "integer"}]
323
"msg": {"type": "string", "title": "Message"},
324
"type": {"type": "string", "title": "Error Type"},
327
"required": ["loc", "msg", "type"],
328
"title": "ValidationError",
336
def test_openapi_schema_no_separate():
337
client = get_app_client(separate_input_output_schemas=False)
338
response = client.get("/openapi.json")
339
assert response.status_code == 200, response.text
340
assert response.json() == {
342
"info": {"title": "FastAPI", "version": "0.1.0"},
346
"summary": "Read Items",
347
"operationId": "read_items_items__get",
350
"description": "Successful Response",
352
"application/json": {
354
"items": {"$ref": "#/components/schemas/Item"},
356
"title": "Response Read Items Items Get",
364
"summary": "Create Item",
365
"operationId": "create_item_items__post",
368
"application/json": {
369
"schema": {"$ref": "#/components/schemas/Item"}
376
"description": "Successful Response",
377
"content": {"application/json": {"schema": {}}},
380
"description": "Validation Error",
382
"application/json": {
384
"$ref": "#/components/schemas/HTTPValidationError"
394
"summary": "Create Item List",
395
"operationId": "create_item_list_items_list__post",
398
"application/json": {
400
"items": {"$ref": "#/components/schemas/Item"},
410
"description": "Successful Response",
411
"content": {"application/json": {"schema": {}}},
414
"description": "Validation Error",
416
"application/json": {
418
"$ref": "#/components/schemas/HTTPValidationError"
429
"HTTPValidationError": {
432
"items": {"$ref": "#/components/schemas/ValidationError"},
438
"title": "HTTPValidationError",
442
"name": {"type": "string", "title": "Name"},
444
"anyOf": [{"type": "string"}, {"type": "null"}],
445
"title": "Description",
449
{"$ref": "#/components/schemas/SubItem"},
455
"required": ["name"],
460
"subname": {"type": "string", "title": "Subname"},
462
"anyOf": [{"type": "string"}, {"type": "null"}],
463
"title": "Sub Description",
466
"items": {"type": "string"},
473
"required": ["subname"],
480
"anyOf": [{"type": "string"}, {"type": "integer"}]
485
"msg": {"type": "string", "title": "Message"},
486
"type": {"type": "string", "title": "Error Type"},
489
"required": ["loc", "msg", "type"],
490
"title": "ValidationError",