Skip to content

Commit e9773ca

Browse files
authored
Merge pull request #166 from zzz4zzz/fields2parameters_missing_default_in
Fix `default_in` error when location is not provided
2 parents 1842221 + cf13bd6 commit e9773ca

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

flask_apispec/apidoc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def get_parameters(self, rule, view, docs, parent=None):
9494
locations = options.pop('locations', None)
9595
if locations:
9696
options['default_in'] = locations[0]
97+
else:
98+
options['default_in'] = 'body'
9799
extra_params += converter(schema, **options) if args else []
98100

99101
rule_params = rule_to_params(rule, docs.get('params')) or []

tests/test_openapi.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def test_error_if_spec_does_not_have_marshmallow_plugin(app):
4848
with pytest.raises(RuntimeError):
4949
ResourceConverter(app=app, spec=bad_spec)
5050

51-
5251
class TestFunctionView:
5352

5453
@pytest.fixture
@@ -244,3 +243,80 @@ def test_params(self, app, path):
244243
}] + rule_to_params(rule)
245244
)
246245
assert params == expected
246+
247+
248+
class TestFiledsNoLocationProvided:
249+
250+
@pytest.fixture
251+
def function_view(self, app):
252+
@app.route('/bands/<int:band_id>/')
253+
@use_kwargs({'name': fields.Str(), 'address': fields.Str()})
254+
def get_band(**kwargs):
255+
return kwargs
256+
257+
return get_band
258+
259+
@pytest.fixture
260+
def path(self, app, spec, function_view):
261+
converter = ViewConverter(app=app, spec=spec)
262+
paths = converter.convert(function_view)
263+
for path in paths:
264+
spec.path(**path)
265+
return spec._paths['/bands/{band_id}/']
266+
267+
def test_params(self, app, path):
268+
params = path['get']['parameters']
269+
rule = app.url_map._rules_by_endpoint['get_band'][0]
270+
expected = (
271+
[{
272+
'in': 'body',
273+
'name': 'body',
274+
'required': False,
275+
'schema': {
276+
'properties': {
277+
'address': {
278+
'type': 'string'
279+
},
280+
'name': {
281+
'type': 'string'
282+
}
283+
},
284+
'type': 'object'
285+
},
286+
}] + rule_to_params(rule)
287+
)
288+
assert params == expected
289+
290+
class TestSchemaNoLocationProvided:
291+
292+
@pytest.fixture
293+
def function_view(self, app, models, schemas):
294+
class BodySchema(Schema):
295+
address = fields.Str()
296+
297+
@app.route('/bands/<int:band_id>/')
298+
@use_kwargs(BodySchema)
299+
def get_band(**kwargs):
300+
return kwargs
301+
return get_band
302+
303+
@pytest.fixture
304+
def path(self, app, spec, function_view):
305+
converter = ViewConverter(app=app, spec=spec)
306+
paths = converter.convert(function_view)
307+
for path in paths:
308+
spec.path(**path)
309+
return spec._paths['/bands/{band_id}/']
310+
311+
def test_params(self, app, path):
312+
params = path['get']['parameters']
313+
rule = app.url_map._rules_by_endpoint['get_band'][0]
314+
expected = (
315+
[{
316+
'in': 'body',
317+
'name': 'body',
318+
'required': False,
319+
'schema': {'$ref': '#/definitions/Body'}
320+
}] + rule_to_params(rule)
321+
)
322+
assert params == expected

0 commit comments

Comments
 (0)