hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
Other
1.08k stars 89 forks source link

Validate response against openapi schema #591

Closed timo-klarshift closed 3 months ago

timo-klarshift commented 4 months ago

Description

Hey, first of all thanks for the great library. I am so happy to finally drop openapi-generator :)

I have the use-case that I am developing an SDK/client against a legacy API. During tests we find cases where the provided schema does not match the actual backend responses. What I would like to have is schema validation when a response is returned. I do of course have the openapi schema available, but my attempt using Interceptors is not successful because when I receive the response I have no access to the request, therefore I do not know what method was called on the url which means I cannot match the response to an operation without ambiguity.

I see it would be possible by replacing the whole request.ts file which is possible via config. But actually I would only need to alter the sendRequest method for this purpose. Here I do have the RequestInit and could process the returned Response from fetch. Then get my schema and validate whether the returned response for said operation is valid against the provided schema. If not I would want to log a warning.

Another way to solve it would be not on request implementation layer (fetch in this case) but on library layer. We could add a similar interceptor concept but extend the Response interface so it would contain a reference to RequestInit. Also adding some meta-data could be helpful, for example operationId from the schema.

I hope this use-case is clear, maybe there is already some way to implement this which I cannot see yet.

mrlubos commented 4 months ago

Welcome @timo-klarshift! 🚀 pretty sure I've heard this request a few times already, it's going to get addressed, but it will take a while as there's a lot to build!