rswag/rswag

[BUG] example with response body is not working

gmkseta opened this issue · 2 comments

Describe the bug

The response.body is not being utilized for generating examples in the documentation, despite attempting to incorporate it within an after block as per the Rswag documentation. The specified after block is designed to parse the response.body and merge it into the example.metadata[:response][:content], but this operation seems to be failing, and no examples are being generated from the response.body.

Steps to Test or Reproduce

  1. Set up a Rails project with the version 7.0.4.3, Ruby version 3.1.4, and Rswag version 2.11.0.
  2. Create a new Rspec test file and include the following after block within a response block:
after do |example|
  content = example.metadata[:response][:content] || {}
  example_spec = {
    "application/json"=>{
      examples: {
        test_example: {
          value: JSON.parse(response.body, symbolize_names: true)
        }
      }
    }
  }
  example.metadata[:response][:content] = content.deep_merge(example_spec)
end
  1. Run the RSpec tests and generate the Swagger documentation.
  2. Observe that the examples generated from response.body are not reflected in the documentation.

Expected behavior

The response.body should be parsed and merged into the example.metadata[:response][:content] to generate examples in the documentation. These examples should be visible in the generated Swagger documentation.

Screenshots

Additional context

The issue persists in creating real-time examples from the response body, which is crucial for accurately documenting the API responses.

Dependency versions

  • Rswag: 2.11.0
  • RSpec: (specify version)
  • Rails: 7.0.4.3
  • Ruby: 3.1.4

Relates to which version of OAS (OpenAPI Specification)

  • OAS2
  • OAS3
  • OAS3.1

Please ensure to fill in the RSpec version under the Dependency versions section and attach any relevant screenshots to provide a clearer context of the issue.

It looks like setting SWAGGER_DRY_RUN=0 fixes this.

Thanks @Roguelazer, closing the issue, but noting we should document the SWAGGER_DRY_RUN=0 better.