Skip to content

Commit

Permalink
Support apispec 1.0.0b2
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Sep 12, 2018
1 parent 16ab77b commit 38a8d69
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
7 changes: 6 additions & 1 deletion flask_rest_api/spec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from flask import current_app
import apispec

from .plugins import FlaskPlugin, MarshmallowPlugin
from .plugins import FlaskPlugin
from flask_rest_api.compat import APISPEC_VERSION_MAJOR
if APISPEC_VERSION_MAJOR == 0:
from .plugins import MarshmallowPlugin
else:
from apispec.ext.marshmallow import MarshmallowPlugin


def _add_leading_slash(string):
Expand Down
46 changes: 33 additions & 13 deletions flask_rest_api/spec/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
Heavily copied from apispec
"""

import re
from urllib.parse import urljoin

import werkzeug.routing

from apispec import Path
from apispec import BasePlugin
from apispec.exceptions import PluginMethodNotImplementedError
from apispec.ext import flask as aef
from apispec.ext import marshmallow as aem

from flask_rest_api.compat import APISPEC_VERSION_MAJOR
if APISPEC_VERSION_MAJOR == 0:
from apispec import Path
from apispec.ext import marshmallow as aem


# from flask-restplus
RE_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')

# From flask-apispec
DEFAULT_CONVERTER_MAPPING = {
Expand All @@ -22,13 +29,22 @@
DEFAULT_TYPE = ('string', None)


class FlaskPlugin(aef.FlaskPlugin):
class FlaskPlugin(BasePlugin):
"""Plugin to create OpenAPI paths from Flask rules"""

def __init__(self):
super().__init__()
self.converter_mapping = dict(DEFAULT_CONVERTER_MAPPING)

# From apispec
@staticmethod
def flaskpath2openapi(path):
"""Convert a Flask URL rule to an OpenAPI-compliant path.
:param str path: Flask path template.
"""
return RE_URL.sub(r'{\1}', path)

def register_converter(self, converter, conv_type, conv_format=None):
"""Register custom path parameter converter
Expand Down Expand Up @@ -59,7 +75,7 @@ def rule_to_params(self, rule):

# Greatly inspired by apispec
def path_helper(self, app, rule, operations=None, **kwargs):
"""Make Path from flask Rule"""
"""Get path from flask Rule and set path parameters in operations"""
path = self.flaskpath2openapi(rule.rule)
app_root = app.config['APPLICATION_ROOT'] or '/'
path = urljoin(app_root.rstrip('/') + '/', path.lstrip('/'))
Expand All @@ -74,15 +90,19 @@ def path_helper(self, app, rule, operations=None, **kwargs):
for path_p in path_parameters:
parameters.append(path_p)

return Path(path=path, operations=operations)
if APISPEC_VERSION_MAJOR == 0:
return Path(path=path, operations=operations)
return path


class MarshmallowPlugin(aem.MarshmallowPlugin):
"""Plugin introspecting marshmallow schemas"""
if APISPEC_VERSION_MAJOR == 0:
# This is not needed in apispec 1.0.0
class MarshmallowPlugin(aem.MarshmallowPlugin):
"""Plugin introspecting marshmallow schemas"""

def path_helper(self, *args, **kwargs):
"""No-op path helper
def path_helper(self, *args, **kwargs):
"""No-op path helper
apispec's path helper parses YAML docstring. We don't need this.
"""
raise PluginMethodNotImplementedError
apispec's path helper parses YAML docstring. We don't need this.
"""
raise PluginMethodNotImplementedError
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
'marshmallow>=2.6.0',
'python-dateutil>=2.5.0',
'webargs>=1.5.2',
'apispec>=0.39.0, <1.0.0a0',
'apispec>=0.39.0',
],
)

0 comments on commit 38a8d69

Please sign in to comment.