Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better APISpec init to allow to modify it before generating spec_json. #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 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,24 @@ 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.generate_spec_json()

return self.spec_json

def generate_spec_json(self):
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 +251,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 All @@ -272,8 +280,6 @@ def api_doc(self) -> Dict:
}
}

return self.spec_json

def register_api(self, api: APIBlueprint) -> None:
"""
Register an APIBlueprint.
Expand Down
Loading