参考下面的示例,我想在其定义中提供NamedElementArray
的示例。这需要显示NamedElement
属性的elements
数组的示例。
我该怎么做?我无法在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
答案 0 :(得分:9)
您必须在高级示例和低级示例之间进行选择。 在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"
}
]
}