Skip to content

Instantly share code, notes, and snippets.

@augensalat
Last active November 7, 2019 17:39
Show Gist options
  • Save augensalat/a67937dde3efbcf4df6f3430aea89a55 to your computer and use it in GitHub Desktop.
Save augensalat/a67937dde3efbcf4df6f3430aea89a55 to your computer and use it in GitHub Desktop.
Swagger2: collectionFormat is optional for multi-value parameters
use Mojo::Base -strict;
use Test::Mojo;
use Test::More;
use Mojolicious::Lite;
get '/pets' => sub {
my $c = shift->openapi->valid_input or return;
$c->render(openapi => $c->validation->output);
},
'getPets';
get '/pets/:id' => sub {
my $c = shift->openapi->valid_input or return;
$c->render(openapi => $c->validation->output);
},
'getPetsById';
plugin OpenAPI => {url => 'data://main/discriminator.json'};
my $t = Test::Mojo->new;
# Expected array - got null
$t->get_ok('/api/pets')->status_is(400)->json_is('/errors/0/path', '/ri');
# Expected integer - got number.
$t->get_ok('/api/pets?ri=1.3')->status_is(400)->json_is('/errors/0/path', '/ri/0');
# Not enough items: 1\/2
$t->get_ok('/api/pets?ri=3&ml=5')->status_is(400)->json_is('/errors/0/path', '/ml');
# Valid
$t->get_ok('/api/pets?ri=3&ml=4&ml=2')->status_is(200)->json_is('/ml', [4, 2])->json_is('/ri', [3]);
# Valid
$t->get_ok('/api/pets?csv=1,2,3&ri=1')->status_is(200)->json_is('/csv', [1, 2, 3]);
# should be valid
$t->get_ok('/api/pets?default=1,2,3&ri=1')->status_is(200)->json_is('/default', [1, 2, 3]);
# In path
$t->get_ok('/api/pets/ilm,a,r,i')->status_is(200)->json_is('/id', [qw(ilm a r i)]);
done_testing;
__DATA__
@@ discriminator.json
{
"swagger" : "2.0",
"info" : { "version": "0.8", "title" : "Test collectionFormat" },
"basePath": "/api",
"paths" : {
"/pets/{id}" : {
"get" : {
"operationId" : "getPetsById",
"parameters" : [
{
"name":"id",
"in":"path",
"type":"array",
"collectionFormat":"csv",
"items":{"type":"string"},
"minItems":0,
"required":true
}
],
"responses" : {
"200": {
"description": "pet response",
"schema": { "type": "object" }
}
}
}
},
"/pets" : {
"get" : {
"operationId" : "getPets",
"parameters" : [
{
"name":"no",
"in":"query",
"type":"array",
"collectionFormat":"multi",
"items":{"type":"integer"},
"minItems":0
},
{
"name":"ml",
"in":"query",
"type":"array",
"collectionFormat":"multi",
"items":{"type":"integer"},
"minItems":2
},
{
"name":"ri",
"in":"query",
"type":"array",
"collectionFormat":"multi",
"required":true,
"items":{"type":"integer"},
"minItems":1
},
{
"name":"csv",
"in":"query",
"type":"array",
"collectionFormat":"csv",
"required":false,
"items":{"type":"integer"},
"minItems":1
},
{
"name":"default",
"in":"query",
"type":"array",
"required":false,
"items":{"type":"integer"},
"minItems":1
}
],
"responses" : {
"200": {
"description": "pet response",
"schema": { "type": "object" }
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment