我正在尝试让我的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
我的控制器看起来像这样:
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';
我做错了什么?
发布于 2016-08-03 19:35:05
我认为你在编造你的例子,或者你可能有一些钩子弄乱了输入。下面的测试运行成功:
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
发布于 2016-08-03 19:42:47
您可能想尝试一下这个插件:https://metacpan.org/release/Mojolicious-Plugin-OpenAPI
它更多地遵循"mojolicious规则“,这意味着你可以像这样提取参数:
sub search {
my $c = shift->openapi->valid_input or return;
my $values = $c->every_param("myfield");
$c->reply->openapi(200 => $values);
}
https://stackoverflow.com/questions/38740698
复制相似问题