Skip to content

Commit 3f999c4

Browse files
committed
new util
1 parent 3dedd99 commit 3f999c4

File tree

1 file changed

+34
-0
lines changed
  • packages/service-library/src/servicelib/fastapi

1 file changed

+34
-0
lines changed

packages/service-library/src/servicelib/fastapi/openapi.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,37 @@ def _custom_openapi_method(self: FastAPI) -> dict:
166166
return output
167167

168168
setattr(app, "openapi", types.MethodType(_custom_openapi_method, app)) # noqa: B010
169+
170+
171+
def create_openapi_specs(
172+
app: FastAPI,
173+
*,
174+
drop_fastapi_default_422: bool = True,
175+
remove_main_sections: bool = True,
176+
):
177+
"""
178+
Includes some patches used in the api/specs generators
179+
"""
180+
override_fastapi_openapi_method(app)
181+
openapi = app.openapi()
182+
183+
# Remove these sections
184+
if remove_main_sections:
185+
for section in ("info", "openapi"):
186+
openapi.pop(section, None)
187+
188+
schemas = openapi["components"]["schemas"]
189+
for section in ("HTTPValidationError", "ValidationError"):
190+
schemas.pop(section, None)
191+
192+
# Removes default response 422
193+
if drop_fastapi_default_422:
194+
for method_item in openapi.get("paths", {}).values():
195+
for param in method_item.values():
196+
# NOTE: If description is like this,
197+
# it assumes it is the default HTTPValidationError from fastapi
198+
if (e422 := param.get("responses", {}).get("422", None)) and e422.get(
199+
"description"
200+
) == "Validation Error":
201+
param.get("responses", {}).pop("422", None)
202+
return openapi

0 commit comments

Comments
 (0)