RSwag not supporting apiKey pairs in requests

As you can see from the github issue, RSwag does not support multiple headers for api Key authentication.

This is my openapi/swagger yml, in following with Multiple API Key Setup in swagger docs:

components:
  securitySchemes:
    apiKey:
      type: apiKey
      in: header
      name: X-API-KEY
      description: Your API Key, provided by GoApron.
    userId:
      type: apiKey
      in: header
      name: X-USER-ID
      description: Your GoApron user ID
security:
- apiKey: []
  userId: []

As the issue notes:

in testing we’ve discovered rswag’s spec helpers only add the first entry of type apiKey to the headers / query.

Is there some way I can actually specify both these headers in my API requests?

The problem with just adding header parameters to each endpoint is that it will add all those parameters to the docs, when that’s not how auth headers are supposed to be shown.

What I would like is a way to “hack” the requests in the rswag request specs to send those headers as part of the requests even though they’re not defined as parameters due to rswag limitations.

My Current Thinking

It seems like overriding build_request (used by submit_request) may be my best bet:

https://github.com/rswag/rswag/blob/master/rswag-specs/lib/rswag/specs/example_helpers.rb#L10
https://github.com/rswag/rswag/blob/master/rswag-specs/lib/rswag/specs/request_factory.rb#L14

So it turns out that everything just hinges on using symbols in Rswag correctly. Specifically, when you’re defining parameters in swagger_helper, make sure you use symbols rather than strings for securitySchemes values. I was using a string instead of symbol on some fields, and even though it generated the correct swagger.yml, Rswag wasn’t able to understand the refs. Just make sure you follow the format here

Everything magically works now with headers and let variables. I don’t think the Github issue was really the issue I thought it was. It turns out it was just Rswag specs not handling Refs correctly when you use symbol instead of string on securitySchemes fields.

Overriding build_request seems to work, and I can pass in custom headers when I went to override it:

module Rswag
  module Specs
    class RequestFactory
      alias_method :original_build_request, :build_request

      def build_request(metadata, example)
        request = original_build_request(metadata, example)

        request[:headers] ||= {}

        # Check for custom headers specified in metadata using run_test! keyword arguments
        if metadata[:custom_headers]
          request[:headers].merge!(metadata[:custom_headers])
        else
          request[:headers].merge!(DEFAULT_HEADERS)
        end

        request
      end
    end
  end
end

Note: super is a more efficient way to write the above, I didn’t end up needing this functionality (see main answer).

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật