如何将对象数组定义为参数? [英] How to define array-of-objects as parameter?
问题描述
我对 Swagger 很陌生,所以这可能是一个基本问题.
I am quite new to Swagger, so this might be a basic question.
我能够为一个以整数数组作为参数的 API 创建 .yml 文件,如下所示:
I am able to create .yml file for an API which takes an array of integers as parameter, as follows:
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 的内容,一切正常:
This is what I send to the above API and everything works fine:
{"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?
例如如果这是我要发送的内容:
E.g. If this is what I want to send:
{"sample_ids": [{
"sample_id": "7",
"some_prop": "123"
},
{
"sample_id": "17",
"some_prop": "134"
}]}
.yml 文件应该是什么样的?我试过这样的事情,但它似乎不起作用:
How should the .yml file look? 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
现在唯一的问题是,在 Swagger UI 中,它没有显示成员变量及其默认值.而是将其显示为空:
Now only problem is, in the Swagger UI, it is not showing member variables and their default values. Rather is showing it as null:
{
"samples": [
null
]
}
这篇关于如何将对象数组定义为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!