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

我对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]}

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

例如.如果这是我想发送的内容:

{"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

最佳答案 这个似乎工作,主要是:

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
  ]
}
点赞