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

sanic 21.3 support #218

Merged
merged 8 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import os
import sys

from recommonmark.transform import AutoStructify

import sanic_openapi

docs_directory = os.path.dirname(os.path.abspath(__file__))
root_directory = os.path.dirname(docs_directory)
sys.path.insert(0, root_directory)

import sanic_openapi

# -- Project information -----------------------------------------------------

Expand Down Expand Up @@ -58,7 +61,6 @@
html_static_path = ["_static"]
html_css_files = ["css/custom.css"]

from recommonmark.transform import AutoStructify


def setup(app):
Expand Down
4 changes: 2 additions & 2 deletions examples/cars/blueprints/car.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from data import test_car, test_success
from models import Car, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_car, test_success
from models import Car, Status
from sanic_openapi import doc

blueprint = Blueprint('Car', '/car')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars/blueprints/driver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from data import test_driver, test_success
from models import Driver, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_driver, test_success
from models import Driver, Status
from sanic_openapi import doc

blueprint = Blueprint('Driver', '/driver')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars/blueprints/garage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from data import test_garage, test_success
from models import Car, Garage, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_garage, test_success
from models import Car, Garage, Status
from sanic_openapi import doc

blueprint = Blueprint('Garage', '/garage')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars/blueprints/manufacturer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from data import test_manufacturer, test_success
from models import Driver, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_manufacturer, test_success
from models import Driver, Status
from sanic_openapi import doc

blueprint = Blueprint('Manufacturer', '/manufacturer')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars/blueprints/repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from sanic.response import json
from sanic.views import HTTPMethodView

from data import test_station
from models import Station
from sanic_openapi import doc

blueprint = Blueprint('Repair', '/repair')
from data import test_station
from models import Station


class RepairStation(HTTPMethodView):
Expand Down
4 changes: 2 additions & 2 deletions examples/cars/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from sanic import Sanic

from blueprints.car import blueprint as car_blueprint
from blueprints.driver import blueprint as driver_blueprint
from blueprints.garage import blueprint as garage_blueprint
from blueprints.manufacturer import blueprint as manufacturer_blueprint
from blueprints.repair import blueprint as repair_blueprint
from sanic import Sanic

from sanic_openapi import swagger_blueprint

app = Sanic("Cars API example")
Expand Down
4 changes: 2 additions & 2 deletions examples/cars_oas3/blueprints/car.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from data import test_car, test_success
from models import Car, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_car, test_success
from models import Car, Status
from sanic_openapi import openapi

blueprint = Blueprint('Car', '/car')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars_oas3/blueprints/driver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from data import test_driver, test_success
from models import Driver, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_driver, test_success
from models import Driver, Status
from sanic_openapi import openapi

blueprint = Blueprint('Driver', '/driver')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars_oas3/blueprints/garage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from data import test_garage, test_success
from models import Car, Garage, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_garage, test_success
from models import Car, Garage, Status
from sanic_openapi import openapi

blueprint = Blueprint('Garage', '/garage')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars_oas3/blueprints/manufacturer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from data import test_manufacturer, test_success
from models import Driver, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from data import test_manufacturer, test_success
from models import Driver, Status
from sanic_openapi import openapi

blueprint = Blueprint('Manufacturer', '/manufacturer')
Expand Down
4 changes: 2 additions & 2 deletions examples/cars_oas3/blueprints/repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from sanic.response import json
from sanic.views import HTTPMethodView

from data import test_station
from models import Station
from sanic_openapi import openapi

blueprint = Blueprint('Repair', '/repair')
from data import test_station
from models import Station


class RepairStation(HTTPMethodView):
Expand Down
4 changes: 2 additions & 2 deletions examples/cars_oas3/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from sanic import Sanic

from blueprints.car import blueprint as car_blueprint
from blueprints.driver import blueprint as driver_blueprint
from blueprints.garage import blueprint as garage_blueprint
from blueprints.manufacturer import blueprint as manufacturer_blueprint
from blueprints.repair import blueprint as repair_blueprint
from sanic import Sanic

from sanic_openapi import openapi3_blueprint

app = Sanic("Cars API example")
Expand Down
2 changes: 1 addition & 1 deletion examples/class_based_view/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from blueprint import blueprint
from sanic import Sanic
from sanic.response import json

from blueprint import blueprint
from sanic_openapi import doc, swagger_blueprint

app = Sanic("Class Based View example")
Expand Down
14 changes: 10 additions & 4 deletions sanic_openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from .openapi3 import openapi3_blueprint, openapi
from .openapi2 import openapi2_blueprint, doc
from .openapi2 import doc, openapi2_blueprint
from .openapi3 import openapi, openapi3_blueprint

swagger_blueprint = openapi2_blueprint

__version__ = "0.6.2"
__all__ = ["openapi2_blueprint", "swagger_blueprint", "openapi3_blueprint", "openapi", "doc"]
__version__ = "21.3.0"
__all__ = [
"openapi2_blueprint",
"swagger_blueprint",
"openapi3_blueprint",
"openapi",
"doc",
]
3 changes: 1 addition & 2 deletions sanic_openapi/openapi2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .blueprint import blueprint_factory


openapi2_blueprint = blueprint_factory()
openapi2_blueprint = blueprint_factory()
7 changes: 4 additions & 3 deletions sanic_openapi/openapi2/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def build_spec(app, loop):

paths = {}

for uri, route_name, route_parameters, method_handlers in get_all_routes(app, swagger_blueprint.url_prefix):
for (uri, route_name, route_parameters, method_handlers) in get_all_routes(app, swagger_blueprint.url_prefix):

# --------------------------------------------------------------- #
# Methods
Expand All @@ -57,7 +57,7 @@ def build_spec(app, loop):

route_spec = route_specs.get(_handler) or RouteSpec()

if _method == "OPTIONS" or route_spec.exclude:
if route_spec.exclude:
continue

api_consumes_content_types = getattr(app.config, "API_CONSUMES_CONTENT_TYPES", ["application/json"])
Expand Down Expand Up @@ -152,7 +152,8 @@ def build_spec(app, loop):
}
)

# otherwise, update with anything parsed from the docstrings yaml
# otherwise, update with anything parsed from the
# docstrings yaml
endpoint.update(autodoc_endpoint)

methods[_method.lower()] = endpoint
Expand Down
8 changes: 6 additions & 2 deletions sanic_openapi/openapi3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
isort:skip_file
"""

from collections import defaultdict

from .builders import OperationBuilder, SpecificationBuilder
Expand All @@ -8,7 +12,7 @@
operations = defaultdict(OperationBuilder)
specification = SpecificationBuilder()

from .blueprint import blueprint_factory
from .blueprint import blueprint_factory # noqa


openapi3_blueprint = blueprint_factory()
openapi3_blueprint = blueprint_factory()
10 changes: 9 additions & 1 deletion sanic_openapi/openapi3/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from ..utils import get_all_routes, get_blueprinted_routes
from . import operations, specification

DEFAULT_SWAGGER_UI_CONFIG = {"apisSorter": "alpha", "operationsSorter": "alpha"}


def blueprint_factory():
oas3_blueprint = Blueprint("openapi", url_prefix="/swagger")
Expand All @@ -27,7 +29,13 @@ def spec(request):

@oas3_blueprint.route("/swagger-config")
def config(request):
return json(getattr(request.app.config, "SWAGGER_UI_CONFIGURATION", {}))
return json(
getattr(
request.app.config,
"SWAGGER_UI_CONFIGURATION",
DEFAULT_SWAGGER_UI_CONFIG,
)
)

@oas3_blueprint.listener("before_server_start")
def build_spec(app, loop):
Expand Down
2 changes: 1 addition & 1 deletion sanic_openapi/openapi3/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def build(self) -> OpenAPI:
paths = self._build_paths()
tags = self._build_tags()

url_servers = getattr(self, "_urls", None)
url_servers = getattr(self, "_urls", None)
servers = []
if url_servers is not None:
for url_server in url_servers:
Expand Down
2 changes: 1 addition & 1 deletion sanic_openapi/openapi3/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any

from . import operations
from .types import (
from .types import ( # noqa
Array,
Binary,
Boolean,
Expand Down
Loading