Skip to content

Commit 7e7bd87

Browse files
committed
improve code
1 parent f26f1c9 commit 7e7bd87

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

flask_openapi3/models/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class APISpec(BaseModel):
5050
openapi: str
5151
info: Info
5252
servers: Optional[List[Server]] = None
53-
paths: Optional[Dict[str, PathItem]] = None
53+
paths: Paths
5454
components: Optional[Components] = None
55-
security: Optional[List[Dict[str, List[str]]]] = None
55+
security: Optional[List[SecurityRequirement]] = None
5656
tags: Optional[List[Tag]] = None
5757
externalDocs: Optional[ExternalDocumentation] = None
5858

flask_openapi3/openapi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,12 @@ def api_doc(self) -> Dict:
243243
openapi=self.openapi_version,
244244
info=self.info,
245245
servers=self.severs,
246+
paths=self.paths,
246247
externalDocs=self.external_docs
247248
)
248249
# Set tags
249250
spec.tags = self.tags or None
250251

251-
# Set paths
252-
spec.paths = self.paths
253-
254252
# Add ValidationErrorModel to components schemas
255253
self.components_schemas[self.validation_error_model.__name__] = Schema(**self.validation_error_model.schema())
256254

flask_openapi3/utils.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def parse_form(
227227

228228
assert properties, f"{form.__name__}'s properties cannot be empty."
229229

230-
title = schema.get("title")
230+
title = schema.get("title") or form.__name__
231231
components_schemas[title] = Schema(**schema)
232232
encoding = {}
233233
for k, v in properties.items():
@@ -269,7 +269,7 @@ def parse_body(
269269
schema = get_model_schema(body)
270270
components_schemas = dict()
271271

272-
title = schema.get("title")
272+
title = schema.get("title") or body.__name__
273273
components_schemas[title] = Schema(**schema)
274274
if extra_body:
275275
content = {
@@ -320,17 +320,18 @@ def get_responses(
320320
)})
321321

322322
model_config = response.Config
323-
if hasattr(model_config, "openapi_extra"):
323+
openapi_extra: dict = getattr(model_config, "openapi_extra", None) # type: ignore
324+
if openapi_extra:
324325
# Add additional information from model_config to the response
325-
_responses[key].description = model_config.openapi_extra.get("description")
326-
_responses[key].headers = model_config.openapi_extra.get("headers")
327-
_responses[key].links = model_config.openapi_extra.get("links")
326+
_responses[key].description = openapi_extra.get("description") # type: ignore
327+
_responses[key].headers = openapi_extra.get("headers")
328+
_responses[key].links = openapi_extra.get("links")
328329
_content = _responses[key].content
329-
_content["application/json"].example = model_config.openapi_extra.get("example") # type: ignore
330-
_content["application/json"].examples = model_config.openapi_extra.get("examples") # type: ignore
331-
_content["application/json"].encoding = model_config.openapi_extra.get("encoding") # type: ignore
332-
if model_config.openapi_extra.get("content"):
333-
_responses[key].content.update(model_config.openapi_extra.get("content")) # type: ignore
330+
_content["application/json"].example = openapi_extra.get("example") # type: ignore
331+
_content["application/json"].examples = openapi_extra.get("examples") # type: ignore
332+
_content["application/json"].encoding = openapi_extra.get("encoding") # type: ignore
333+
if openapi_extra.get("content"):
334+
_responses[key].content.update(openapi_extra.get("content")) # type: ignore
334335

335336
_schemas[response.__name__] = Schema(**schema)
336337
definitions = schema.get("definitions")
@@ -468,12 +469,13 @@ def parse_parameters(
468469
"content": _content,
469470
})
470471
model_config = form.Config
471-
if hasattr(model_config, "openapi_extra"):
472-
request_body.description = model_config.openapi_extra.get("description")
473-
request_body.content["multipart/form-data"].example = model_config.openapi_extra.get("example")
474-
request_body.content["multipart/form-data"].examples = model_config.openapi_extra.get("examples")
475-
if model_config.openapi_extra.get("encoding"):
476-
request_body.content["multipart/form-data"].encoding = model_config.openapi_extra.get("encoding")
472+
openapi_extra: dict = getattr(model_config, "openapi_extra", None) # type: ignore
473+
if openapi_extra:
474+
request_body.description = openapi_extra.get("description")
475+
request_body.content["multipart/form-data"].example = openapi_extra.get("example")
476+
request_body.content["multipart/form-data"].examples = openapi_extra.get("examples")
477+
if openapi_extra.get("encoding"):
478+
request_body.content["multipart/form-data"].encoding = openapi_extra.get("encoding")
477479
operation.requestBody = request_body
478480

479481
if body:
@@ -488,11 +490,12 @@ def parse_parameters(
488490
else:
489491
request_body = RequestBody(content=_content)
490492
model_config = body.Config
491-
if hasattr(model_config, "openapi_extra"):
492-
request_body.description = model_config.openapi_extra.get("description")
493-
request_body.content["application/json"].example = model_config.openapi_extra.get("example")
494-
request_body.content["application/json"].examples = model_config.openapi_extra.get("examples")
495-
request_body.content["application/json"].encoding = model_config.openapi_extra.get("encoding")
493+
openapi_extra: dict = getattr(model_config, "openapi_extra", None) # type: ignore
494+
if openapi_extra:
495+
request_body.description = openapi_extra.get("description")
496+
request_body.content["application/json"].example = openapi_extra.get("example")
497+
request_body.content["application/json"].examples = openapi_extra.get("examples")
498+
request_body.content["application/json"].encoding = openapi_extra.get("encoding")
496499
operation.requestBody = request_body
497500

498501
# Set the parsed parameters in the operation object

0 commit comments

Comments
 (0)