Skip to content

Instantly share code, notes, and snippets.

@augensalat
Created November 7, 2019 10:49
Show Gist options
  • Save augensalat/316b3af7cb53978a61e1ed3075d995d5 to your computer and use it in GitHub Desktop.
Save augensalat/316b3af7cb53978a61e1ed3075d995d5 to your computer and use it in GitHub Desktop.
Mojolicious::Plugin::OpenAPI: OpenAPI3 array parameters test
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:///parameters.json',
schema => 'v3'
};
my $t = Test::Mojo->new;
# Valid
$t->get_ok('/pets?a=3&a=4')->status_is(200)->json_is('/a', [3, 4]);
# Should be valid as well
$t->get_ok('/pets?a=3')->status_is(200)->json_is('/a', [3]);
# In path
$t->get_ok('/pets/a,b,c')->status_is(200)->json_is('/id', [qw(a b c)]);
done_testing;
__DATA__
@@ parameters.json
{
"openapi": "3.0.0",
"info": {
"license": {
"name": "MIT"
},
"title": "Swagger Petstore",
"version": "1.0.0"
},
"servers": [
{ "url": "/" }
],
"paths" : {
"/pets/{id}" : {
"get" : {
"operationId" : "getPetsById",
"parameters" : [
{
"name":"id",
"in":"path",
"style":"simple",
"explode":false,
"schema": {
"type":"array",
"items":{"type":"integer"},
"minItems":0
},
"required":true
}
],
"responses" : {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": { "type": "object" }
}
}
}
}
}
},
"/pets" : {
"get" : {
"operationId" : "getPets",
"parameters" : [
{
"name":"a",
"in":"query",
"style":"form",
"explode":true,
"schema": {
"type":"array",
"items":{"type":"integer"},
"minItems":0
},
"required":false
}
],
"responses" : {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": { "type": "object" }
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment