我正在尝试让我的API支持同名的多个参数:例如
/myAPI/search?myfield=1&myfield=2&myfield=3
我正在使用Perl和cpan模块Mojolicious和Swagger2
我的swagger文件(yaml)具有以下定义(已验证):
/search:
get:
x-mojo-controller: "Search"
operationId: search
description: Search
parameters:
- name: myfield
description: Array of types
in: query
type: array
collectionFormat: multi
uniqueItems: true
items:
type: string
required: false
我的 Controller 如下所示:
package myAPI::Controller::Search;
use Mojo::Base 'Mojolicious::Controller';
sub search {
my( $self, $args, $cb ) = @_;
$self->render(text => Dumper $args);
}
当将args转储到浏览器时,“myfield”字段似乎是一个数组,但其中只有最后一个值。
$VAR1 = { 'myfield' => [ '3' ] };
Swagger2版本是:
our $VERSION = '0.83';
我究竟做错了什么 ?
最佳答案
我认为您正在编造示例,或者可能有一些使输入混乱的问题。以下测试成功运行:
use Mojo::Base -strict;
use Test::Mojo;
use Test::More;
package MyApp::Example;
use Mojo::Base 'Mojolicious::Controller';
sub search {
my ($self, $args, $cb) = @_;
$self->$cb($args, 200);
}
package main;
use Mojolicious::Lite;
plugin Swagger2 => {url => 'data://main/multi-param.json'};
my $t = Test::Mojo->new;
$t->get_ok('/search?myfield=1&myfield=2&myfield=3')->status_is(200)->json_is('/myfield', [1, 2, 3]);
done_testing;
__DATA__
@@ multi-param.json
{
"swagger": "2.0",
"info": {"version": "1.0", "title": "Test multi"},
"paths": {
"/search": {
"get": {
"x-mojo-controller": "MyApp::Example",
"operationId": "search",
"parameters": [
{
"name": "myfield",
"in": "query",
"type": "array",
"collectionFormat": "multi",
"uniqueItems": true,
"items": { "type": "string" },
"required": false
}
],
"responses": {
"200": {"description": "whatever", "schema": {"type": "object"}}
}
}
}
}
}
已经有一个测试:https://github.com/jhthorsen/swagger2/blob/master/t/multi-param.t