How to reference self in a definition?

Viewed 4796
---
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

Here is the minimal example. If I write this in swagger-editor, it shows that parent_thing is of type undefined: https://i.imgur.com/OGHlKxg.png

How do I fix that? I want Thing to have a reference to other Things.

2 Answers

You can achieve that by a proxy model (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'
    ...
Related