collectionFormat:多不能与 Perl Mojolicious Swagger2 一起使用

collectionFormat: multi not working with Perl Mojolicious Swagger2

我正在尝试让我的 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';

我做错了什么?

我认为你是在编造你的例子,或者你可能有一些搞乱输入的钩子。下面的测试运行成功:

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

您可能想试试这个插件:https://metacpan.org/release/Mojolicious-Plugin-OpenAPI

它与 "mojolicious rules" 一起播放更多,这意味着您可以像这样提取参数:

sub search {
  my $c = shift->openapi->valid_input or return; 
  my $values = $c->every_param("myfield");
  $c->reply->openapi(200 => $values);
}