---
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/:
get:
responses:
200:
description: OK
definitions:
Thing:
properties:
parent_thing:
allOf:
- $ref: '#/definitions/Thing'
description: parent of this thing
下面是一个最小示例。如果我在 swagger-editor 中写这个,它表明parent_thing的类型为 undefined
: https://i.stack.imgur.com/Hqta3.png
我该如何解决这个问题?我希望Thing
引用其他Thing
。
你可以有自引用,但你可能不使用allOf
结构:
definitions:
Thing:
properties:
parent_thing:
$ref: '#/definitions/Thing'
以上是有效的,如果 swagger-editor 没有正确显示它,那就是一个错误。
您可以通过代理模型 (https://stackoverflow.com/a/59047433/1046909) 来实现这一点:
...
_MessageProxy:
description: Message
type: object
required:
- id
- user
- body
- publishedAt
properties:
id:
title: Message id
type: string
readOnly: true
example: '595f4acf828b0b766ad11290'
user:
$ref: '#/components/schemas/User'
Message:
allOf:
- $ref: '#/components/schemas/_MessageProxy'
- type: object
properties:
parent:
title: Parent
readOnly: true
allOf:
- $ref: '#/components/schemas/_MessageProxy'
...