Coverage for tests / test_openapi_examples.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Union 1abcd

2 

3from fastapi import Body, Cookie, FastAPI, Header, Path, Query 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6from pydantic import BaseModel 1abcd

7 

8app = FastAPI() 1abcd

9 

10 

11class Item(BaseModel): 1abcd

12 data: str 1abcd

13 

14 

15@app.post("/examples/") 1abcd

16def examples( 1abcd

17 item: Item = Body( 

18 examples=[ 

19 {"data": "Data in Body examples, example1"}, 

20 ], 

21 openapi_examples={ 

22 "Example One": { 

23 "summary": "Example One Summary", 

24 "description": "Example One Description", 

25 "value": {"data": "Data in Body examples, example1"}, 

26 }, 

27 "Example Two": { 

28 "value": {"data": "Data in Body examples, example2"}, 

29 }, 

30 }, 

31 ), 

32): 

33 return item 1efg

34 

35 

36@app.get("/path_examples/{item_id}") 1abcd

37def path_examples( 1abcd

38 item_id: str = Path( 

39 examples=[ 

40 "json_schema_item_1", 

41 "json_schema_item_2", 

42 ], 

43 openapi_examples={ 

44 "Path One": { 

45 "summary": "Path One Summary", 

46 "description": "Path One Description", 

47 "value": "item_1", 

48 }, 

49 "Path Two": { 

50 "value": "item_2", 

51 }, 

52 }, 

53 ), 

54): 

55 return item_id 1efg

56 

57 

58@app.get("/query_examples/") 1abcd

59def query_examples( 1abcd

60 data: Union[str, None] = Query( 

61 default=None, 

62 examples=[ 

63 "json_schema_query1", 

64 "json_schema_query2", 

65 ], 

66 openapi_examples={ 

67 "Query One": { 

68 "summary": "Query One Summary", 

69 "description": "Query One Description", 

70 "value": "query1", 

71 }, 

72 "Query Two": { 

73 "value": "query2", 

74 }, 

75 }, 

76 ), 

77): 

78 return data 1efg

79 

80 

81@app.get("/header_examples/") 1abcd

82def header_examples( 1abcd

83 data: Union[str, None] = Header( 

84 default=None, 

85 examples=[ 

86 "json_schema_header1", 

87 "json_schema_header2", 

88 ], 

89 openapi_examples={ 

90 "Header One": { 

91 "summary": "Header One Summary", 

92 "description": "Header One Description", 

93 "value": "header1", 

94 }, 

95 "Header Two": { 

96 "value": "header2", 

97 }, 

98 }, 

99 ), 

100): 

101 return data 1efg

102 

103 

104@app.get("/cookie_examples/") 1abcd

105def cookie_examples( 1abcd

106 data: Union[str, None] = Cookie( 

107 default=None, 

108 examples=["json_schema_cookie1", "json_schema_cookie2"], 

109 openapi_examples={ 

110 "Cookie One": { 

111 "summary": "Cookie One Summary", 

112 "description": "Cookie One Description", 

113 "value": "cookie1", 

114 }, 

115 "Cookie Two": { 

116 "value": "cookie2", 

117 }, 

118 }, 

119 ), 

120): 

121 return data 1efg

122 

123 

124client = TestClient(app) 1abcd

125 

126 

127def test_call_api(): 1abcd

128 response = client.post("/examples/", json={"data": "example1"}) 1efg

129 assert response.status_code == 200, response.text 1efg

130 

131 response = client.get("/path_examples/foo") 1efg

132 assert response.status_code == 200, response.text 1efg

133 

134 response = client.get("/query_examples/") 1efg

135 assert response.status_code == 200, response.text 1efg

136 

137 response = client.get("/header_examples/") 1efg

138 assert response.status_code == 200, response.text 1efg

139 

140 response = client.get("/cookie_examples/") 1efg

141 assert response.status_code == 200, response.text 1efg

142 

143 

144def test_openapi_schema(): 1abcd

145 response = client.get("/openapi.json") 1hij

146 assert response.status_code == 200, response.text 1hij

147 assert response.json() == snapshot( 1hij

148 { 

149 "openapi": "3.1.0", 

150 "info": {"title": "FastAPI", "version": "0.1.0"}, 

151 "paths": { 

152 "/examples/": { 

153 "post": { 

154 "summary": "Examples", 

155 "operationId": "examples_examples__post", 

156 "requestBody": { 

157 "content": { 

158 "application/json": { 

159 "schema": { 

160 "$ref": "#/components/schemas/Item", 

161 "examples": [ 

162 {"data": "Data in Body examples, example1"} 

163 ], 

164 }, 

165 "examples": { 

166 "Example One": { 

167 "summary": "Example One Summary", 

168 "description": "Example One Description", 

169 "value": { 

170 "data": "Data in Body examples, example1" 

171 }, 

172 }, 

173 "Example Two": { 

174 "value": { 

175 "data": "Data in Body examples, example2" 

176 } 

177 }, 

178 }, 

179 } 

180 }, 

181 "required": True, 

182 }, 

183 "responses": { 

184 "200": { 

185 "description": "Successful Response", 

186 "content": {"application/json": {"schema": {}}}, 

187 }, 

188 "422": { 

189 "description": "Validation Error", 

190 "content": { 

191 "application/json": { 

192 "schema": { 

193 "$ref": "#/components/schemas/HTTPValidationError" 

194 } 

195 } 

196 }, 

197 }, 

198 }, 

199 } 

200 }, 

201 "/path_examples/{item_id}": { 

202 "get": { 

203 "summary": "Path Examples", 

204 "operationId": "path_examples_path_examples__item_id__get", 

205 "parameters": [ 

206 { 

207 "name": "item_id", 

208 "in": "path", 

209 "required": True, 

210 "schema": { 

211 "type": "string", 

212 "examples": [ 

213 "json_schema_item_1", 

214 "json_schema_item_2", 

215 ], 

216 "title": "Item Id", 

217 }, 

218 "examples": { 

219 "Path One": { 

220 "summary": "Path One Summary", 

221 "description": "Path One Description", 

222 "value": "item_1", 

223 }, 

224 "Path Two": {"value": "item_2"}, 

225 }, 

226 } 

227 ], 

228 "responses": { 

229 "200": { 

230 "description": "Successful Response", 

231 "content": {"application/json": {"schema": {}}}, 

232 }, 

233 "422": { 

234 "description": "Validation Error", 

235 "content": { 

236 "application/json": { 

237 "schema": { 

238 "$ref": "#/components/schemas/HTTPValidationError" 

239 } 

240 } 

241 }, 

242 }, 

243 }, 

244 } 

245 }, 

246 "/query_examples/": { 

247 "get": { 

248 "summary": "Query Examples", 

249 "operationId": "query_examples_query_examples__get", 

250 "parameters": [ 

251 { 

252 "name": "data", 

253 "in": "query", 

254 "required": False, 

255 "schema": { 

256 "anyOf": [{"type": "string"}, {"type": "null"}], 

257 "examples": [ 

258 "json_schema_query1", 

259 "json_schema_query2", 

260 ], 

261 "title": "Data", 

262 }, 

263 "examples": { 

264 "Query One": { 

265 "summary": "Query One Summary", 

266 "description": "Query One Description", 

267 "value": "query1", 

268 }, 

269 "Query Two": {"value": "query2"}, 

270 }, 

271 } 

272 ], 

273 "responses": { 

274 "200": { 

275 "description": "Successful Response", 

276 "content": {"application/json": {"schema": {}}}, 

277 }, 

278 "422": { 

279 "description": "Validation Error", 

280 "content": { 

281 "application/json": { 

282 "schema": { 

283 "$ref": "#/components/schemas/HTTPValidationError" 

284 } 

285 } 

286 }, 

287 }, 

288 }, 

289 } 

290 }, 

291 "/header_examples/": { 

292 "get": { 

293 "summary": "Header Examples", 

294 "operationId": "header_examples_header_examples__get", 

295 "parameters": [ 

296 { 

297 "name": "data", 

298 "in": "header", 

299 "required": False, 

300 "schema": { 

301 "anyOf": [{"type": "string"}, {"type": "null"}], 

302 "examples": [ 

303 "json_schema_header1", 

304 "json_schema_header2", 

305 ], 

306 "title": "Data", 

307 }, 

308 "examples": { 

309 "Header One": { 

310 "summary": "Header One Summary", 

311 "description": "Header One Description", 

312 "value": "header1", 

313 }, 

314 "Header Two": {"value": "header2"}, 

315 }, 

316 } 

317 ], 

318 "responses": { 

319 "200": { 

320 "description": "Successful Response", 

321 "content": {"application/json": {"schema": {}}}, 

322 }, 

323 "422": { 

324 "description": "Validation Error", 

325 "content": { 

326 "application/json": { 

327 "schema": { 

328 "$ref": "#/components/schemas/HTTPValidationError" 

329 } 

330 } 

331 }, 

332 }, 

333 }, 

334 } 

335 }, 

336 "/cookie_examples/": { 

337 "get": { 

338 "summary": "Cookie Examples", 

339 "operationId": "cookie_examples_cookie_examples__get", 

340 "parameters": [ 

341 { 

342 "name": "data", 

343 "in": "cookie", 

344 "required": False, 

345 "schema": { 

346 "anyOf": [{"type": "string"}, {"type": "null"}], 

347 "examples": [ 

348 "json_schema_cookie1", 

349 "json_schema_cookie2", 

350 ], 

351 "title": "Data", 

352 }, 

353 "examples": { 

354 "Cookie One": { 

355 "summary": "Cookie One Summary", 

356 "description": "Cookie One Description", 

357 "value": "cookie1", 

358 }, 

359 "Cookie Two": {"value": "cookie2"}, 

360 }, 

361 } 

362 ], 

363 "responses": { 

364 "200": { 

365 "description": "Successful Response", 

366 "content": {"application/json": {"schema": {}}}, 

367 }, 

368 "422": { 

369 "description": "Validation Error", 

370 "content": { 

371 "application/json": { 

372 "schema": { 

373 "$ref": "#/components/schemas/HTTPValidationError" 

374 } 

375 } 

376 }, 

377 }, 

378 }, 

379 } 

380 }, 

381 }, 

382 "components": { 

383 "schemas": { 

384 "HTTPValidationError": { 

385 "properties": { 

386 "detail": { 

387 "items": { 

388 "$ref": "#/components/schemas/ValidationError" 

389 }, 

390 "type": "array", 

391 "title": "Detail", 

392 } 

393 }, 

394 "type": "object", 

395 "title": "HTTPValidationError", 

396 }, 

397 "Item": { 

398 "properties": {"data": {"type": "string", "title": "Data"}}, 

399 "type": "object", 

400 "required": ["data"], 

401 "title": "Item", 

402 }, 

403 "ValidationError": { 

404 "properties": { 

405 "ctx": {"title": "Context", "type": "object"}, 

406 "input": {"title": "Input"}, 

407 "loc": { 

408 "items": { 

409 "anyOf": [{"type": "string"}, {"type": "integer"}] 

410 }, 

411 "type": "array", 

412 "title": "Location", 

413 }, 

414 "msg": {"type": "string", "title": "Message"}, 

415 "type": {"type": "string", "title": "Error Type"}, 

416 }, 

417 "type": "object", 

418 "required": ["loc", "msg", "type"], 

419 "title": "ValidationError", 

420 }, 

421 } 

422 }, 

423 } 

424 )