對象數組的定義如下:
type: array
items:
type: object
properties:
prop1:
type: string
prop2:
type: integer
# etc.
在您的示例中,響應包含具有屬性 balanceDisplaySettings 的對象,并且此屬性包含對象數組 . 這可以定義如下:
paths:
/Path:
get:
responses:
200:
description: OK
schema:
type: object
properties:
balanceDisplaySettings:
type: array
items:
type: object
properties:
type:
type: string
label:
type: string
visible:
type: boolean
primary:
type: boolean
請注意,架構定義了響應結構,這意味著您無需在任何位置指定實際值( "Balance" , "AvailableBalance" 等) . 但是,如果要在Swagger UI中顯示帖子(包含2個對象的數組)中的示例,可以像下面這樣添加它:
balanceDisplaySettings:
type: array
items:
type: object
properties:
type:
type: string
label:
type: string
visible:
type: boolean
primary:
type: boolean
example: #
- type: Balance
label: Current
visible: true
primary: false
- type: AvailableBalance
label: Available
visible: true
primary: true
最后,您可能希望拆分內聯嵌套模式以使規范更加模塊化 .
paths:
/Path:
get:
responses:
200:
description: OK
schema:
$ref: '#/definitions/MyResponseObject'
# |
definitions: # |
# TODO: better name # |
MyResponseObject: #
type: object
properties:
balanceDisplaySettings:
type: array
items:
$ref: '#/definitions/BalanceDisplaySetting'
example: # |
- type: Balance # |
label: Current # |
visible: true # |
primary: false # |
- type: AvailableBalance # |
label: Available # |
visible: true # |
primary: true # |
# |
BalanceDisplaySetting: #
type: object
properties:
type:
type: string
example: Balance
label:
type: string
example: Current
visible:
type: boolean
boolean:
type: boolean