如何在定义中引用自我?

Cra*_*Man 5 swagger

---
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
Run Code Online (Sandbox Code Playgroud)

这是最小的例子.如果我在swagger-editor中编写它,则表明parent_thing的类型为undefined:https://i.imgur.com/OGHlKxg.png

我该如何解决这个问题?我想Thing引用其他Things.

feh*_*guy 5

您可以拥有自引用,但您可能不使用该allOf构造:

definitions:
  Thing:
    properties:
      parent_thing:
        $ref: '#/definitions/Thing'
Run Code Online (Sandbox Code Playgroud)

以上是有效的,如果swagger-editor没有正确显示,那就是一个bug.

  • 两种方式都显示"未定义".顺便问一下,你认为使用allOf是不对的吗?我使用它是因为我想使用自定义描述来说明属性的含义. (2认同)