Rswag: How to create proper :array parameters

Created on 20 Nov 2019  路  3Comments  路  Source: rswag/rswag

One of the most common standards for sending an array in a hash is something along the lines of:

array[]=value_1&array[]=value2

If you get that in rails, and call params[:array] you can check that it is indeed an proper array. But how could I set up the parameters in a schema in a way to reflect this syntax.

I need to send an array of channel_ids, I have tought of something along the lines of:

parameter name: :channel_ids, in: :query,  type:  :array,  description: 'filter by channel ids', required: false, items: {
    type: :string,
    example: "ebfec9ec-ba72-4b24-95b8-68aebe5f0024"
  }

But it sent the data as channel_ids=value, instead of channel_ids[]=value.

Then the idea came to use:

parameter name: "channel_ids[]", in: :query,  type:  :array,  description: 'filter by channel ids', required: false, items: {
        type: :string,
        example: "ebfec9ec-ba72-4b24-95b8-68aebe5f0024"
      }

Which does send channel_ids[]=value, but I'm still unable to send multiple values, like channel_ids[]=value1&channel_ids[]=value2.

Is there an way to properly do it?

Most helpful comment

This doesn't actually pass the OpenAPI V3 standards. Running OpenAPIV3 parser we do get an error of Unexpected fields: collectionFormat. It looks like this is the default OpenAPI V3 serialization method. But I cannot figure out how to set my lets!(...) so that it matches the parameter set up.

parameter({
        name: 'kind[]',
        in: :query,
        required: false,
        schema: {
          type: :array,
          items: {
            type: :string,
            enum: [
              'file',
              'photo',
              'video',
            ],
            nullable: true,
            description: 'Kind of Attachment'
          }
        }
      })

...
let!(:'kind[]') { ['file', 'video'] }

All 3 comments

Check https://github.com/rswag/rswag/issues/99. Looks like collectionFormat: :multi does the magic.

I found out from the source code but looks like it's actually documented here.

Thanks man, really helpfull @vladflorescu94

This doesn't actually pass the OpenAPI V3 standards. Running OpenAPIV3 parser we do get an error of Unexpected fields: collectionFormat. It looks like this is the default OpenAPI V3 serialization method. But I cannot figure out how to set my lets!(...) so that it matches the parameter set up.

parameter({
        name: 'kind[]',
        in: :query,
        required: false,
        schema: {
          type: :array,
          items: {
            type: :string,
            enum: [
              'file',
              'photo',
              'video',
            ],
            nullable: true,
            description: 'Kind of Attachment'
          }
        }
      })

...
let!(:'kind[]') { ['file', 'video'] }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DalfDev picture DalfDev  路  3Comments

douglasrlee picture douglasrlee  路  4Comments

ManevilleF picture ManevilleF  路  4Comments

martio picture martio  路  4Comments

bspellacy picture bspellacy  路  3Comments