diff --git a/setup.py b/setup.py index 24f9a76..11477c4 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ def get_packages(package): 'starlette<1', 'apispec<4', ], - version='0.0.12', + version='0.0.13', url='https://github.com/slv0/start_resty', license='BSD', description='The web framework', diff --git a/star_resty/apidocs/route.py b/star_resty/apidocs/route.py index 5a3ba45..9d0f067 100644 --- a/star_resty/apidocs/route.py +++ b/star_resty/apidocs/route.py @@ -1,7 +1,7 @@ -from typing import Sequence, Union +from typing import Sequence from apispec import APISpec -from starlette.routing import Route, Mount +from starlette.routing import BaseRoute, Mount, Route from .operation import setup_route_operations from .utils import convert_path @@ -9,7 +9,7 @@ __all__ = ('setup_routes',) -def setup_routes(routes: Sequence[Union[Route, Mount]], +def setup_routes(routes: Sequence[BaseRoute], spec: APISpec, version: int = 2, add_head_methods: bool = False, path: str = ''): @@ -18,14 +18,15 @@ def setup_routes(routes: Sequence[Union[Route, Mount]], setup_routes(route.routes, spec, version=version, add_head_methods=add_head_methods, path=f'{path}{route.path}') continue - elif isinstance(route, Route) and not route.include_in_schema: - continue + elif isinstance(route, Route): + if not route.include_in_schema: + continue - endpoint = getattr(route.endpoint, '__endpoint__', None) - if endpoint is None: - continue + endpoint = getattr(route.endpoint, '__endpoint__', None) + if endpoint is None: + continue - operations = setup_route_operations(route, endpoint, version=version, - add_head_methods=add_head_methods) - route_path = f'{path}{route.path}' - spec.path(convert_path(route_path), operations=operations) + operations = setup_route_operations(route, endpoint, version=version, + add_head_methods=add_head_methods) + route_path = f'{path}{route.path}' + spec.path(convert_path(route_path), operations=operations) diff --git a/star_resty/apidocs/setup.py b/star_resty/apidocs/setup.py index 18d90b0..3a80b26 100644 --- a/star_resty/apidocs/setup.py +++ b/star_resty/apidocs/setup.py @@ -5,7 +5,7 @@ from apispec.ext.marshmallow import MarshmallowPlugin from starlette.applications import Starlette from starlette.requests import Request -from starlette.responses import UJSONResponse +from starlette.responses import JSONResponse from .route import setup_routes from .utils import resolve_schema_name @@ -35,19 +35,19 @@ def setup_spec(app: Starlette, title: str, plugins=[MarshmallowPlugin(schema_name_resolver=resolve_schema_name)], **{'swagger': openapi_version, 'basePath': base_path, **options, **kwargs} ) - initialized = False + api_spec = None @app.route(route, include_in_schema=False) def generate_api_docs(_: Request): - nonlocal initialized + nonlocal api_spec nonlocal spec - if not initialized: + if api_spec is None: logger.info('initialize open api schema') setup_routes(app.routes, spec, version=get_open_api_version(openapi_version) , add_head_methods=add_head_methods) - initialized = True + api_spec = spec.to_dict() - return UJSONResponse(spec.to_dict()) + return JSONResponse(api_spec) def get_open_api_version(version: str) -> int: