Skip to content

Commit

Permalink
Merge e43cfc0 into 37ee864
Browse files Browse the repository at this point in the history
  • Loading branch information
strongbugman committed Jan 7, 2019
2 parents 37ee864 + e43cfc0 commit 328538d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
20 changes: 16 additions & 4 deletions examples/parsed_view_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ def get(self):
"Swagger.SCHEMA_LOCATIONS" for more locations
"""
return jsonify(
[{'name': 'test', 'id': 1, 'type': request.parsed_data['args']['type']},
{'name': 'test2', 'id': 2, 'type': request.parsed_data['args']['type']}])
[{'name': 'test', 'id': 1,
'type': request.parsed_data['args']['type']},
{'name': 'test2', 'id': 2,
'type': request.parsed_data['args']['type']}])

def post(self):
return jsonify(
Expand All @@ -53,7 +55,8 @@ def get(self, id):

def put(self, id):
return jsonify(
{'name': request.parsed_data['json']['name'], 'id': 3, 'type': 'NORMAL'})
{'name': request.parsed_data['json']['name'],
'id': 3, 'type': 'NORMAL'})


class EmptyView(MethodView):
Expand Down Expand Up @@ -90,6 +93,11 @@ def users(group):
type: integer
name:
type: string
tags:
type: array
minItems: 1
items:
type: integer
definitions:
User:
type: object
Expand Down Expand Up @@ -162,7 +170,11 @@ def test_swag(client, specs_data):
assert res.status_code == 400
res = client.post(
'/api/users/1/',
json={'data': {'name': 'test', 'age': 20}})
json={'data': {'name': 'test', 'age': 20}, 'tags': ['error_tag']})
assert res.status_code == 400
res = client.post(
'/api/users/1/',
json={'data': {'name': 'test', 'age': 20}, 'tags': [1, 2]})
assert res.status_code == 200


Expand Down
23 changes: 9 additions & 14 deletions flasgger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Swagger(object):
}

SCHEMA_TYPES = {'string': str, 'integer': int, 'number': int,
'boolean': bool, 'object': dict}
'boolean': bool}
SCHEMA_LOCATIONS = {'query': 'args', 'header': 'headers',
'formData': 'form', 'body': 'json', 'path': 'path'}

Expand Down Expand Up @@ -536,6 +536,10 @@ def after_request(response): # noqa
def parse_request(self, app):
@app.before_request
def before_request(): # noqa
"""
Parse and validate request data(query, form, header and body),
set data to `request.parsed_data`
"""
# convert "/api/items/<int:id>/" to "/api/items/{id}/"
subs = []
for sub in str(request.url_rule).split('/'):
Expand Down Expand Up @@ -571,19 +575,8 @@ def before_request(): # noqa
)
for param in doc['parameters']:
location = self.SCHEMA_LOCATIONS[param['in']]
if location == 'json':
schemas[location]['properties'].update(
param['schema']['properties'])

required_keys = param['schema'].get('required', [])
keys = param['schema']['properties']
for key in keys:
parsers[location].add_argument(
key,
type=self.SCHEMA_TYPES[
keys[key]['type']],
required=key in required_keys, location='json',
store_missing=False)
if location == 'json': # load data from 'request.json'
schemas[location] = param['schema']
else:
name = param['name']
if location != 'path':
Expand All @@ -607,6 +600,8 @@ def before_request(): # noqa
parsed_data = {'path': request.view_args}
for location in parsers.keys():
parsed_data[location] = parsers[location].parse_args()
if 'json' in schemas:
parsed_data['json'] = request.json or {}
for location, data in parsed_data.items():
try:
jsonschema.validate(
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apispec-webframeworks
flask-restful
pep8==1.5.7
flake8==2.4.1
pytest>=3.0.7
pytest>3.6
flex
coveralls
pytest-cov
Expand Down

0 comments on commit 328538d

Please sign in to comment.