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?
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'] }
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 mylets!(...)so that it matches the parameter set up.