pb33f / libopenapi-validator

OpenAPI validation extension for libopenapi, validate http requests and responses as well as schemas
https://pb33f.io/libopenapi/validation/
Other
60 stars 21 forks source link

Question / feature proposal: Using the validator with `httptest.ResponseRecorder` #40

Open jamietanna opened 11 months ago

jamietanna commented 11 months ago

I'm looking at adding libopenapi to https://gitlab.com/jamietanna/httptest-openapi, a library I've got for writing (unit) tests with Go's net/http handlers by validating the response matches an OpenAPI spec.

I've looked at using this library to give me full OpenAPI 3.0 and 3.1 support, instead of Kin's OpenAPI 3.0 only, but noticed that I couldn't use it right away.

In the test code i.e. https://gitlab.com/jamietanna/httptest-openapi/-/blob/v0.3.0/openapi3/validator.go?ref_type=tags#L95 we only have access to a httptest.ResponseRecorder whereas the library expects an http.Response.

Would it be of interest to add support for providing a httptest.ResponseRecorder or should I instead look at how to convert between the two types?

jamietanna commented 11 months ago

I guess I could pass in a httptest.ResponseRecorder but couldn't remember whether they're safe for production use cases, as the middleware is expected for prod code

daveshanley commented 11 months ago

Hi Jamie,

I'm not quite sure what you mean, would you be able to help me understand?

I'd be happy to help add in support to allow you get this working, I just need more help understanding the use case.

jamietanna commented 11 months ago

Hey, thank you! That'll teach me from raising an issue on mobile :sweat_smile:

I'd originally phrased the question correctly, then second-guessed myself, and amended it.

I've amended the description to note that I'm actually trying to use this with httptest.ResponseRecorder, not for use with a http.Response.

daveshanley commented 11 months ago

I can't see why not, I used the httptest.ResponseRecorder in the tests myself. I will have a look at the code and see how much work it would be.