| 1 |
from fastapi import FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
|
| 5 |
app = FastAPI() |
| 6 |
|
| 7 |
|
| 8 |
@app.get( |
| 9 |
"/", |
| 10 |
openapi_extra={ |
| 11 |
"parameters": [ |
| 12 |
{ |
| 13 |
"required": False, |
| 14 |
"schema": {"title": "Extra Param 1"}, |
| 15 |
"name": "extra_param_1", |
| 16 |
"in": "query", |
| 17 |
}, |
| 18 |
{ |
| 19 |
"required": True, |
| 20 |
"schema": {"title": "Extra Param 2"}, |
| 21 |
"name": "extra_param_2", |
| 22 |
"in": "query", |
| 23 |
}, |
| 24 |
] |
| 25 |
}, |
| 26 |
) |
| 27 |
def route_with_extra_query_parameters(standard_query_param: int | None = 50): |
| 28 |
return {} |
| 29 |
|
| 30 |
|
| 31 |
client = TestClient(app) |
| 32 |
|
| 33 |
|
| 34 |
def test_get_route(): |
| 35 |
response = client.get("/") |
| 36 |
assert response.status_code == 200, response.text |
| 37 |
assert response.json() == {} |
| 38 |
|
| 39 |
|
| 40 |
def test_openapi(): |
| 41 |
response = client.get("/openapi.json") |
| 42 |
assert response.status_code == 200, response.text |
| 43 |
assert response.json() == snapshot( |
| 44 |
{ |
| 45 |
"openapi": "3.1.0", |
| 46 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 47 |
"paths": { |
| 48 |
"/": { |
| 49 |
"get": { |
| 50 |
"summary": "Route With Extra Query Parameters", |
| 51 |
"operationId": "route_with_extra_query_parameters__get", |
| 52 |
"parameters": [ |
| 53 |
{ |
| 54 |
"required": False, |
| 55 |
"schema": { |
| 56 |
"anyOf": [{"type": "integer"}, {"type": "null"}], |
| 57 |
"default": 50, |
| 58 |
"title": "Standard Query Param", |
| 59 |
}, |
| 60 |
"name": "standard_query_param", |
| 61 |
"in": "query", |
| 62 |
}, |
| 63 |
{ |
| 64 |
"required": False, |
| 65 |
"schema": {"title": "Extra Param 1"}, |
| 66 |
"name": "extra_param_1", |
| 67 |
"in": "query", |
| 68 |
}, |
| 69 |
{ |
| 70 |
"required": True, |
| 71 |
"schema": {"title": "Extra Param 2"}, |
| 72 |
"name": "extra_param_2", |
| 73 |
"in": "query", |
| 74 |
}, |
| 75 |
], |
| 76 |
"responses": { |
| 77 |
"200": { |
| 78 |
"description": "Successful Response", |
| 79 |
"content": {"application/json": {"schema": {}}}, |
| 80 |
}, |
| 81 |
"422": { |
| 82 |
"description": "Validation Error", |
| 83 |
"content": { |
| 84 |
"application/json": { |
| 85 |
"schema": { |
| 86 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 87 |
} |
| 88 |
} |
| 89 |
}, |
| 90 |
}, |
| 91 |
}, |
| 92 |
} |
| 93 |
} |
| 94 |
}, |
| 95 |
"components": { |
| 96 |
"schemas": { |
| 97 |
"HTTPValidationError": { |
| 98 |
"title": "HTTPValidationError", |
| 99 |
"type": "object", |
| 100 |
"properties": { |
| 101 |
"detail": { |
| 102 |
"title": "Detail", |
| 103 |
"type": "array", |
| 104 |
"items": { |
| 105 |
"$ref": "#/components/schemas/ValidationError" |
| 106 |
}, |
| 107 |
} |
| 108 |
}, |
| 109 |
}, |
| 110 |
"ValidationError": { |
| 111 |
"title": "ValidationError", |
| 112 |
"required": ["loc", "msg", "type"], |
| 113 |
"type": "object", |
| 114 |
"properties": { |
| 115 |
"loc": { |
| 116 |
"title": "Location", |
| 117 |
"type": "array", |
| 118 |
"items": { |
| 119 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 120 |
}, |
| 121 |
}, |
| 122 |
"msg": {"title": "Message", "type": "string"}, |
| 123 |
"type": {"title": "Error Type", "type": "string"}, |
| 124 |
"input": {"title": "Input"}, |
| 125 |
"ctx": {"title": "Context", "type": "object"}, |
| 126 |
}, |
| 127 |
}, |
| 128 |
} |
| 129 |
}, |
| 130 |
} |
| 131 |
) |