| 1 |
from fastapi import Body, Cookie, FastAPI, Header, Path, Query |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
from pydantic import BaseModel |
| 5 |
|
| 6 |
app = FastAPI() |
| 7 |
|
| 8 |
|
| 9 |
class Item(BaseModel): |
| 10 |
data: str |
| 11 |
|
| 12 |
|
| 13 |
@app.post("/examples/") |
| 14 |
def examples( |
| 15 |
item: Item = Body( |
| 16 |
examples=[ |
| 17 |
{"data": "Data in Body examples, example1"}, |
| 18 |
], |
| 19 |
openapi_examples={ |
| 20 |
"Example One": { |
| 21 |
"summary": "Example One Summary", |
| 22 |
"description": "Example One Description", |
| 23 |
"value": {"data": "Data in Body examples, example1"}, |
| 24 |
}, |
| 25 |
"Example Two": { |
| 26 |
"value": {"data": "Data in Body examples, example2"}, |
| 27 |
}, |
| 28 |
}, |
| 29 |
), |
| 30 |
): |
| 31 |
return item |
| 32 |
|
| 33 |
|
| 34 |
@app.get("/path_examples/{item_id}") |
| 35 |
def path_examples( |
| 36 |
item_id: str = Path( |
| 37 |
examples=[ |
| 38 |
"json_schema_item_1", |
| 39 |
"json_schema_item_2", |
| 40 |
], |
| 41 |
openapi_examples={ |
| 42 |
"Path One": { |
| 43 |
"summary": "Path One Summary", |
| 44 |
"description": "Path One Description", |
| 45 |
"value": "item_1", |
| 46 |
}, |
| 47 |
"Path Two": { |
| 48 |
"value": "item_2", |
| 49 |
}, |
| 50 |
}, |
| 51 |
), |
| 52 |
): |
| 53 |
return item_id |
| 54 |
|
| 55 |
|
| 56 |
@app.get("/query_examples/") |
| 57 |
def query_examples( |
| 58 |
data: str | None = Query( |
| 59 |
default=None, |
| 60 |
examples=[ |
| 61 |
"json_schema_query1", |
| 62 |
"json_schema_query2", |
| 63 |
], |
| 64 |
openapi_examples={ |
| 65 |
"Query One": { |
| 66 |
"summary": "Query One Summary", |
| 67 |
"description": "Query One Description", |
| 68 |
"value": "query1", |
| 69 |
}, |
| 70 |
"Query Two": { |
| 71 |
"value": "query2", |
| 72 |
}, |
| 73 |
}, |
| 74 |
), |
| 75 |
): |
| 76 |
return data |
| 77 |
|
| 78 |
|
| 79 |
@app.get("/header_examples/") |
| 80 |
def header_examples( |
| 81 |
data: str | None = Header( |
| 82 |
default=None, |
| 83 |
examples=[ |
| 84 |
"json_schema_header1", |
| 85 |
"json_schema_header2", |
| 86 |
], |
| 87 |
openapi_examples={ |
| 88 |
"Header One": { |
| 89 |
"summary": "Header One Summary", |
| 90 |
"description": "Header One Description", |
| 91 |
"value": "header1", |
| 92 |
}, |
| 93 |
"Header Two": { |
| 94 |
"value": "header2", |
| 95 |
}, |
| 96 |
}, |
| 97 |
), |
| 98 |
): |
| 99 |
return data |
| 100 |
|
| 101 |
|
| 102 |
@app.get("/cookie_examples/") |
| 103 |
def cookie_examples( |
| 104 |
data: str | None = Cookie( |
| 105 |
default=None, |
| 106 |
examples=["json_schema_cookie1", "json_schema_cookie2"], |
| 107 |
openapi_examples={ |
| 108 |
"Cookie One": { |
| 109 |
"summary": "Cookie One Summary", |
| 110 |
"description": "Cookie One Description", |
| 111 |
"value": "cookie1", |
| 112 |
}, |
| 113 |
"Cookie Two": { |
| 114 |
"value": "cookie2", |
| 115 |
}, |
| 116 |
}, |
| 117 |
), |
| 118 |
): |
| 119 |
return data |
| 120 |
|
| 121 |
|
| 122 |
client = TestClient(app) |
| 123 |
|
| 124 |
|
| 125 |
def test_call_api(): |
| 126 |
response = client.post("/examples/", json={"data": "example1"}) |
| 127 |
assert response.status_code == 200, response.text |
| 128 |
|
| 129 |
response = client.get("/path_examples/foo") |
| 130 |
assert response.status_code == 200, response.text |
| 131 |
|
| 132 |
response = client.get("/query_examples/") |
| 133 |
assert response.status_code == 200, response.text |
| 134 |
|
| 135 |
response = client.get("/header_examples/") |
| 136 |
assert response.status_code == 200, response.text |
| 137 |
|
| 138 |
response = client.get("/cookie_examples/") |
| 139 |
assert response.status_code == 200, response.text |
| 140 |
|
| 141 |
|
| 142 |
def test_openapi_schema(): |
| 143 |
response = client.get("/openapi.json") |
| 144 |
assert response.status_code == 200, response.text |
| 145 |
assert response.json() == snapshot( |
| 146 |
{ |
| 147 |
"openapi": "3.1.0", |
| 148 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 149 |
"paths": { |
| 150 |
"/examples/": { |
| 151 |
"post": { |
| 152 |
"summary": "Examples", |
| 153 |
"operationId": "examples_examples__post", |
| 154 |
"requestBody": { |
| 155 |
"content": { |
| 156 |
"application/json": { |
| 157 |
"schema": { |
| 158 |
"$ref": "#/components/schemas/Item", |
| 159 |
"examples": [ |
| 160 |
{"data": "Data in Body examples, example1"} |
| 161 |
], |
| 162 |
}, |
| 163 |
"examples": { |
| 164 |
"Example One": { |
| 165 |
"summary": "Example One Summary", |
| 166 |
"description": "Example One Description", |
| 167 |
"value": { |
| 168 |
"data": "Data in Body examples, example1" |
| 169 |
}, |
| 170 |
}, |
| 171 |
"Example Two": { |
| 172 |
"value": { |
| 173 |
"data": "Data in Body examples, example2" |
| 174 |
} |
| 175 |
}, |
| 176 |
}, |
| 177 |
} |
| 178 |
}, |
| 179 |
"required": True, |
| 180 |
}, |
| 181 |
"responses": { |
| 182 |
"200": { |
| 183 |
"description": "Successful Response", |
| 184 |
"content": {"application/json": {"schema": {}}}, |
| 185 |
}, |
| 186 |
"422": { |
| 187 |
"description": "Validation Error", |
| 188 |
"content": { |
| 189 |
"application/json": { |
| 190 |
"schema": { |
| 191 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 192 |
} |
| 193 |
} |
| 194 |
}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
} |
| 198 |
}, |
| 199 |
"/path_examples/{item_id}": { |
| 200 |
"get": { |
| 201 |
"summary": "Path Examples", |
| 202 |
"operationId": "path_examples_path_examples__item_id__get", |
| 203 |
"parameters": [ |
| 204 |
{ |
| 205 |
"name": "item_id", |
| 206 |
"in": "path", |
| 207 |
"required": True, |
| 208 |
"schema": { |
| 209 |
"type": "string", |
| 210 |
"examples": [ |
| 211 |
"json_schema_item_1", |
| 212 |
"json_schema_item_2", |
| 213 |
], |
| 214 |
"title": "Item Id", |
| 215 |
}, |
| 216 |
"examples": { |
| 217 |
"Path One": { |
| 218 |
"summary": "Path One Summary", |
| 219 |
"description": "Path One Description", |
| 220 |
"value": "item_1", |
| 221 |
}, |
| 222 |
"Path Two": {"value": "item_2"}, |
| 223 |
}, |
| 224 |
} |
| 225 |
], |
| 226 |
"responses": { |
| 227 |
"200": { |
| 228 |
"description": "Successful Response", |
| 229 |
"content": {"application/json": {"schema": {}}}, |
| 230 |
}, |
| 231 |
"422": { |
| 232 |
"description": "Validation Error", |
| 233 |
"content": { |
| 234 |
"application/json": { |
| 235 |
"schema": { |
| 236 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 237 |
} |
| 238 |
} |
| 239 |
}, |
| 240 |
}, |
| 241 |
}, |
| 242 |
} |
| 243 |
}, |
| 244 |
"/query_examples/": { |
| 245 |
"get": { |
| 246 |
"summary": "Query Examples", |
| 247 |
"operationId": "query_examples_query_examples__get", |
| 248 |
"parameters": [ |
| 249 |
{ |
| 250 |
"name": "data", |
| 251 |
"in": "query", |
| 252 |
"required": False, |
| 253 |
"schema": { |
| 254 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 255 |
"examples": [ |
| 256 |
"json_schema_query1", |
| 257 |
"json_schema_query2", |
| 258 |
], |
| 259 |
"title": "Data", |
| 260 |
}, |
| 261 |
"examples": { |
| 262 |
"Query One": { |
| 263 |
"summary": "Query One Summary", |
| 264 |
"description": "Query One Description", |
| 265 |
"value": "query1", |
| 266 |
}, |
| 267 |
"Query Two": {"value": "query2"}, |
| 268 |
}, |
| 269 |
} |
| 270 |
], |
| 271 |
"responses": { |
| 272 |
"200": { |
| 273 |
"description": "Successful Response", |
| 274 |
"content": {"application/json": {"schema": {}}}, |
| 275 |
}, |
| 276 |
"422": { |
| 277 |
"description": "Validation Error", |
| 278 |
"content": { |
| 279 |
"application/json": { |
| 280 |
"schema": { |
| 281 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 282 |
} |
| 283 |
} |
| 284 |
}, |
| 285 |
}, |
| 286 |
}, |
| 287 |
} |
| 288 |
}, |
| 289 |
"/header_examples/": { |
| 290 |
"get": { |
| 291 |
"summary": "Header Examples", |
| 292 |
"operationId": "header_examples_header_examples__get", |
| 293 |
"parameters": [ |
| 294 |
{ |
| 295 |
"name": "data", |
| 296 |
"in": "header", |
| 297 |
"required": False, |
| 298 |
"schema": { |
| 299 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 300 |
"examples": [ |
| 301 |
"json_schema_header1", |
| 302 |
"json_schema_header2", |
| 303 |
], |
| 304 |
"title": "Data", |
| 305 |
}, |
| 306 |
"examples": { |
| 307 |
"Header One": { |
| 308 |
"summary": "Header One Summary", |
| 309 |
"description": "Header One Description", |
| 310 |
"value": "header1", |
| 311 |
}, |
| 312 |
"Header Two": {"value": "header2"}, |
| 313 |
}, |
| 314 |
} |
| 315 |
], |
| 316 |
"responses": { |
| 317 |
"200": { |
| 318 |
"description": "Successful Response", |
| 319 |
"content": {"application/json": {"schema": {}}}, |
| 320 |
}, |
| 321 |
"422": { |
| 322 |
"description": "Validation Error", |
| 323 |
"content": { |
| 324 |
"application/json": { |
| 325 |
"schema": { |
| 326 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 327 |
} |
| 328 |
} |
| 329 |
}, |
| 330 |
}, |
| 331 |
}, |
| 332 |
} |
| 333 |
}, |
| 334 |
"/cookie_examples/": { |
| 335 |
"get": { |
| 336 |
"summary": "Cookie Examples", |
| 337 |
"operationId": "cookie_examples_cookie_examples__get", |
| 338 |
"parameters": [ |
| 339 |
{ |
| 340 |
"name": "data", |
| 341 |
"in": "cookie", |
| 342 |
"required": False, |
| 343 |
"schema": { |
| 344 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 345 |
"examples": [ |
| 346 |
"json_schema_cookie1", |
| 347 |
"json_schema_cookie2", |
| 348 |
], |
| 349 |
"title": "Data", |
| 350 |
}, |
| 351 |
"examples": { |
| 352 |
"Cookie One": { |
| 353 |
"summary": "Cookie One Summary", |
| 354 |
"description": "Cookie One Description", |
| 355 |
"value": "cookie1", |
| 356 |
}, |
| 357 |
"Cookie Two": {"value": "cookie2"}, |
| 358 |
}, |
| 359 |
} |
| 360 |
], |
| 361 |
"responses": { |
| 362 |
"200": { |
| 363 |
"description": "Successful Response", |
| 364 |
"content": {"application/json": {"schema": {}}}, |
| 365 |
}, |
| 366 |
"422": { |
| 367 |
"description": "Validation Error", |
| 368 |
"content": { |
| 369 |
"application/json": { |
| 370 |
"schema": { |
| 371 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 372 |
} |
| 373 |
} |
| 374 |
}, |
| 375 |
}, |
| 376 |
}, |
| 377 |
} |
| 378 |
}, |
| 379 |
}, |
| 380 |
"components": { |
| 381 |
"schemas": { |
| 382 |
"HTTPValidationError": { |
| 383 |
"properties": { |
| 384 |
"detail": { |
| 385 |
"items": { |
| 386 |
"$ref": "#/components/schemas/ValidationError" |
| 387 |
}, |
| 388 |
"type": "array", |
| 389 |
"title": "Detail", |
| 390 |
} |
| 391 |
}, |
| 392 |
"type": "object", |
| 393 |
"title": "HTTPValidationError", |
| 394 |
}, |
| 395 |
"Item": { |
| 396 |
"properties": {"data": {"type": "string", "title": "Data"}}, |
| 397 |
"type": "object", |
| 398 |
"required": ["data"], |
| 399 |
"title": "Item", |
| 400 |
}, |
| 401 |
"ValidationError": { |
| 402 |
"properties": { |
| 403 |
"ctx": {"title": "Context", "type": "object"}, |
| 404 |
"input": {"title": "Input"}, |
| 405 |
"loc": { |
| 406 |
"items": { |
| 407 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 408 |
}, |
| 409 |
"type": "array", |
| 410 |
"title": "Location", |
| 411 |
}, |
| 412 |
"msg": {"type": "string", "title": "Message"}, |
| 413 |
"type": {"type": "string", "title": "Error Type"}, |
| 414 |
}, |
| 415 |
"type": "object", |
| 416 |
"required": ["loc", "msg", "type"], |
| 417 |
"title": "ValidationError", |
| 418 |
}, |
| 419 |
} |
| 420 |
}, |
| 421 |
} |
| 422 |
) |