0

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.

Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71
  • 1
    `servers` is definitely not a defined helper but maybe try `path('/v1/payment-methods/cards', servers: [{url: 'https://secure.tryedge.com',description: 'Edge secure card portal'}])` as it appears `path` supports pass through metadata. Either that or see if you can access the metadata directly e.g. `metadata[:operation][:servers] = [{url: 'https://secure.tryedge.com',description: 'Edge secure card portal'}]` – engineersmnky Jan 11 '23 at 18:48
  • The metadata is accessible from within, and we can set it. `post('...') do; metadata[:operation][:servers] = [{...}]; end` works. You want to add an answer? – Ziyan Junaideen Jan 12 '23 at 04:38

1 Answers1

1

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.

Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71
  • 1
    Glad it worked for you. Not sure why this isn't a methid in the first place as the change to the code base would simply be adding a single element to an Array. – engineersmnky Jan 12 '23 at 13:02
  • @engineersmnky Yes, given it's so simple, it should be there. I wonder if I missed it rushing through the code. I am going to check it out this weekend or the other to make sure. – Ziyan Junaideen Jan 13 '23 at 03:54
  • 1
    Your not missing it. [Source](https://github.com/rswag/rswag/blob/master/rswag-specs/lib/rswag/specs/example_group_helpers.rb#L38) this is how I came up with the proposed solution. – engineersmnky Jan 13 '23 at 04:15