|
7 | 7 | from pathlib import Path |
8 | 8 | from typing import Annotated, NamedTuple, Optional, Union, get_args, get_origin |
9 | 9 |
|
10 | | -import yaml |
11 | 10 | from common_library.json_serialization import json_dumps |
12 | 11 | from common_library.pydantic_fields_extension import get_type |
13 | | -from fastapi import FastAPI, Query |
| 12 | +from fastapi import Query |
14 | 13 | from models_library.basic_types import LogLevel |
15 | 14 | from pydantic import BaseModel, ConfigDict, Field, Json, create_model |
16 | 15 | from pydantic.fields import FieldInfo |
17 | | -from servicelib.fastapi.openapi import override_fastapi_openapi_method |
18 | 16 |
|
19 | 17 | CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent |
20 | 18 |
|
@@ -119,48 +117,6 @@ class Error(BaseModel): |
119 | 117 | status: int | None = Field(None, description="HTTP error code") |
120 | 118 |
|
121 | 119 |
|
122 | | -def create_openapi_specs( |
123 | | - app: FastAPI, |
124 | | - *, |
125 | | - drop_fastapi_default_422: bool = True, |
126 | | - remove_main_sections: bool = True, |
127 | | -): |
128 | | - override_fastapi_openapi_method(app) |
129 | | - openapi = app.openapi() |
130 | | - |
131 | | - # Remove these sections |
132 | | - if remove_main_sections: |
133 | | - for section in ("info", "openapi"): |
134 | | - openapi.pop(section, None) |
135 | | - |
136 | | - schemas = openapi["components"]["schemas"] |
137 | | - for section in ("HTTPValidationError", "ValidationError"): |
138 | | - schemas.pop(section, None) |
139 | | - |
140 | | - # Removes default response 422 |
141 | | - if drop_fastapi_default_422: |
142 | | - for method_item in openapi.get("paths", {}).values(): |
143 | | - for param in method_item.values(): |
144 | | - # NOTE: If description is like this, |
145 | | - # it assumes it is the default HTTPValidationError from fastapi |
146 | | - if (e422 := param.get("responses", {}).get("422", None)) and e422.get( |
147 | | - "description" |
148 | | - ) == "Validation Error": |
149 | | - param.get("responses", {}).pop("422", None) |
150 | | - return openapi |
151 | | - |
152 | | - |
153 | | -def create_and_save_openapi_specs( |
154 | | - app: FastAPI, file_path: Path, *, drop_fastapi_default_422: bool = True |
155 | | -): |
156 | | - openapi = create_openapi_specs( |
157 | | - app=app, drop_fastapi_default_422=drop_fastapi_default_422 |
158 | | - ) |
159 | | - with file_path.open("wt") as fh: |
160 | | - yaml.safe_dump(openapi, fh, indent=1, sort_keys=False) |
161 | | - print("Saved OAS to", file_path) # noqa: T201 |
162 | | - |
163 | | - |
164 | 120 | class ParamSpec(NamedTuple): |
165 | 121 | name: str |
166 | 122 | annotated_type: type |
|
0 commit comments