rspec-openapi icon indicating copy to clipboard operation
rspec-openapi copied to clipboard

Dealing with responses with different possible bodies for the same status code

Open Uaitt opened this issue 1 year ago • 1 comments

While using this gem in one of my recents projects, I have faced the following issue:

A specific controller action can return a 200 and a 401 when the client is not authorized. However, the 200 response can have very different bodies based on the headers that the client passes to the server (with very different, I mean completely different JSON structures).

I have written very comprehensive request specs, testing every possible response which can be returned by that controller action (basically, I have written a test for each possible 200 response, and the 401 response).

Currently rspec-openapi will generate an openapi.yaml with:

  • A 200 response, taken from the last example (I guess?) of the request specs for my controller
  • A 401 response

As of now, the only way I have found to add the other 200 responses is to manually edit the openapi.yaml file. Is there another way to achieve this behaviour maybe?

Thanks for your time, and thanks to the maintainers for this amazing gem ❤️ 😃

Uaitt avatar Oct 10 '23 12:10 Uaitt

The current implementation assumes that responses from the same API share the same JSON structure. So rspec-openapi collects API responses and merges. I do not remember the details, but a response from the last test case overwrites the former ones. I understand this assumption is only sometimes valid.

We may group responses by same HTTP status code and merge in each group.

Relates to https://github.com/exoego/rspec-openapi/issues/140

exoego avatar Oct 13 '23 10:10 exoego