Skip to content

Commit

Permalink
fix json response
Browse files Browse the repository at this point in the history
  • Loading branch information
vvcheremushkin committed Dec 22, 2020
1 parent 3defd49 commit 700a322
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
25 changes: 13 additions & 12 deletions star_resty/apidocs/route.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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

__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 = ''):
Expand All @@ -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)
12 changes: 6 additions & 6 deletions star_resty/apidocs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 700a322

Please sign in to comment.