7
---
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.stack.imgur.com/Hqta3.png

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

CrabMan
  • 1,578
  • 18
  • 37

2 Answers2

5

You can have self-references, but you probably don't use the allOf construct:

definitions:
  Thing:
    properties:
      parent_thing:
        $ref: '#/definitions/Thing'

The above is valid, if the swagger-editor is not showing it correctly, that is a bug.

fehguy
  • 6,724
  • 25
  • 23
  • 2
    Both ways show "undefined". Btw, do you think it's wrong to use allOf? I used it because I want to use custom description which tells what the property means. – CrabMan Mar 15 '16 at 07:34
  • 1
    Please file a ticket in swagger-editor. What is listed above is correct and valid, if it does not display, it needs to be fixed. – fehguy Mar 16 '16 at 14:40
  • It seems it's still not fixed; are you sure it's a bug? – Stefan Jun 05 '19 at 13:35
0

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'
    ...
MingalevME
  • 1,827
  • 1
  • 22
  • 19