2013-10-25 30 views
31

我有一個REST的服務記錄, 他們有的喜歡接受簡單的數組:如何使用簡單對象在Swagger中爲一個數組描述模型?

[ 
    { "name":"a" }, 
    { "name":"b" }, 
    { "name":"c" } 
] 

如何形容這揚鞭模型部分?我只能創建「命名的數組」像

model { 
properties: { "arr": { "type":"array", ...... 

,但它描述了這樣的數據:

"arr": [ 
    { "name":"a" }, 
    { "name":"b" }, 
    { "name":"c" } 
] 
+2

如果你想避免用手打字,你可以試試這個JSON招搖定義轉換器:https://roger13.github.io/SwagDefGen/ – Roger

回答

-2

如果我的理解是正確的,我覺得有以下可能你想要什麼。

至於你提到的

其中一些接受簡單的數組

然後,它會通過一個參數傳遞。

"parameters" : [{ 
    "name" : "param_name", 
    "type" : "array", 
    "items" : { 
    "$ref" : "M" 
    } 
    ... 
}] 
... 

對於模型部分:

"models" : { 
    "M": { 
    "id" : "M", 
    "properties": { 
     "name" : { 
     "type" : "string" 
     } 
    } 
    } 
+13

我在askking如何描述: [{「name」:「a」},{「name」:「b」},{「name」:「c」}] – razor

7

它可能看起來像這樣:

... 
    "parameters" : [{ 
     "name" : "whatEverThatArrayCalled", 
     "type" : "array", 
     "items" : { 
     "$ref" : "whatEverThatArrayCalled" 
     } 
     ... 
    }], 
    "models" : { 
    { 
    "id" : "whatEverThatArrayCalled", 
    "properties": 
     { 
     "whatEverThatArrayCalled" : 
      { 
     "type" : "array", 
     "items":{"name":"a", 
        "name":"b", 
        "name":"c" 
        }, 

      } 
     } 
    } 
}   

...

2

我試圖在editor.swagger.io的follwoing,它滿足了這個問題和作品的要求。 POST請求體需要一個數組。該數組由'stackoverflow'項目組成。每個項目都是一個具有名稱屬性的對象。

paths: 
    /test: 
    post: 
     summary: test 123 
     description: test 123 
     parameters: 
     - name: param1 
      in: body 
      required: true 
      description: test param1 
      schema: 
      type: array 
      items: 
       $ref: '#/definitions/stackoverflow' 
     responses: 
     200: 
      description: OK 
definitions: 
    stackoverflow: 
    type: object 
    properties: 
     name: 
     type: string 
     description: name of the object 
+0

這是正確答案。關鍵是'in:body'。根據Swagger規範:「由於只能有一個有效載荷,因此只能有一個主體參數,主體參數的名稱對參數本身沒有影響,僅用於文檔目的。」 – jrc

7

託尼元很接近,但沒有雪茄。這是的OpenAPI /揚鞭使用YAML適當的定義:

/test: 
post: 
    summary: test 123 
    description: test 123 
    parameters: 
    - name: param1 
     in: body 
     required: true 
     description: test param1 
     schema: 
      $ref: '#/definitions/stackoverflow' 
    responses: 
    200: 
     description: OK 

這將產生:

stackoverflow2[ 
    { 
    name: string 
    } 
] 

託尼的例子產生:

[ 
    stackoverflow { 
       name: string 
    } 
] 

完全揚鞭/ OpenAPI的爲YAML(複製&粘貼)

swagger: '2.0' 

################################################################################ 
#        API Information         # 
################################################################################ 
info: 
    version: "Two-point-Oh!" 
    title: Simple objects in array test 
    description: | 
    Simple objects in array test 

################################################################################ 
#         Parameters         # 
################################################################################ 

paths: 
    /test: 
    post: 
     summary: Array with named objects 
     description: Array with named objects 
     parameters: 
     - name: param1 
      in: body 
      required: true 
      description: test param1 
      schema: 
      type: array 
      items: 
       $ref: '#/definitions/stackoverflow' 
     responses: 
     200: 
      description: OK 
    /test2: 
    post: 
     summary: Array with simpel (nameless) objects 
     description: Array with simpel (nameless) objects 
     parameters: 
     - name: param1 
      in: body 
      required: true 
      description: test param1 
      schema: 
       $ref: '#/definitions/stackoverflow2' 
     responses: 
     200: 
      description: OK 
definitions: 
    stackoverflow: 
    type: object 
    properties: 
     name: 
     type: string 
     description: name of the object 
    stackoverflow2: 
    type: array 
    items: 
     type: object 
     properties: 
     name: 
      type: string 
      description: name of the object 

這裏是揚鞭/ OpenAPI的的JSON版本

{ 
    "swagger" : "2.0", 
    "info" : { 
    "description" : "Simple objects in array test\n", 
    "version" : "Two-point-Oh!", 
    "title" : "Simple objects in array test" 
    }, 
    "paths" : { 
    "/test" : { 
     "post" : { 
     "summary" : "Array with named objects", 
     "description" : "Array with named objects", 
     "parameters" : [ { 
      "in" : "body", 
      "name" : "param1", 
      "description" : "test param1", 
      "required" : true, 
      "schema" : { 
      "type" : "array", 
      "items" : { 
       "$ref" : "#/definitions/stackoverflow" 
      } 
      } 
     } ], 
     "responses" : { 
      "200" : { 
      "description" : "OK" 
      } 
     } 
     } 
    }, 
    "/test2" : { 
     "post" : { 
     "summary" : "Array with simpel (nameless) objects", 
     "description" : "Array with simpel (nameless) objects", 
     "parameters" : [ { 
      "in" : "body", 
      "name" : "param1", 
      "description" : "test param1", 
      "required" : true, 
      "schema" : { 
      "$ref" : "#/definitions/stackoverflow2" 
      } 
     } ], 
     "responses" : { 
      "200" : { 
      "description" : "OK" 
      } 
     } 
     } 
    } 
    }, 
    "definitions" : { 
    "stackoverflow" : { 
     "type" : "object", 
     "properties" : { 
     "name" : { 
      "type" : "string", 
      "description" : "name of the object" 
     } 
     } 
    }, 
    "stackoverflow2" : { 
     "type" : "array", 
     "items" : { 
     "$ref" : "#/definitions/stackoverflow2_inner" 
     } 
    }, 
    "stackoverflow2_inner" : { 
     "properties" : { 
     "name" : { 
      "type" : "string", 
      "description" : "name of the object" 
     } 
     } 
    } 
    } 
} 
3

粘貼這http://editor.swagger.io/#/並點擊 「試此操作」

{ 
    "swagger": "2.0", 
    "info": { 
    "version": "1.0.0", 
    "title": "Privacy-Service API" 
    }, 
    "paths": { 
    "/allNames": { 
     "post": { 
     "consumes": [ 
      "application/json" 
     ], 
     "produces": [ 
      "application/json" 
     ], 
     "parameters": [ 
      { 
      "name": "request", 
      "in": "body", 
      "schema": { 
       "$ref": "#/definitions/ArrayOfNames" 
      } 
      } 
     ], 
     "responses": { 
      "200": { 
      "description": "List of names", 
      "schema": { 
       "type": "array", 
       "items": { 
       "type": "string" 
       } 
      } 
      } 
     } 
     } 
    } 
    }, 
    "definitions": { 
    "ArrayOfNames": { 
     "type": "array", 
     "items": { 
     "minItems": 1, 
     "type": "object", 
     "required": [ 
      "name" 
     ], 
     "properties": { 
      "name": { 
      "type": "string" 
      } 
     } 
     } 
    } 
    } 
} 
+0

這可能沒有'應用程序/ json'? – Gobliins

1
parameters: 
    - name: "items" 
    in: "formData" 
    description: "description" 
    required: "true" 
    type: "array" 
    items: 
     type: "object" 
     additionalProperties: 
     properties: 
      name: 
      type: "string" 

根據他們的文檔https://swagger.io/docs/specification/data-models/dictionaries/,這將導致具有名爲name和datatype屬性的對象的數組是字符串。
它可以通過請求體進行訪問,像request.body.items

更新:

好像它是足夠做(不additionalproperties):

items: 
    type: object 
    properties: 
    name: 
     type: string 

現在,你有每個項目都有一個叫做name的鍵和一個相應的值。

如果是這樣,什麼是TO要求....

+2

感謝您使用此代碼段,這可能會提供一些有限的即時幫助。一個[正確的解釋將大大提高其長期價值](/ meta.stackexchange.com/q/114762/350567)通過顯示*爲什麼*這是一個很好的解決方案,並會使它對未來更有用有其他類似問題的讀者。請[編輯]你的答案以添加一些解釋,包括你所做的假設。 – iBug

相關問題