我对 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 中,它没有显示成员变量及其默认值。而是将其显示为空:
{
"samples": [
null
]
}