[英]How to define array-of-objects as parameter?
I am quite new to Swagger , so this might be a basic question. 我对Swagger很新,所以这可能是一个基本问题。
I am able to create .yml file for an API which takes an array of integers as parameter, as follows: 我能够为一个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.
This is what I send to the above API and everything works fine: 这是我发送到上面的API,一切正常:
{"sample_ids": [475690,475689,475688]}
Now, instead of an array of integers, if I want to use some complex object as parameter, how to do it? 现在,如果我想使用一些复杂的对象作为参数,而不是整数数组,该怎么做?
Eg If this is what I want to send: 例如,如果这是我要发送的内容:
{"sample_ids": [{
"sample_id": "7",
"some_prop": "123"
},
{
"sample_id": "17",
"some_prop": "134"
}]}
How should the .yml file look? .yml文件应该怎么样? I have tried something like this and it doesn't seem to work: 我尝试过这样的东西,它似乎不起作用:
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
This one seems to work, mostly: 这个似乎工作,主要是:
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
Now only problem is, in the Swagger UI, it is not showing member variables and their default values. 现在唯一的问题是,在Swagger UI中,它没有显示成员变量及其默认值。 Rather is showing it as null: 而是将其显示为null:
{
"samples": [
null
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.