Skip to content
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

Fix validation for Body without $ref #185

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion flex/validation/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def construct_parameter_validators(parameter, context):
if 'schema' in parameter:
schema_validators = construct_schema_validators(parameter['schema'], context=context)
for key, value in schema_validators.items():
validators.setdefault(key, value)
# merge validators for those sharing the same key (like 'required') to not omit some
if key in validators:
validators[key] += value
else:
validators.setdefault(key, value)
return validators


Expand Down
50 changes: 50 additions & 0 deletions tests/validation/request/test_request_body_parameter_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,53 @@ def test_request_body_parameter_validation_with_invalid_value(user_post_schema):
MESSAGES['format']['invalid_email'],
err.value.detail,
)


def test_request_body_parameter_validation_invalid_without_ref():
"""
Test validating the request body with a invalid post.
"""
schema = SchemaFactory(
paths={
'/post/': {
POST: {
'consumes': ['application/json'],
'parameters': [
{
'in': BODY,
'name': BODY,
'required': True,
'schema': {
'type': OBJECT,
'required': ['name'],
'properties': {
'name': {
'type': STRING
}
}
}
}
],
'responses': {200: {'description': "Success"}},
}
}
}
)

request = RequestFactory(
url='http://www.example.com/post/',
content_type='application/json',
body=json.dumps({}),
method=POST,
)

with pytest.raises(ValidationError) as err:
validate_request(
request=request,
schema=schema,
)

assert_message_in_errors(
MESSAGES['required']['required'],
err.value.detail,
)