数组内的OpenAPI多种类型 [英] OpenAPI multiple types inside an array
问题描述
我在使用OpenAPI 3定义可重用架构组件时遇到麻烦,该组件允许包含多个类型的数组.每种项目类型都继承自相同的父类,但是具有特定的子级属性.在SwaggerHub的model
视图中,这似乎可以正常工作,但是示例视图无法正确显示数据.
I'm having trouble defining a reusable schema component using OpenAPI 3 which would allow for an array that contains multiple types. Each item type inherits from the same parent class but has specific child properties. This seems to work alright in the model
view on SwaggerHub but the example view doesn't show the data correctly.
TLDR; 是否可以在OpenAPI 3中定义包含不同对象类型的数组?
TLDR; Is there a way to define an array containing different object types in 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'
推荐答案
您的规范正确.仅仅是Swagger UI中尚不支持oneOf
和anyOf
模式的示例渲染.您可以跟踪这些问题以进行状态更新:
Your spec is correct. It's just that example rendering for oneOf
and anyOf
schemas is not yet supported in Swagger UI. You can track these issues for status updates:
OAS 3.0支持积压
使用oneOf属性的多个响应不会出现在UI中
解决方法是在oneOf
/anyOf
架构旁边或父架构中手动添加example
:
The workaround is to manually add an example
alongside the oneOf
/anyOf
schema or to the parent schema:
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
这篇关于数组内的OpenAPI多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!