Skip to content

Commit

Permalink
Better APISpec init to allow to modify it before generating spec_json.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddorian committed Nov 12, 2024
1 parent 7883df4 commit 24cb6a9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions flask_openapi3/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def __init__(

# Initialize specification JSON
self.spec_json: Dict = {}
self.spec = APISpec(
openapi=self.openapi_version,
info=self.info,
paths=self.paths
)

def _init_doc(self) -> None:
"""
Expand Down Expand Up @@ -215,21 +220,19 @@ def api_doc(self) -> Dict:
if self.spec_json:
return self.spec_json

spec = APISpec(
openapi=self.openapi_version,
info=self.info,
paths=self.paths
)
self.spec.openapi = self.openapi_version
self.spec.info = self.info
self.spec.paths = self.paths

if self.severs:
spec.servers = self.severs
self.spec.servers = self.severs

if self.external_docs:
spec.externalDocs = self.external_docs
self.spec.externalDocs = self.external_docs

# Set tags
if self.tags:
spec.tags = self.tags
self.spec.tags = self.tags

# Add ValidationErrorModel to components schemas
schema = get_model_schema(self.validation_error_model)
Expand All @@ -243,10 +246,10 @@ def api_doc(self) -> Dict:
# Set components
self.components.schemas = self.components_schemas
self.components.securitySchemes = self.security_schemes
spec.components = self.components
self.spec.components = self.components

# Convert spec to JSON
self.spec_json = spec.model_dump(mode="json", by_alias=True, exclude_unset=True, warnings=False)
self.spec_json = self.spec.model_dump(mode="json", by_alias=True, exclude_unset=True, warnings=False)

# Update with OpenAPI extensions
self.spec_json.update(**self.openapi_extensions)
Expand Down

0 comments on commit 24cb6a9

Please sign in to comment.