Skip to content

Commit

Permalink
Fix UserWarning (use instance.ctx._spec instead with sanic >= 21.3.0) (
Browse files Browse the repository at this point in the history
  • Loading branch information
comic31 committed May 11, 2021
1 parent 0328f51 commit c4ba3d6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sanic_openapi/openapi2/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import inspect
from distutils.version import LooseVersion
from os.path import abspath, dirname, realpath

from sanic import __version__ as sanic_version
from sanic.blueprints import Blueprint
from sanic.response import json, redirect

Expand All @@ -9,6 +11,9 @@
from .doc import RouteSpec, definitions, route_specs, serialize_schema
from .spec import Spec as Swagger2Spec

SANIC_VERSION = LooseVersion(sanic_version)
SANIC_21_3_0 = LooseVersion("21.3.0")


def blueprint_factory():
swagger_blueprint = Blueprint("swagger", url_prefix="/swagger")
Expand All @@ -26,7 +31,11 @@ def index(request):

@swagger_blueprint.route("/swagger.json")
def spec(request):
return json(swagger_blueprint._spec.as_dict)

if SANIC_VERSION >= SANIC_21_3_0:
return json(swagger_blueprint.ctx._spec.as_dict)
else:
return json(swagger_blueprint._spec.as_dict)

@swagger_blueprint.route("/swagger-config")
def config(request):
Expand Down Expand Up @@ -181,6 +190,10 @@ def build_spec(app, loop):
_spec.add_tags(tags=[{"name": name} for name in tags])

_spec.add_paths(paths)
swagger_blueprint._spec = _spec

if SANIC_VERSION >= SANIC_21_3_0:
swagger_blueprint.ctx._spec = _spec
else:
swagger_blueprint._spec = _spec

return swagger_blueprint

0 comments on commit c4ba3d6

Please sign in to comment.