2013-10-25 51 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

相关问题