| 1 |
from fastapi import Depends, FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
from pydantic import BaseModel |
| 5 |
|
| 6 |
app = FastAPI() |
| 7 |
|
| 8 |
client = TestClient(app) |
| 9 |
|
| 10 |
|
| 11 |
class Item(BaseModel): |
| 12 |
data: str |
| 13 |
|
| 14 |
|
| 15 |
def duplicate_dependency(item: Item): |
| 16 |
return item |
| 17 |
|
| 18 |
|
| 19 |
def dependency(item2: Item): |
| 20 |
return item2 |
| 21 |
|
| 22 |
|
| 23 |
def sub_duplicate_dependency( |
| 24 |
item: Item, sub_item: Item = Depends(duplicate_dependency) |
| 25 |
): |
| 26 |
return [item, sub_item] |
| 27 |
|
| 28 |
|
| 29 |
@app.post("/with-duplicates") |
| 30 |
async def with_duplicates(item: Item, item2: Item = Depends(duplicate_dependency)): |
| 31 |
return [item, item2] |
| 32 |
|
| 33 |
|
| 34 |
@app.post("/no-duplicates") |
| 35 |
async def no_duplicates(item: Item, item2: Item = Depends(dependency)): |
| 36 |
return [item, item2] |
| 37 |
|
| 38 |
|
| 39 |
@app.post("/with-duplicates-sub") |
| 40 |
async def no_duplicates_sub( |
| 41 |
item: Item, sub_items: list[Item] = Depends(sub_duplicate_dependency) |
| 42 |
): |
| 43 |
return [item, sub_items] |
| 44 |
|
| 45 |
|
| 46 |
def test_no_duplicates_invalid(): |
| 47 |
response = client.post("/no-duplicates", json={"item": {"data": "myitem"}}) |
| 48 |
assert response.status_code == 422, response.text |
| 49 |
assert response.json() == { |
| 50 |
"detail": [ |
| 51 |
{ |
| 52 |
"type": "missing", |
| 53 |
"loc": ["body", "item2"], |
| 54 |
"msg": "Field required", |
| 55 |
"input": None, |
| 56 |
} |
| 57 |
] |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
def test_no_duplicates(): |
| 62 |
response = client.post( |
| 63 |
"/no-duplicates", |
| 64 |
json={"item": {"data": "myitem"}, "item2": {"data": "myitem2"}}, |
| 65 |
) |
| 66 |
assert response.status_code == 200, response.text |
| 67 |
assert response.json() == [{"data": "myitem"}, {"data": "myitem2"}] |
| 68 |
|
| 69 |
|
| 70 |
def test_duplicates(): |
| 71 |
response = client.post("/with-duplicates", json={"data": "myitem"}) |
| 72 |
assert response.status_code == 200, response.text |
| 73 |
assert response.json() == [{"data": "myitem"}, {"data": "myitem"}] |
| 74 |
|
| 75 |
|
| 76 |
def test_sub_duplicates(): |
| 77 |
response = client.post("/with-duplicates-sub", json={"data": "myitem"}) |
| 78 |
assert response.status_code == 200, response.text |
| 79 |
assert response.json() == [ |
| 80 |
{"data": "myitem"}, |
| 81 |
[{"data": "myitem"}, {"data": "myitem"}], |
| 82 |
] |
| 83 |
|
| 84 |
|
| 85 |
def test_openapi_schema(): |
| 86 |
response = client.get("/openapi.json") |
| 87 |
assert response.status_code == 200, response.text |
| 88 |
assert response.json() == snapshot( |
| 89 |
{ |
| 90 |
"openapi": "3.1.0", |
| 91 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 92 |
"paths": { |
| 93 |
"/with-duplicates": { |
| 94 |
"post": { |
| 95 |
"summary": "With Duplicates", |
| 96 |
"operationId": "with_duplicates_with_duplicates_post", |
| 97 |
"requestBody": { |
| 98 |
"content": { |
| 99 |
"application/json": { |
| 100 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 101 |
} |
| 102 |
}, |
| 103 |
"required": True, |
| 104 |
}, |
| 105 |
"responses": { |
| 106 |
"200": { |
| 107 |
"description": "Successful Response", |
| 108 |
"content": {"application/json": {"schema": {}}}, |
| 109 |
}, |
| 110 |
"422": { |
| 111 |
"description": "Validation Error", |
| 112 |
"content": { |
| 113 |
"application/json": { |
| 114 |
"schema": { |
| 115 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 116 |
} |
| 117 |
} |
| 118 |
}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
} |
| 122 |
}, |
| 123 |
"/no-duplicates": { |
| 124 |
"post": { |
| 125 |
"summary": "No Duplicates", |
| 126 |
"operationId": "no_duplicates_no_duplicates_post", |
| 127 |
"requestBody": { |
| 128 |
"content": { |
| 129 |
"application/json": { |
| 130 |
"schema": { |
| 131 |
"$ref": "#/components/schemas/Body_no_duplicates_no_duplicates_post" |
| 132 |
} |
| 133 |
} |
| 134 |
}, |
| 135 |
"required": True, |
| 136 |
}, |
| 137 |
"responses": { |
| 138 |
"200": { |
| 139 |
"description": "Successful Response", |
| 140 |
"content": {"application/json": {"schema": {}}}, |
| 141 |
}, |
| 142 |
"422": { |
| 143 |
"description": "Validation Error", |
| 144 |
"content": { |
| 145 |
"application/json": { |
| 146 |
"schema": { |
| 147 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 148 |
} |
| 149 |
} |
| 150 |
}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
} |
| 154 |
}, |
| 155 |
"/with-duplicates-sub": { |
| 156 |
"post": { |
| 157 |
"summary": "No Duplicates Sub", |
| 158 |
"operationId": "no_duplicates_sub_with_duplicates_sub_post", |
| 159 |
"requestBody": { |
| 160 |
"content": { |
| 161 |
"application/json": { |
| 162 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 163 |
} |
| 164 |
}, |
| 165 |
"required": True, |
| 166 |
}, |
| 167 |
"responses": { |
| 168 |
"200": { |
| 169 |
"description": "Successful Response", |
| 170 |
"content": {"application/json": {"schema": {}}}, |
| 171 |
}, |
| 172 |
"422": { |
| 173 |
"description": "Validation Error", |
| 174 |
"content": { |
| 175 |
"application/json": { |
| 176 |
"schema": { |
| 177 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 178 |
} |
| 179 |
} |
| 180 |
}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
} |
| 184 |
}, |
| 185 |
}, |
| 186 |
"components": { |
| 187 |
"schemas": { |
| 188 |
"Body_no_duplicates_no_duplicates_post": { |
| 189 |
"title": "Body_no_duplicates_no_duplicates_post", |
| 190 |
"required": ["item", "item2"], |
| 191 |
"type": "object", |
| 192 |
"properties": { |
| 193 |
"item": {"$ref": "#/components/schemas/Item"}, |
| 194 |
"item2": {"$ref": "#/components/schemas/Item"}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
"HTTPValidationError": { |
| 198 |
"title": "HTTPValidationError", |
| 199 |
"type": "object", |
| 200 |
"properties": { |
| 201 |
"detail": { |
| 202 |
"title": "Detail", |
| 203 |
"type": "array", |
| 204 |
"items": { |
| 205 |
"$ref": "#/components/schemas/ValidationError" |
| 206 |
}, |
| 207 |
} |
| 208 |
}, |
| 209 |
}, |
| 210 |
"Item": { |
| 211 |
"title": "Item", |
| 212 |
"required": ["data"], |
| 213 |
"type": "object", |
| 214 |
"properties": {"data": {"title": "Data", "type": "string"}}, |
| 215 |
}, |
| 216 |
"ValidationError": { |
| 217 |
"title": "ValidationError", |
| 218 |
"required": ["loc", "msg", "type"], |
| 219 |
"type": "object", |
| 220 |
"properties": { |
| 221 |
"loc": { |
| 222 |
"title": "Location", |
| 223 |
"type": "array", |
| 224 |
"items": { |
| 225 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 226 |
}, |
| 227 |
}, |
| 228 |
"msg": {"title": "Message", "type": "string"}, |
| 229 |
"type": {"title": "Error Type", "type": "string"}, |
| 230 |
"input": {"title": "Input"}, |
| 231 |
"ctx": {"title": "Context", "type": "object"}, |
| 232 |
}, |
| 233 |
}, |
| 234 |
} |
| 235 |
}, |
| 236 |
} |
| 237 |
) |