anymaniax / 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
2.56k stars 284 forks source link

Request parameters in mocks #422

Open BartoGabriel opened 2 years ago

BartoGabriel commented 2 years ago

I would like to have the possibility that the mock functions can receive the request parameters, in order to generate data based on them.

An example would be: GET /api/pets?page=1&pagesize=10

It would take the pageSize to generate an array of 10 elements.

image

Is it possible to do this? Or I'm wrong

Thanks!

anymaniax commented 2 years ago

Hey @BartoGabriel, should be possible if you want you can check to add it to the codebase. Otherwise I will check when I have a bit of time to add it

shfrmn commented 7 months ago

Hi @anymaniax, Thank you for all your work on orval!

I would really like to see this one done, so I'm considering to submit a PR. I plan to take the minimal approach of just passing URL params and request body to all mock functions (and the nested data and properties handlers).

I'm sure it's also possible to add some configuration options to streamline this use case, but I think we should probably keep it out of the scope until the basic functionality is merged.

Some inspirational use cases:

  1. Endpoint that creates an entity could return a response with correctly set relational fields, so application code could actually work with all the data coming from the mocks (e.g. creating a comment under a post would actually return an instance with the relevant postId, which would enable any flows after that to work as expected).
  2. Conditional fields (optional values or enums that have to be determined based on the request data)
  3. Page size and other meta parameters could be set correctly (@BartoGabriel's reason for opening this issue)

I'd really appreciate if you could answer a few questions before I work on this.

  1. Do you agree to accept this change in principle?
  2. Would it be possible to release this enhancement with a minor version bump without waiting for v7 release?
  3. What are the current workarounds that allow setting properties based on the request object, if there are any? I tried to make it work at the level of msw setup (right before calling setupWorker), but without much success.

Hope we can get this done, as it would make mocking a lot more practical and realistic.

melloware commented 7 months ago

cc @Will-Mann-16 who just recently did a bunch of mock work