Skip to content

Commit

Permalink
Fix path parameters description for OpenAPIv3
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Nov 18, 2018
1 parent f9da243 commit 655e3f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 11 additions & 2 deletions flask_rest_api/spec/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class FlaskPlugin(BasePlugin):
def __init__(self):
super().__init__()
self.converter_mapping = dict(DEFAULT_CONVERTER_MAPPING)
self.openapi_version = None

def init_spec(self, spec):
super().init_spec(spec)
self.openapi_version = spec.openapi_version

# From apispec
@staticmethod
Expand Down Expand Up @@ -66,9 +71,13 @@ def rule_to_params(self, rule):
}
type_, format_ = self.converter_mapping.get(
type(rule._converters[argument]), DEFAULT_TYPE)
param['type'] = type_
schema = {'type': type_}
if format_ is not None:
param['format'] = format_
schema['format'] = format_
if self.openapi_version.major < 3:
param.update(schema)
else:
param['schema'] = schema
params.append(param)
return params

Expand Down
16 changes: 12 additions & 4 deletions tests/test_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def func(document, query_args):
}

@pytest.mark.parametrize('openapi_version', ('2.0', '3.0.1'))
def test_blueprint_path_parameters(self, app, schemas, openapi_version):
def test_blueprint_path_parameters(self, app, openapi_version):
"""Check auto and manual param docs are merged"""
app.config['OPENAPI_VERSION'] = openapi_version
api = Api(app)
Expand All @@ -181,9 +181,17 @@ def get(item_id):
spec = api.spec.to_dict()
params = spec['paths']['/test/{item_id}']['get']['parameters']
assert len(params) == 1
assert params == [{
'name': 'item_id', 'required': True, 'description': 'Item ID',
'format': 'int32', 'type': 'integer', 'in': 'path'}]
if openapi_version == '2.0':
assert params == [{
'name': 'item_id', 'in': 'path', 'required': True,
'description': 'Item ID',
'format': 'int32', 'type': 'integer'}]
else:
assert params == [{
'name': 'item_id', 'in': 'path', 'required': True,
'description': 'Item ID',
'schema': {'format': 'int32', 'type': 'integer'}
}]

@pytest.mark.parametrize('openapi_version', ['2.0', '3.0.1'])
def test_blueprint_response_schema(self, app, openapi_version, schemas):
Expand Down

0 comments on commit 655e3f0

Please sign in to comment.