2016-07-04 347 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之前所做的。