Skip to content

Commit f4ca080

Browse files
zzz4zzzSahil Jolly
authored andcommitted
Fix default_in error when location is not provided
1 parent f06ea98 commit f4ca080

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
@@ -98,6 +98,8 @@ def get_parameters(self, rule, view, docs, parent=None):
9898
locations = options.pop('locations', None)
9999
if locations:
100100
options['default_in'] = locations[0]
101+
else:
102+
options['default_in'] = 'body'
101103
extra_params += converter(schema, **options) if args else []
102104

103105
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
@@ -57,7 +57,6 @@ def test_error_if_spec_does_not_have_marshmallow_plugin(app):
5757
with pytest.raises(RuntimeError):
5858
ResourceConverter(app=app, spec=bad_spec)
5959

60-
6160
class TestFunctionView:
6261

6362
@pytest.fixture
@@ -298,3 +297,80 @@ def test_params(self, app, path):
298297
}] + rule_to_params(rule)
299298
)
300299
assert params == expected
300+
301+
302+
class TestFiledsNoLocationProvided:
303+
304+
@pytest.fixture
305+
def function_view(self, app):
306+
@app.route('/bands/<int:band_id>/')
307+
@use_kwargs({'name': fields.Str(), 'address': fields.Str()})
308+
def get_band(**kwargs):
309+
return kwargs
310+
311+
return get_band
312+
313+
@pytest.fixture
314+
def path(self, app, spec, function_view):
315+
converter = ViewConverter(app=app, spec=spec)
316+
paths = converter.convert(function_view)
317+
for path in paths:
318+
spec.path(**path)
319+
return spec._paths['/bands/{band_id}/']
320+
321+
def test_params(self, app, path):
322+
params = path['get']['parameters']
323+
rule = app.url_map._rules_by_endpoint['get_band'][0]
324+
expected = (
325+
[{
326+
'in': 'body',
327+
'name': 'body',
328+
'required': False,
329+
'schema': {
330+
'properties': {
331+
'address': {
332+
'type': 'string'
333+
},
334+
'name': {
335+
'type': 'string'
336+
}
337+
},
338+
'type': 'object'
339+
},
340+
}] + rule_to_params(rule)
341+
)
342+
assert params == expected
343+
344+
class TestSchemaNoLocationProvided:
345+
346+
@pytest.fixture
347+
def function_view(self, app, models, schemas):
348+
class BodySchema(Schema):
349+
address = fields.Str()
350+
351+
@app.route('/bands/<int:band_id>/')
352+
@use_kwargs(BodySchema)
353+
def get_band(**kwargs):
354+
return kwargs
355+
return get_band
356+
357+
@pytest.fixture
358+
def path(self, app, spec, function_view):
359+
converter = ViewConverter(app=app, spec=spec)
360+
paths = converter.convert(function_view)
361+
for path in paths:
362+
spec.path(**path)
363+
return spec._paths['/bands/{band_id}/']
364+
365+
def test_params(self, app, path):
366+
params = path['get']['parameters']
367+
rule = app.url_map._rules_by_endpoint['get_band'][0]
368+
expected = (
369+
[{
370+
'in': 'body',
371+
'name': 'body',
372+
'required': False,
373+
'schema': {'$ref': '#/definitions/Body'}
374+
}] + rule_to_params(rule)
375+
)
376+
assert params == expected

0 commit comments

Comments
 (0)