如何定义对象数组作为参数?

时间:2016-11-16 19:03:12

标签: yaml swagger swagger-2.0

我是 Swagger 的新手,所以这可能是一个基本问题。

我能够为一个API创建.yml文件,该文件将整数数组作为参数,如下所示:

Add samples
---
tags:
 - MY API
parameters:
 - name: my_id
   in: path
   type: integer
   required: true
   description: Some des
 - name: body
   in: body
   schema:
     id: add_samples
     required:
       - sample_ids
     properties:
       sample_ids:
         type: array
         items:
            type: integer
         description: A list of sample ids to be added
responses:
   '200':
     description: Added samples.
   '400':
     description: Error adding samples.

这是我发送到上述API的内容,一切正常:

{"sample_ids": [475690,475689,475688]}

现在,如果我想使用一些复杂的对象作为参数,而不是整数数组,该怎么做?

E.g。如果这是我想发送的内容:

{"sample_ids": [{
    "sample_id": "7",
    "some_prop": "123"
},
{
    "sample_id": "17",
    "some_prop": "134"
}]}

.yml文件应该如何显示?我尝试过类似的东西,它似乎不起作用:

Add sample
---
tags:
 - Samples API
models:
  Sample:
    id: Sample
    properties:
      sample_id:
        type: string
        default: ""
        description: The id for this sample
      some_prop:
        type: integer
        description: Some prop this sample
parameters:
 - name: body
   in: body
   schema:
     id: add_sample
     required:
       - sample_ids
     properties:
       samples:
         type: array
         description: A list of samples to be added
         items:
           $ref: Sample
responses:
   '201':
     description: Created a new sample with the provided parameters
   '400':
     description: SOME ERROR CODE

1 个答案:

答案 0 :(得分:2)

这个似乎很有用,主要是:

Add sample
---
tags:
 - Samples API
models:
  Sample:
    id: Sample
    properties:
      sample_id:
        type: string
        default: ""
        description: The id for this sample
      some_prop:
        type: integer
        description: Some prop this sample
parameters:
 - name: body
   in: body
   schema:
     id: add_sample
     required:
       - sample_ids
     properties:
       samples:
         type: array
         description: A list of samples to be added
         items:
           $ref: Sample
responses:
   '201':
     description: Created a new sample with the provided parameters
   '400':
     description: SOME ERROR CODE

现在唯一的问题是,在Swagger UI中,它没有显示成员变量及其默认值。而是将其显示为null:

{
  "samples": [
     null
  ]
}