-
-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect what the APIs consume #289
Changes from 3 commits
14bfab1
927e8a0
6abd065
91aeb69
58da2b3
1fb776e
e01171a
0d014d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,13 +58,15 @@ def get_val_from_param(value, query_param): | |
return make_type(value, query_param["type"]) | ||
|
||
|
||
def parameter_to_arg(parameters, function): | ||
def parameter_to_arg(parameters, consumes, function): | ||
""" | ||
Pass query and body parameters as keyword arguments to handler function. | ||
|
||
See (https://github.com/zalando/connexion/issues/59) | ||
:param parameters: All the parameters of the handler functions | ||
:type parameters: dict|None | ||
:param consumes: The list of content types the operation consumes | ||
:type consumes: list | ||
:param function: The handler function for the REST endpoint. | ||
:type function: function|None | ||
""" | ||
|
@@ -87,10 +89,13 @@ def parameter_to_arg(parameters, function): | |
def wrapper(*args, **kwargs): | ||
logger.debug('Function Arguments: %s', arguments) | ||
|
||
try: | ||
request_body = flask.request.json | ||
except exceptions.BadRequest: | ||
request_body = None | ||
if 'application/json' in consumes: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the right check. There are lots of "+json" mimetypes that have to be parsed as json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
try: | ||
request_body = flask.request.json | ||
except exceptions.BadRequest: | ||
request_body = None | ||
else: | ||
request_body = flask.request.data | ||
|
||
if default_body and not request_body: | ||
request_body = default_body | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,12 +97,14 @@ def validate_parameter_list(parameter_type, request_params, spec_params): | |
|
||
|
||
class RequestBodyValidator(object): | ||
def __init__(self, schema, is_null_value_valid=False): | ||
def __init__(self, schema, consumes, is_null_value_valid=False): | ||
""" | ||
:param schema: The schema of the request body | ||
:param consumes: The list of content types the operation consumes | ||
:param is_nullable: Flag to indicate if null is accepted as valid value. | ||
""" | ||
self.schema = schema | ||
self.consumes = consumes | ||
self.has_default = schema.get('default', False) | ||
self.is_null_value_valid = is_null_value_valid | ||
|
||
|
@@ -114,6 +116,9 @@ def __call__(self, function): | |
|
||
@functools.wraps(function) | ||
def wrapper(*args, **kwargs): | ||
if 'application/json' not in self.consumes: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again the wrong check. And I would put the validation logic inside the if to avoid repeating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return function(*args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? We should have a test that covers this scenario. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is. Without it validation fails on the plain text request. Added a test. |
||
|
||
data = flask.request.json | ||
|
||
logger.debug("%s validating schema...", flask.request.url) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please lets not duplicate comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, what exactly do you mean? The comment relates to the
consumes
param yet it's almost similar to theproduces
. Should I rewrite it somehow or just remove?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its ok.