1
from typing import Union
3
from dirty_equals import IsDict
4
from fastapi import Body, Cookie, FastAPI, Header, Path, Query
5
from fastapi.testclient import TestClient
6
from pydantic import BaseModel
15
@app.post("/examples/")
19
{"data": "Data in Body examples, example1"},
23
"summary": "Example One Summary",
24
"description": "Example One Description",
25
"value": {"data": "Data in Body examples, example1"},
28
"value": {"data": "Data in Body examples, example2"},
36
@app.get("/path_examples/{item_id}")
45
"summary": "Path One Summary",
46
"description": "Path One Description",
58
@app.get("/query_examples/")
60
data: Union[str, None] = Query(
68
"summary": "Query One Summary",
69
"description": "Query One Description",
81
@app.get("/header_examples/")
83
data: Union[str, None] = Header(
86
"json_schema_header1",
87
"json_schema_header2",
91
"summary": "Header One Summary",
92
"description": "Header One Description",
104
@app.get("/cookie_examples/")
106
data: Union[str, None] = Cookie(
108
examples=["json_schema_cookie1", "json_schema_cookie2"],
111
"summary": "Cookie One Summary",
112
"description": "Cookie One Description",
124
client = TestClient(app)
128
response = client.post("/examples/", json={"data": "example1"})
129
assert response.status_code == 200, response.text
131
response = client.get("/path_examples/foo")
132
assert response.status_code == 200, response.text
134
response = client.get("/query_examples/")
135
assert response.status_code == 200, response.text
137
response = client.get("/header_examples/")
138
assert response.status_code == 200, response.text
140
response = client.get("/cookie_examples/")
141
assert response.status_code == 200, response.text
144
def test_openapi_schema():
145
response = client.get("/openapi.json")
146
assert response.status_code == 200, response.text
147
assert response.json() == {
149
"info": {"title": "FastAPI", "version": "0.1.0"},
153
"summary": "Examples",
154
"operationId": "examples_examples__post",
157
"application/json": {
159
"allOf": [{"$ref": "#/components/schemas/Item"}],
162
{"data": "Data in Body examples, example1"}
167
"summary": "Example One Summary",
168
"description": "Example One Description",
170
"data": "Data in Body examples, example1"
175
"data": "Data in Body examples, example2"
185
"description": "Successful Response",
186
"content": {"application/json": {"schema": {}}},
189
"description": "Validation Error",
191
"application/json": {
193
"$ref": "#/components/schemas/HTTPValidationError"
201
"/path_examples/{item_id}": {
203
"summary": "Path Examples",
204
"operationId": "path_examples_path_examples__item_id__get",
213
"json_schema_item_1",
214
"json_schema_item_2",
220
"summary": "Path One Summary",
221
"description": "Path One Description",
224
"Path Two": {"value": "item_2"},
230
"description": "Successful Response",
231
"content": {"application/json": {"schema": {}}},
234
"description": "Validation Error",
236
"application/json": {
238
"$ref": "#/components/schemas/HTTPValidationError"
246
"/query_examples/": {
248
"summary": "Query Examples",
249
"operationId": "query_examples_query_examples__get",
257
"anyOf": [{"type": "string"}, {"type": "null"}],
259
"json_schema_query1",
260
"json_schema_query2",
266
# TODO: remove when deprecating Pydantic v1
269
"json_schema_query1",
270
"json_schema_query2",
278
"summary": "Query One Summary",
279
"description": "Query One Description",
282
"Query Two": {"value": "query2"},
288
"description": "Successful Response",
289
"content": {"application/json": {"schema": {}}},
292
"description": "Validation Error",
294
"application/json": {
296
"$ref": "#/components/schemas/HTTPValidationError"
304
"/header_examples/": {
306
"summary": "Header Examples",
307
"operationId": "header_examples_header_examples__get",
315
"anyOf": [{"type": "string"}, {"type": "null"}],
317
"json_schema_header1",
318
"json_schema_header2",
324
# TODO: remove when deprecating Pydantic v1
328
"json_schema_header1",
329
"json_schema_header2",
336
"summary": "Header One Summary",
337
"description": "Header One Description",
340
"Header Two": {"value": "header2"},
346
"description": "Successful Response",
347
"content": {"application/json": {"schema": {}}},
350
"description": "Validation Error",
352
"application/json": {
354
"$ref": "#/components/schemas/HTTPValidationError"
362
"/cookie_examples/": {
364
"summary": "Cookie Examples",
365
"operationId": "cookie_examples_cookie_examples__get",
373
"anyOf": [{"type": "string"}, {"type": "null"}],
375
"json_schema_cookie1",
376
"json_schema_cookie2",
382
# TODO: remove when deprecating Pydantic v1
386
"json_schema_cookie1",
387
"json_schema_cookie2",
394
"summary": "Cookie One Summary",
395
"description": "Cookie One Description",
398
"Cookie Two": {"value": "cookie2"},
404
"description": "Successful Response",
405
"content": {"application/json": {"schema": {}}},
408
"description": "Validation Error",
410
"application/json": {
412
"$ref": "#/components/schemas/HTTPValidationError"
423
"HTTPValidationError": {
426
"items": {"$ref": "#/components/schemas/ValidationError"},
432
"title": "HTTPValidationError",
435
"properties": {"data": {"type": "string", "title": "Data"}},
437
"required": ["data"],
444
"anyOf": [{"type": "string"}, {"type": "integer"}]
449
"msg": {"type": "string", "title": "Message"},
450
"type": {"type": "string", "title": "Error Type"},
453
"required": ["loc", "msg", "type"],
454
"title": "ValidationError",