ruby-on-railsrubyrspecrspec-railsrswag

How to send parameter outside of the RSwag schema in the RSpec test?


With RSwag and RSpec in Ruby on Rails, is it possible to send a parameter that is not defined as a parameter via the run_test!?

Example test:

# frozen_string_literal: true

require "swagger_helper"

describe "Resources", type: :request, swagger_doc: "api/resources.json" do
  path "/resources" do
    get "get resources" do
      tags "resources"

      parameter name: "sort[column]",
                in: :query,
                type: :string,
                enum: ["created_at", "updated_at"]
                required: false
      parameter name: "sort[order]",
                in: :query,
                type: :string,
                enum: ["asc", "desc"]
                required: false

      response "422", "Unprocessable Entity" do
        context "with unrecognized param" do
          # define sort[direction] here

          run_test! do |respone|
            expect(json_response["errors"]).to contain_exactly(
              # my serialized response error
            )
          end
        end
      end
    end
  end
end

I'd like to send sort[direction] in my context. What I have tried so far:

let(:"sort[direction]") { "asc" }
let(:sort) { { direction: "asc" } }
let(:sort) { { "direction" => "asc" } }

with no avail - I got HTTP 200 Success, even though the same request sent via Postman with sort[direction] defined responds with expected HTTP 422 Unprocessable Entity.


Solution

  • I have managed to do it this way:

    context "with unrecognized sort param" do
      before do |example|
        example.metadata[:example_group][:operation][:parameters] += [{
          name: "sort[direction]",
          in: :query,
          type: :string,
          required: false
        }]
      end
    
      let(:"sort[direction]") { "asc" }
    
      run_test! do |respone|
        expect(json_response["errors"]).to contain_exactly(
        # my serialized response error
        )
      end
    end
    

    It does not add the params to the generated OpenAPI JSON file, which is good.