openapi_parser icon indicating copy to clipboard operation
openapi_parser copied to clipboard

Empty response body fails validation

Open klausbadelt opened this issue 4 years ago • 1 comments

Validation fails on 204 response, which has empty string as body.

Example:

paths:
  /health_check:
    get:
      description: Health check
      responses:
        '204':
          description: OK

Code

OpenAPIParser.load 'openapi.yml'
request_operation = root.request_operation :get, '/health_check'
validatable_response_body = OpenAPIParser::RequestOperation::ValidatableResponseBody.new(204, "", {})
response_validate_options = OpenAPIParser::SchemaValidator::ResponseValidateOptions.new
request_operation.validate_response_body(validatable_response_body, response_validate_options)

Expected result is truth-y, but actual result is nil.

This is btw similar to a bug in https://github.com/openapi-library/OpenAPIValidators/issues/54.

klausbadelt avatar Feb 09 '21 01:02 klausbadelt

  • This gem raise error when validation is failed.
  • This gem return nil when validation passed.
    • In this case, we can't find media type object so don't validate parameter and return nil
    • https://github.com/ota42y/openapi_parser/blob/9c7a802866b272212dc55c1ccc454586727da773/lib/openapi_parser/schemas/response.rb#L26-L26

But I think it is better to return true when the validation was passed so we should change return value :)

ota42y avatar Mar 27 '21 14:03 ota42y

To those who using openapi_parser via committee:

committee has validate_success_only option, which can turn off responseBody validation if the response status is 4xx or 5xx.

In my experience, API schemas often omit the response body content for error responses specifically. If you're encountering issues with how openapi_parser via committee handles error responses, disabling validate_success_only might help resolve them.

t407o avatar Sep 25 '24 16:09 t407o