Skip to content

Commit cc08e52

Browse files
committed
feat: Register generic 4xx and 5xx responses for paths in swagger docs.
1 parent 76e0e52 commit cc08e52

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

src/fastapi_problem/handler.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,32 @@ def wrapper() -> dict:
103103
"title": "Problem",
104104
}
105105

106+
res["components"]["schemas"]["HTTPValidationError"] = validation_error
107+
res["components"]["schemas"]["Problem"] = problem
108+
106109
if generic_defaults:
107-
res["components"]["schemas"]["HTTPValidationError"] = validation_error
108-
res["components"]["schemas"]["Problem"] = problem
110+
for methods in res["paths"].values():
111+
for details in methods.values():
112+
details["responses"]["4XX"] = {
113+
"description": "Client Error",
114+
"content": {
115+
"application/json": {
116+
"schema": {
117+
"$ref": "#/components/schemas/Problem",
118+
},
119+
},
120+
},
121+
}
122+
details["responses"]["5XX"] = {
123+
"description": "Server Error",
124+
"content": {
125+
"application/json": {
126+
"schema": {
127+
"$ref": "#/components/schemas/Problem",
128+
},
129+
},
130+
},
131+
}
109132

110133
return res
111134

tests/test_handler.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,50 @@ async def status(_a: str) -> dict:
484484
assert "Problem" in res["components"]["schemas"]
485485
assert "ValidationError" in res["components"]["schemas"]
486486

487+
assert res["paths"]["/status"]["get"]["responses"] == {
488+
"200": {
489+
"content": {
490+
"application/json": {
491+
"schema": {
492+
"title": "Response Status Status Get",
493+
"type": "object",
494+
},
495+
},
496+
},
497+
"description": "Successful Response",
498+
},
499+
"422": {
500+
"content": {
501+
"application/json": {
502+
"schema": {
503+
"$ref": "#/components/schemas/HTTPValidationError",
504+
},
505+
},
506+
},
507+
"description": "Validation Error",
508+
},
509+
"4XX": {
510+
"content": {
511+
"application/json": {
512+
"schema": {
513+
"$ref": "#/components/schemas/Problem",
514+
},
515+
},
516+
},
517+
"description": "Client Error",
518+
},
519+
"5XX": {
520+
"content": {
521+
"application/json": {
522+
"schema": {
523+
"$ref": "#/components/schemas/Problem",
524+
},
525+
},
526+
},
527+
"description": "Server Error",
528+
},
529+
}
530+
487531

488532
async def test_customise_openapi_handles_no_components():
489533
app = FastAPI()
@@ -493,3 +537,71 @@ async def test_customise_openapi_handles_no_components():
493537
res = app.openapi()
494538
assert res["paths"] == {}
495539
assert "components" not in res
540+
541+
542+
async def test_customise_openapi_generic_opt_out():
543+
app = FastAPI()
544+
545+
app.openapi = handler.customise_openapi(app.openapi, generic_defaults=False)
546+
547+
@app.get("/status")
548+
async def status(_a: str) -> dict:
549+
return {}
550+
551+
res = app.openapi()
552+
assert res["components"]["schemas"]["HTTPValidationError"] == {
553+
"properties": {
554+
"title": {
555+
"type": "string",
556+
"title": "Problem Title",
557+
},
558+
"type": {
559+
"type": "string",
560+
"title": "Problem type",
561+
},
562+
"status": {
563+
"type": "integer",
564+
"title": "Status code",
565+
},
566+
"errors": {
567+
"type": "array",
568+
"items": {
569+
"$ref": "#/components/schemas/ValidationError",
570+
},
571+
},
572+
},
573+
"type": "object",
574+
"required": [
575+
"type",
576+
"title",
577+
"errors",
578+
"status",
579+
],
580+
"title": "ValidationError",
581+
}
582+
assert "Problem" in res["components"]["schemas"]
583+
assert "ValidationError" in res["components"]["schemas"]
584+
585+
assert res["paths"]["/status"]["get"]["responses"] == {
586+
"200": {
587+
"content": {
588+
"application/json": {
589+
"schema": {
590+
"title": "Response Status Status Get",
591+
"type": "object",
592+
},
593+
},
594+
},
595+
"description": "Successful Response",
596+
},
597+
"422": {
598+
"content": {
599+
"application/json": {
600+
"schema": {
601+
"$ref": "#/components/schemas/HTTPValidationError",
602+
},
603+
},
604+
},
605+
"description": "Validation Error",
606+
},
607+
}

0 commit comments

Comments
 (0)