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

Support for non-JSON response validation #257

Closed
mt-kelvintaywl opened this issue Mar 24, 2020 · 1 comment
Closed

Support for non-JSON response validation #257

mt-kelvintaywl opened this issue Mar 24, 2020 · 1 comment

Comments

@mt-kelvintaywl
Copy link
Contributor

I have an OpenAPI 3 spec that for one of the responses return a HTTP 302 response, for example:

# snippet
paths:
    /faq/{id}:
    get:
      description: |
        fetch a FAQ page (HTTP 302 redirect to our external CMS for FAQ page)
      responses:
        '302':
          description: Response of a Connect redirect
          headers:
            Location:
              schema:
                type: string
               description: Redirection URL
           content:
             text/plain:
               schema:
                 type: string

and since OpenAPI3's response_validate handler will attempt to always parse response body to JSON:

def response_validate(status, headers, response, test_method = false)
full_body = +""
response.each do |chunk|
full_body << chunk
end
data = full_body.empty? ? {} : JSON.parse(full_body)
strict = test_method
Committee::SchemaValidator::OpenAPI3::ResponseValidator.
new(@operation_object, validator_option).
call(status, headers, data, strict)
end

This is the same for HyperSchema too:

def response_validate(status, headers, response, _test_method = false)
return unless link_exist?
full_body = +""
response.each do |chunk|
full_body << chunk
end
data = full_body.empty? ? {} : JSON.parse(full_body)
Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: validator_option.validate_success_only).call(status, headers, data)
end

I ended up having to set validate_success_only to true, but that meant I turned off validation for all non HTTP 2XX responses including client errors (HTTP 4XX) :(

Would be nice for the response_validate to attempt to only parse as JSON based on the Content-Type response header?

@mt-kelvintaywl
Copy link
Contributor Author

closing this since this is resolved by #273

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 a pull request may close this issue.

1 participant