如何在OpenAPI/Swagger中递归引用类型定义? [英] How to refer to enclosing type definition recursively in OpenAPI / Swagger?
问题描述
我正在Swagger编辑器中编写OpenAPI定义.
I'm writing an OpenAPI definition in Swagger Editor.
我的一个类型定义包含一个数组,该数组包含与父类型相同类型的子元素. IE.像这样的东西:
One of my type definitions contains an array containing child elements of the same type as the parent. I.e. something like this:
definitions:
TreeNode:
type: object
properties:
name:
type: string
description: The name of the tree node.
children:
type: array
items:
$ref: '#/definitions/TreeNode'
但是,Swagger编辑器不会在children
数组中拾取递归引用,该数组只是显示为未定义"元素的数组.
However, Swagger Editor doesn't pick up the recursive reference in the children
array, which is simply shown as an array of "undefined" elements.
有人对此有想法吗?
推荐答案
您的定义非常好.在Swagger编辑器和Swagger UI中渲染递归模式是一个已知问题:
Your definition is perfectly fine. It's a known issue issue with rendering recursive schemas in Swagger Editor and Swagger UI:
https://github.com/swagger-api/swagger-ui/Issues/3325
要变通解决示例值"中的null
/未定义值,可以将自定义example
添加到您的架构中:
To work around the null
/undefined values in the "Example Value", you can add a custom example
to your schema:
definitions:
TreeNode:
type: object
properties:
name:
type: string
description: The name of the tree node.
children:
type: array
items:
$ref: '#/definitions/TreeNode'
example:
name: foo
children:
- name: bar
- name: baz
children:
- name: qux
这篇关于如何在OpenAPI/Swagger中递归引用类型定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!