如何在 swagger 中提供包含数组作为其属性之一的对象定义示例
How to provide an example of an object definition in swagger that contains an array as one of its attributes
参考下面的例子,我想在其定义中提供一个NamedElementArray
的例子。这将需要显示 elements
属性的 NamedElement
数组示例。
我该怎么做?我在 swagger 规范中找不到有关如何执行此操作的详细信息。
swagger: '2.0'
info:
version: "0.0.0"
title: Example
definitions:
Identifier:
type: string
format: uuid
NamedElement:
type: object
properties:
name:
type: string
identifier:
$ref: "#/definitions/Identifier"
required:
- name
- identifier
example:
name: Identifier1
identifier: ab804529-11d0-4781-a49a-3bbbc40243df
NamedElementArray:
type: object
properties:
name:
type: string
elements:
type: array
minLength: 0
items:
$ref: "#/definitions/NamedElement"
required:
- name
- elements
example:
name: Fred
elements:
paths:
/elements/{name}:
get:
description: |
Gets `NamedElement` objects, based on the **name** query param.
parameters:
-
name: name
in: path
description: Name of element array to return
required: true
type: string
responses:
200:
description: Returns a named element array
schema:
$ref: "#/definitions/NamedElementArray"
default:
description: Return nothing
您必须在高级别示例和低级别示例之间做出选择。
在 Swagger UI 中,高级示例先于本地示例。
上提供完整示例
你可以在每个属性(低级别)上定义一个例子:
Identifier:
type: string
format: uuid
example: Local UUID example
NamedElement:
type: object
properties:
name:
type: string
example: Local identifier example
identifier:
$ref: "#/definitions/Identifier"
required:
- name
- identifier
NamedElementArray:
type: object
properties:
name:
type: string
example: Local name example
elements:
type: array
minLength: 0
items:
$ref: "#/definitions/NamedElement"
required:
- name
- elements
在那种情况下,示例在 Swagger 中将如下所示 UI:
{
"name": "Local name example",
"elements": [
{
"name": "Local identifier example",
"identifier": "Local UUID example"
}
]
}
但是您也可以像在 NamedElement
:
上的示例中那样给出一个完整的高级示例
NamedElementArray:
type: object
properties:
name:
type: string
elements:
type: array
minLength: 0
items:
$ref: "#/definitions/NamedElement"
required:
- name
- elements
example:
name: Fred
elements:
- name: Identifier1
identifier: ab804529-11d0-4781-a49a-3bbbc40243df
- name: Identifier2
identifier: zzz4529-11d0-4781-a49a-3bbbc40243df
在那种情况下,示例在 Swagger 中将如下所示 UI:
{
"name": "Fred",
"elements": [
{
"name": "Identifier1",
"identifier": "ab804529-11d0-4781-a49a-3bbbc40243df"
},
{
"name": "Identifier2",
"identifier": "zzz4529-11d0-4781-a49a-3bbbc40243df"
}
]
}
参考下面的例子,我想在其定义中提供一个NamedElementArray
的例子。这将需要显示 elements
属性的 NamedElement
数组示例。
我该怎么做?我在 swagger 规范中找不到有关如何执行此操作的详细信息。
swagger: '2.0'
info:
version: "0.0.0"
title: Example
definitions:
Identifier:
type: string
format: uuid
NamedElement:
type: object
properties:
name:
type: string
identifier:
$ref: "#/definitions/Identifier"
required:
- name
- identifier
example:
name: Identifier1
identifier: ab804529-11d0-4781-a49a-3bbbc40243df
NamedElementArray:
type: object
properties:
name:
type: string
elements:
type: array
minLength: 0
items:
$ref: "#/definitions/NamedElement"
required:
- name
- elements
example:
name: Fred
elements:
paths:
/elements/{name}:
get:
description: |
Gets `NamedElement` objects, based on the **name** query param.
parameters:
-
name: name
in: path
description: Name of element array to return
required: true
type: string
responses:
200:
description: Returns a named element array
schema:
$ref: "#/definitions/NamedElementArray"
default:
description: Return nothing
您必须在高级别示例和低级别示例之间做出选择。 在 Swagger UI 中,高级示例先于本地示例。
上提供完整示例你可以在每个属性(低级别)上定义一个例子:
Identifier:
type: string
format: uuid
example: Local UUID example
NamedElement:
type: object
properties:
name:
type: string
example: Local identifier example
identifier:
$ref: "#/definitions/Identifier"
required:
- name
- identifier
NamedElementArray:
type: object
properties:
name:
type: string
example: Local name example
elements:
type: array
minLength: 0
items:
$ref: "#/definitions/NamedElement"
required:
- name
- elements
在那种情况下,示例在 Swagger 中将如下所示 UI:
{
"name": "Local name example",
"elements": [
{
"name": "Local identifier example",
"identifier": "Local UUID example"
}
]
}
但是您也可以像在 NamedElement
:
NamedElementArray:
type: object
properties:
name:
type: string
elements:
type: array
minLength: 0
items:
$ref: "#/definitions/NamedElement"
required:
- name
- elements
example:
name: Fred
elements:
- name: Identifier1
identifier: ab804529-11d0-4781-a49a-3bbbc40243df
- name: Identifier2
identifier: zzz4529-11d0-4781-a49a-3bbbc40243df
在那种情况下,示例在 Swagger 中将如下所示 UI:
{
"name": "Fred",
"elements": [
{
"name": "Identifier1",
"identifier": "ab804529-11d0-4781-a49a-3bbbc40243df"
},
{
"name": "Identifier2",
"identifier": "zzz4529-11d0-4781-a49a-3bbbc40243df"
}
]
}