Rails rswag - override default server to provide a different base URL

464 Views Asked by At

The API I am working with has a few endpoints to be routed through VeryGoodSecurity for PCI compliance.

It does seem as though OpenAPI V3 supports overriding servers as mentioned in this page

The global servers array can be overridden on the path level or operation level. This is handy if some endpoints use a different server or base path than the rest of the API. Common examples are:

How can this be done with rswag?

I tried some thing like this:

  path('/v1/payment-methods/cards') do
    post('Create a payment method from card details') do
      tags('Payment Method')
      consumes('application/json')
      produces('application/json')
      # ....

      # Rest of the API will be on api.tryedge.com
      servers([{
        url: 'https://secure.tryedge.com',
        description: 'Edge secure card portal'}])

Hoping to achieve some thing like this in Swagger YML:

  /v1/payment-methdos/cards:
    post:
      servers:
        - url: https://secure.tryedge.com
          description: Edge secure card portal

But I am getting an error.

  undefined method `servers' for RSpec::ExampleGroups::ApiV1PaymentMethodsController::V1PaymentMethodsCards::Post:Class

Any help highly appreciated. Thanks in advance.

1

There are 1 best solutions below

3
On

rswag doesn't define a full set of helpers (including servers as experienced in the above issue) to cover the entirety of the OpenAPI v3 schema.

However, it is possible to achieve the same result by manipulating the test case metadata as highlighted by @engineersmnky 's comments above.

  path('/v1/payment_methods/cards') do
    post('Link a card') do
      tags('Payment Method')
      consumes('application/json')
      produces('application/json')
      security([bearerAuth: []])
      operationId('linkCard')
      description('Links a card to an existing Edge customer.')

      metadata[:operation][:servers] = [{ url: 'https://secure.tryedge.com', description: 'Edge secure card portal' }]

This is because rswag is building the schema by updating the metadata of the test case.