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

Correctly validate 204 responses #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tristanoneil
Copy link

This fix should resolve #101 in that it'll return true instead of nil when validation passes. This also ensures that an error is not raised when in strict mode when the status code is 204 and the content type is nil since that should be the expected behavior.

@@ -21,9 +21,11 @@ def validate(response_body, response_validate_options)

media_type = select_media_type(response_body.content_type)
unless media_type
raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference if response_validate_options.strict
if response_validate_options.strict && response_body.status_code != 204

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more universal solution would be to not do content-type validation if the response body is empty, regardless of what the status code is, e.g.

Suggested change
if response_validate_options.strict && response_body.status_code != 204
if response_validate_options.strict && response_body_not_blank

This is conceptually similar to the fix used in https://github.com/openapi-library/OpenAPIValidators/pull/59/files

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good call! I fixed up 69c8c9a.

Tristan O'Neil added 2 commits March 30, 2022 19:47
Empty response bodies (204 responses for instance) should not have a
content type and therefore an error should not be raised in strict mode
when the response body is empty and the content type is nil as this
behavior is expected.
As mentioned in ota42y#101 it makes more sense to return true from this method
when validation passes instead of nil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty response body fails validation
2 participants