gpt4 book ai didi

swagger - 数组内的 OpenAPI 多种类型

转载 作者:行者123 更新时间:2023-12-01 19:18:54 32 4
gpt4 key购买 nike

我在使用 OpenAPI 3 定义可重用架构组件时遇到问题,该组件允许包含多种类型的数组。每个项目类型都继承自同一个父类,但具有特定的子属性。这似乎在 model 中工作正常在 SwaggerHub 上查看,但示例 View 未正确显示数据。

TLDR; 有没有办法在 OpenAPI 3 中定义包含不同对象类型的数组?

Response:
allOf:
- $ref: '#/components/schemas/BaseResponse'
- type: object
title: A full response
required:
- things
properties:
things:
type: array
items:
anyOf:
- $ref: '#/components/schemas/ItemOne'
- $ref: '#/components/schemas/ItemTwo'
- $ref: '#/components/schemas/ItemThree'

最佳答案

你的规范是正确的。这只是 oneOf 的示例渲染和 anyOf Swagger UI 尚不支持 schemas。您可以跟踪此问题以获取状态更新:

Multiple responses using oneOf attribute do not appear in UI

解决方法是手动添加 example旁边 oneOf/anyOf模式或父模式:

        things:
type: array
items:
anyOf:
- $ref: '#/components/schemas/ItemOne'
- $ref: '#/components/schemas/ItemTwo'
- $ref: '#/components/schemas/ItemThree'
# Note that array example is on the same
# level as `type: array`
example:
- foo: bar # Example of ItemOne
baz: qux
- "Hello, world" # Example of ItemTwo
- [4, 8, 15, 16, 23, 42] # Example of ItemThree

关于swagger - 数组内的 OpenAPI 多种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656791/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com