我可以在查询字符串上使用swagger-php使用数组吗?

Leo*_*lis 4 php swagger swagger-php swagger-ui

我用Swagger-php.当我定义查询字符串上的参数时,它可以是一个数组.但从我所看到的,它不支持这种查询字符串:

https://api.domain.tld/v1/objects?q[]=1&q[]=5&q[]=12
Run Code Online (Sandbox Code Playgroud)

我相信如果可能的话,这将collectionFormat现场设置.目前我一直在使用pipes,但我想使用上述格式,并且Swagger-UI也反映了这一点.但是,我读到了这个github问题,这让我想知道这是否真的可行而且我错过了它?

我的Swagger-PHP定义的一个例子:

/**
*     @SWG\Parameter(
*         name="ids",
*         in="query",
*         description="A list of IDs (separated by pipes) to filter the Returns",
*         required=false,
*         type="array",
*         @SWG\Items(
*             type="integer",
*             format="int32"
*         ),
*         collectionFormat="pipes"
*     )
*/
Run Code Online (Sandbox Code Playgroud)

这导致以下JSON:

"parameters": {
    "ids": {
        "name": "ids",
        "in": "query",
        "description": "A list of IDs (separated by pipes) to filter the Returns",
        "required": false,
        "type": "array",
        "items": {
            "type": "integer",
            "format": "int32"
        },
        "collectionFormat": "pipes"
    }
}
Run Code Online (Sandbox Code Playgroud)

Ima*_*Ima 9

/**
 *     @SWG\Parameter(
 *         name="q[]",
 *         in="query",
 *         description="A list of IDs (separated by new lines) to filter the Returns",
 *         required=false,
 *         type="array",
 *         collectionFormat="multi",
 *         uniqueItems=true,
 *     )
 */
Run Code Online (Sandbox Code Playgroud)

这将产生类似于此的东西

{
    "name": "q[]",
    "in": "query",
    "description": "type",
    "required": false,
    "type": "array",
    "collectionFormat": "multi",
    "uniqueItems": true
}
Run Code Online (Sandbox Code Playgroud)

得到的图像

  • 这应该是答案.我花了几个小时搜索答案,正如当前的答案所说,只接受multi,csv,ssv或tsv,但在名称中添加括号可以使用multi.除此评论外,此案例没有网站,博客或文档. (3认同)

Kla*_*aus 2

现在无需 [] hack 最喜欢的答案就可以做到这一点。使用 deepObject 样式。

 name: q
 style: deepObject
 schema:
   type: array
   items:
     type: string
Run Code Online (Sandbox Code Playgroud)

这将生成如下所示的 url:q[0]=string1&q[1]=string2