zoubingwu / msw-auto-mock

A cli tool to generate random mock data from OpenAPI definition for msw.
246 stars 54 forks source link

Incompatible with "default" responses #34

Open solomonhawk opened 10 months ago

solomonhawk commented 10 months ago

One of the recommended patterns in the OpenAPI 3.0 specification is the use of default responses when the underlying API returns the same shape of data for a variety of error cases.

https://swagger.io/docs/specification/describing-responses/#:~:text=each%20response%20individually.-,Default%20Response,-Sometimes%2C%20an%20operation

Right now, msw-auto-mock generates code with ctx.status(NaN) which is invalid and throws an error like RangeError: Failed to construct 'Response': The status provided (0) is outside the range [200, 599]..

I'm not sure how we can reliably generate the correct mocked responses for cases such as these, since they do not really capture information about which specific errors are expected to be returned from those handlers.

At the very least a note in the README about this edge case would be good for those deciding whether this library will satisfy their specific use case.

Alternatively:

  1. we could assume default responses correspond with a specific status code, like 5XX
  2. we could allow users to specify a list of status codes to generate responses for when we encounter a default response (e.g. defaultStatuses: [404, 500] or something like that)
  3. we could fail more gracefully, by emitting an error during generation if we try to parse a status code and end up with NaN