Skip to content

Commit

Permalink
feat: fix for class based routing
Browse files Browse the repository at this point in the history
  • Loading branch information
fMeow committed May 10, 2018
1 parent 7b5cbf1 commit de21965
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions sanic_openapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sanic.blueprints import Blueprint
from sanic.response import json
from sanic.views import CompositionView
from sanic.constants import HTTP_METHODS

from .doc import route_specs, RouteSpec, serialize_schema, definitions

Expand Down Expand Up @@ -47,10 +48,21 @@ def build_spec(app, loop):
for blueprint in app.blueprints.values():
if hasattr(blueprint, 'routes'):
for route in blueprint.routes:
route_spec = route_specs[route.handler]
route_spec.blueprint = blueprint
if not route_spec.tags:
route_spec.tags.append(blueprint.name)
if hasattr(route.handler, 'view_class'):
# class based view
view = route.handler.view_class
for http_method in HTTP_METHODS:
_handler = getattr(view, http_method.lower(), None)
if _handler:
route_spec = route_specs[_handler]
route_spec.blueprint = blueprint
if not route_spec.tags:
route_spec.tags.append(blueprint.name)
else:
route_spec = route_specs[route.handler]
route_spec.blueprint = blueprint
if not route_spec.tags:
route_spec.tags.append(blueprint.name)

paths = {}
for uri, route in app.router.routes_all.items():
Expand All @@ -73,7 +85,11 @@ def build_spec(app, loop):

methods = {}
for _method, _handler in method_handlers:
route_spec = route_specs.get(_handler) or RouteSpec()
if hasattr(_handler, 'view_class'):
view_handler = getattr(_handler.view_class, _method.lower())
route_spec = route_specs.get(view_handler) or RouteSpec()
else:
route_spec = route_specs.get(_handler) or RouteSpec()

if _method == 'OPTIONS' or route_spec.exclude:
continue
Expand Down

0 comments on commit de21965

Please sign in to comment.