2016-07-04 93 views
4

我有一个GET路由,我想将url中的对象参数编码为查询字符串。在Swagger文档中使用对象类型查询参数

当写招摇文档我基本上得到禁止使用我的错误在query类型参数使用schema/object类型:

paths: 
    /mypath/: 
    get: 
     parameters 
     - in: path 
      name: someParam 
      description: some param that works 
      required: true 
      type: string 
      format: timeuuid #good param, works well 
     - $ref: "#/parameters/mySortingParam" #this yields an error 

parameters: 
    mySortingParam 
    name: paging 
    in: query 
    description: Holds various paging attributes 
    required: false 
    schema: 
     type: object 
     properties: 
     pageSize: 
      type: number 
     cursor: 
      type: object 
      properties: 
      after: 
       type: string 
       format: string 

具有对象值的请求查询参数将在实际要求进行编码。

尽管Swagger在屏幕顶部显示错误,但该对象在swagger UI编辑器中正确呈现,但该错误浮动在文档顶部。

回答

6

我不认为你可以使用“对象”,如扬鞭规范作为查询参数查询参数只支持基本类型(https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types

+0

是的,这似乎是这种情况,谢谢 – Vee6

+1

相反,对swagger.io文档感到失望的是:https://swagger.io/docs/specification/describing-parameters/。请参阅架构与内容下的部分。虽然上面的链接描述了将对象转换为内容类型(如JSON),但它并没有给出与OP的查询直接相关的示例。 –

+0

@DanielMaclean:你发布的链接是关于OpenAPI 3.0,而答案是关于OpenAPI/Swagger 2.0(3.0在2016年不存在)。该链接的2.0版本是https://swagger.io/docs/specification/2-0/describing-parameters/ – Helen

0

这是可能的,只是不能与扬鞭即OpenAPI的V2。 OpenAPI的V3介绍这怎么可能在这里:

https://swagger.io/docs/specification/describing-parameters/

parameters: 
- in: query 
name: filter 
# Wrap 'schema' into 'content.<media-type>' 
content: 
    application/json: # <---- media type indicates how to serialize/deserialize the parameter content 
    schema: 
     type: object 
     properties: 
     type: 
      type: string 
     color: 
      type: string 

你可以只是查询参数作为一个普通的老字符串类型,然后手动执行序列的同时,然后将查询参数作为需要。这就是我在SwaggerUI完全支持OpenAPI v3之前所做的。