orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.24k stars 337 forks source link

support faker for API schemas #1399

Open karlismelderis-mckinsey opened 6 months ago

karlismelderis-mckinsey commented 6 months ago

What happens?

currently msw code is generating only faker code for response which is fine for mocking away API spec

we're looking for an option to generate faker code for each schema mentioned within API spec and use it in our unit/integration tests Some schemas in API spec describe real data entities and we even have an option to "inject" extra schemas that represent internal data types to generate extra faker code

In any case testing would be easier if users would be working with shoppingCartDtoMock instead of getShoppingCartControllerGetByIdResponseMock

something similar is available within kubb but I would like to avoid to add another tool if possible

Do you happen to have some hidden configuration option or if not would you be willing to accept change to generate such code?

if yes, how do you see it? as part of mock configuration or as brand new client?

What were you expecting to happen?

I would like to see folder that holds file per schema with faker code due to sheer amount of possible schemas I don't think it would be possible to put all in one file or split it somehow else

soartec-lab commented 6 months ago

@karlismelderis-mckinsey

The generateEachHttpStatus option allows you to generate each http response that you define as a schema within OpenAPI. Does it solve your question?

AllieJonsson commented 6 months ago

I think this is similar to #1318, do you have any use case outside of when an endpoint returns a oneOf response? The linked pr solves that use case in the next release

karlismelderis-mckinsey commented 6 months ago

our use-case is testing of BE services we can easily reuse faker data for plenty of service code tests and in rare cases where Schema is missing we can always inject required schema to generate faker

Beauty of our situation is that we are attaching Swagger decorators to every key out of the box due to reusable decorators within our code and even internal data structures end up having Swagger schema if needed

but because tests don't think about Request/Response it would be very weird for back-end developers to use getShoppingCartControllerGetByIdResponseMock instead of shoppingCartDtoMock

karlismelderis-mckinsey commented 6 months ago

Also currently Orval is generating only faker code for Response but many our test cases would benefit from access to request mock

that would allow us to replace huge pile of static test data with generated faker code

soartec-lab commented 6 months ago

@karlismelderis-mckinsey well, i don't think about backend testing. I wanna more understand, so could you show me some sample code for testing ?

karlismelderis-mckinsey commented 6 months ago

Let's narrow down the scope for our ask

if Orval can generate faker code to cover Request Schema that will help us to generate data for e2e/black-box tests

anything we need for integration/unit tests we will solve differently not to couple things up to much