Skip to content

Commit 70efb8c

Browse files
authored
Merge pull request #131 from DStape/support-apispec-1.0.0
Support apispec 1.0.0
2 parents 535fc9d + 6e5be13 commit 70efb8c

File tree

7 files changed

+27
-23
lines changed

7 files changed

+27
-23
lines changed

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ language: python
22
cache: pip
33
sudo: false
44
env:
5-
- APISPEC_VERSION="==0.39.0"
6-
- APISPEC_VERSION=""
75
- MARSHMALLOW_VERSION="==2.13.0"
86
- MARSHMALLOW_VERSION=""
97
python:
@@ -15,7 +13,6 @@ before_install:
1513
install:
1614
- travis_retry pip install -U .
1715
- travis_retry pip install -U -r dev-requirements.txt
18-
- travis_retry pip install -U --pre apispec"$APISPEC_VERSION"
1916
- travis_retry pip install -U --pre marshmallow"$MARSHMALLOW_VERSION"
2017
before_script:
2118
- flake8 flask_apispec
@@ -29,7 +26,7 @@ jobs:
2926
env: []
3027
# Override install, and script to no-ops
3128
before_install: true
32-
install:
29+
install:
3330
- travis_retry pip install -U .
3431
- travis_retry pip install -r dev-requirements.txt
3532
before_script: true

flask_apispec/apidoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ def convert(self, target, endpoint=None, blueprint=None, **kwargs):
3939
def get_path(self, rule, target, **kwargs):
4040
operations = self.get_operations(rule, target)
4141
parent = self.get_parent(target, **kwargs)
42+
valid_methods = VALID_METHODS[self.spec.openapi_version.major]
4243
return {
4344
'view': target,
4445
'path': rule_to_path(rule),
4546
'operations': {
4647
method.lower(): self.get_operation(rule, view, parent=parent)
4748
for method, view in six.iteritems(operations)
48-
if method.lower() in (set(VALID_METHODS) - {'head'})
49+
if method.lower() in (set(valid_methods) - {'head'})
4950
},
5051
}
5152

@@ -84,7 +85,6 @@ def get_parameters(self, rule, view, docs, parent=None):
8485
locations = options.pop('locations', None)
8586
if locations:
8687
options['default_in'] = locations[0]
87-
options['spec'] = self.app.config.get('APISPEC_SPEC', None)
8888

8989
rule_params = rule_to_params(rule, docs.get('params')) or []
9090
extra_params = converter(schema, **options) if args else []

flask_apispec/extension.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class FlaskApiSpec(object):
2121
'APISPEC_SPEC': APISpec(
2222
title='pets',
2323
version='v1',
24+
openapi_version='2.0',
2425
plugins=[MarshmallowPlugin()],
2526
),
2627
'APISPEC_SWAGGER_URL': '/swagger/',
@@ -51,7 +52,8 @@ def init_app(self, app):
5152
self.app = app
5253
self.spec = self.app.config.get('APISPEC_SPEC') or \
5354
make_apispec(self.app.config.get('APISPEC_TITLE', 'flask-apispec'),
54-
self.app.config.get('APISPEC_VERSION', 'v1'))
55+
self.app.config.get('APISPEC_VERSION', 'v1'),
56+
self.app.config.get('APISPEC_OAS_VERSION', '2.0'))
5557
self.add_swagger_routes()
5658
self.resource_converter = ResourceConverter(self.app, spec=self.spec)
5759
self.view_converter = ViewConverter(app=self.app, spec=self.spec)
@@ -143,12 +145,13 @@ def _register(self, target, endpoint=None, blueprint=None,
143145
else:
144146
raise TypeError()
145147
for path in paths:
146-
self.spec.add_path(**path)
148+
self.spec.path(**path)
147149

148150

149-
def make_apispec(title='flask-apispec', version='v1'):
151+
def make_apispec(title='flask-apispec', version='v1', openapi_version='2.0'):
150152
return APISpec(
151153
title=title,
152154
version=version,
155+
openapi_version=openapi_version,
153156
plugins=[MarshmallowPlugin()],
154157
)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ universal=1
1010
# E301: expected 1 blank line, found 1
1111
# E302: expected 2 blank lines, found 0
1212
[flake8]
13-
ignore = E127,E128,E265,E301,E302
13+
ignore = E127,E128,E265,E301,E302,W504
1414
max-line-length = 90

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'flask>=0.10.1',
1010
'marshmallow>=2.0.0',
1111
'webargs>=0.18.0',
12-
'apispec>=0.17.0',
12+
'apispec>=1.0.0',
1313
]
1414

1515
def find_version(fname):

tests/test_extension.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ def test_serve_swagger_ui_custom_url(self, app, client):
9292
def test_apispec_config(self, app):
9393
app.config['APISPEC_TITLE'] = 'test-extension'
9494
app.config['APISPEC_VERSION'] = '2.1'
95+
app.config['APISPEC_OAS_VERSION'] = '2.0'
9596
docs = FlaskApiSpec(app)
9697

97-
assert docs.spec.info == {
98-
'title': 'test-extension',
99-
'version': '2.1',
100-
}
98+
assert docs.spec.title == 'test-extension'
99+
assert docs.spec.version == '2.1'
100+
assert docs.spec.openapi_version == '2.0'

tests/test_openapi.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@ def spec(marshmallow_plugin):
2020
return APISpec(
2121
title='title',
2222
version='v1',
23+
openapi_version='2.0',
2324
plugins=[marshmallow_plugin],
2425
)
2526

2627
@pytest.fixture()
2728
def openapi(marshmallow_plugin):
2829
return marshmallow_plugin.openapi
2930

31+
def ref_path(spec):
32+
if spec.openapi_version.version[0] < 3:
33+
return "#/definitions/"
34+
return "#/components/schemas/"
3035

3136
def test_error_if_spec_does_not_have_marshmallow_plugin(app):
3237
bad_spec = APISpec(
3338
title='title',
3439
version='v1',
40+
openapi_version='2.0',
3541
plugins=[], # oh no! no MarshmallowPlugin
3642
)
3743
with pytest.raises(RuntimeError):
@@ -57,7 +63,7 @@ def path(self, app, spec, function_view):
5763
converter = ViewConverter(app=app, spec=spec)
5864
paths = converter.convert(function_view)
5965
for path in paths:
60-
spec.add_path(**path)
66+
spec.path(**path)
6167
return spec._paths['/bands/{band_id}/']
6268

6369
def test_params(self, app, path):
@@ -77,8 +83,7 @@ def test_params(self, app, path):
7783
def test_responses(self, schemas, path, openapi):
7884
response = path['get']['responses']['default']
7985
assert response['description'] == 'a band'
80-
expected = openapi.schema2jsonschema(schemas.BandSchema)
81-
assert response['schema'] == expected
86+
assert response['schema'] == {'$ref': ref_path(openapi.spec) + 'Band'}
8287

8388
def test_tags(self, path):
8489
assert path['get']['tags'] == ['band']
@@ -101,7 +106,7 @@ def path(self, app, spec, function_view):
101106
converter = ViewConverter(app=app, spec=spec)
102107
paths = converter.convert(function_view)
103108
for path in paths:
104-
spec.add_path(**path)
109+
spec.path(**path)
105110
return spec._paths['/bands/{band_id}/']
106111

107112
def test_params(self, app, path, openapi):
@@ -145,7 +150,7 @@ def path(self, app, spec, function_view):
145150
converter = ViewConverter(app=app, spec=spec)
146151
paths = converter.convert(function_view)
147152
for path in paths:
148-
spec.add_path(**path)
153+
spec.path(**path)
149154
return spec._paths['/bands/{band_id}/']
150155

151156
def test_responses(self, schemas, path):
@@ -172,7 +177,7 @@ def path(self, app, spec, resource_view):
172177
converter = ResourceConverter(app=app, spec=spec)
173178
paths = converter.convert(resource_view, endpoint='band')
174179
for path in paths:
175-
spec.add_path(**path)
180+
spec.path(**path)
176181
return spec._paths['/bands/{band_id}/']
177182

178183
def test_params(self, app, path, openapi):
@@ -188,8 +193,7 @@ def test_params(self, app, path, openapi):
188193
def test_responses(self, schemas, path, openapi):
189194
response = path['get']['responses']['default']
190195
assert response['description'] == 'a band'
191-
expected = openapi.schema2jsonschema(schemas.BandSchema)
192-
assert response['schema'] == expected
196+
assert response['schema'] == {'$ref': ref_path(openapi.spec) + 'Band'}
193197

194198
def test_tags(self, path):
195199
assert path['get']['tags'] == ['band']

0 commit comments

Comments
 (0)