如何解决SwaggerOpenApi 3.0.0中的关联数组
我有一个API,它总是用一个关联数组来回答,其中只有一个带有键“数据”的条目包含最终结果。结果可以是一个对象或对象数组。这是API输出:
{
"data": {
"id": 1,"name": "product1 name","type": "type a","created_at": "2011-09-28T13:20:15+02:00"
}
}
{
"data": [
{
"id": 1,"created_at": "2010-09-28T13:20:15+02:00"
},{
"id": 6,"name": "product6 name","type": "type f","created_at": "2010-09-28T13:20:28+02:00"
},{
"id": 17,"name": "product17 name","type": "type Q","created_at": "2010-09-28T13:20:42+02:00"
}
]
}
我如何大张旗鼓地在文档中显示密钥的名称数据?
就目前而言,我仅能大幅度地获取嵌套数组:
[
{
"id": 1,"created_at": "2010-09-28T13:20:15+02:00"
}
]
[
[
{
"id": 1,"type": "type_a","created_at": "2010-01-01T18:21:20+02:00"
}
]
]
我需要数据键,因为它从API返回时会大张旗鼓地显示。可以吗?我还没有找到任何解决方案...:/
我的Yaml文件的一部分:
openapi: "3.0.0"
info:
version: 1.0.0
paths:
/products/:
get:
summary: List all available products
operationId: listProducts
tags:
- products
responses:
200:
description: Array of products
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Products"
/products/{product_id}:
get:
summary: Get specific product
operationId: showProductById
tags:
- products
parameters:
- name: product_id
in: path
required: true
description: The id of the product to retrieve
schema:
type: integer
format: int32
example: 1
responses:
200:
description: Product object
content:
application/json:
schema:
type: array
properties:
datar: string
items:
$ref: "#/components/schemas/Product"
components:
schemas:
Product:
type: object
properties:
id:
type: integer
format: int32
example: 10
name:
type: string
example: "producta name"
type:
type: string
example: "type a"
created_at:
type: string
format: date
example: "2010-01-01T18:21:20+02:00"
Products:
type: array
items:
$ref: "#/components/schemas/Product"
任何帮助将不胜感激:)
欢呼
解决方法
尝试在关联数组的中间层中使用“additionalProperties”,来源:https://swagger.io/docs/specification/data-models/dictionaries/
components:
schemas:
Product:
type: object
properties:
id:
type: integer
format: int32
example: 10
name:
type: string
example: "producta name"
type:
type: string
example: "type a"
created_at:
type: string
format: date
example: "2010-01-01T18:21:20+02:00"
ArrayOfProduct:
type: object
additionalProperties:
$ref: "#/components/schemas/Product"
Products:
type: object
properties:
data:
type: object
$ref: "#/components/schemas/ArrayOfProduct"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。