ahx / openapi_first

openapi_first is a Ruby gem for request / response validation and contract-testing against an OpenAPI API description. It makes APIFirst easy and reliable.
MIT License
97 stars 12 forks source link

Getting the full response with the ResponseInvalidError #255

Closed neongrau closed 3 weeks ago

neongrau commented 3 months ago

When i get errors about this and that field not being of the right type. I really need to see what the response actually was.

Trying to write a log file with the actual response (prettified json) and the error so i can later review those instead of having it break with me wondering what was wrong.

So best would be to have the full result of @app.call(env) in the error object so i can log away and just pass on the result to see.

Would this currently be possible?

ahx commented 3 months ago

Hi. I think it makes sense to add more details to exceptions. I think it would be best to add ResponseInvalidError#errors or ResponseInvalidError#failure.errors to return an array of error objects (Schema::ValidationError) coming from the schema validation result and use that to get detailed information about what went wrong during validation without leaking too many details of your actual response body.

Making the actual response available on the exception makes sense to me as well for debugging, but I would not use that to log the response body.

ahx commented 3 months ago

By the way, if you use request.validate_response(rack_response) without passing raise_error: true you get an object where you can get all details including the response body. Somehing like:

response = Rack::Response[*app.call(env)]
request = Rack::Request.new(env)
validated = definition.validate_response(request, response)
unless validated.valid?
  validated.body # => parsed response body
  validated.error # => Failure instance
  validated.error.errors.map(&:message)
end
neongrau commented 3 months ago

By the way, if you use request.validate_response(rack_response) without passing raise_error: true you get an object where you can get all details including the response body. Somehing like:

Thanks! That seems to work nice.

Logging at this point is just because some minor errors (mostly missing null being allowed) are still too abundant.

Once i got the bulk going without errors i will switch over to send validation problems to Sentry and later have request and response validation active all the time on our public development server (opposed to just on my local dev machine).

ahx commented 3 weeks ago

Solved via https://github.com/ahx/openapi_first/pull/266