Forum Discussion

j2020's avatar
j2020
New Member
5 years ago

Enum of defined objects

Hello, I'm not sure if this is the right board but I want to define a property of a definition which should have a value from an enum but this enum is of object type. And its values should be of othe...
  • HKosova's avatar
    5 years ago

    An enum of objects is defined as follows. The enum items are object literals (i.e. using property_name: value syntax), not Schema Objects.

    enum:
      - red: 255   # Red
        green: 0
        blue: 0
      - red: 0     # Green
        green: 255
        blue: 0
      - red: 0     # Blue
        green: 0
        blue: 255

    If these are the only allowed color values you can add this enum to the base Color schema. Otherwise you can use allOf to create a schema that extends the Color schema with this enum, like so:

    definitions:
      Buble:
        type: object
        description: Demo object with the wished enum
        properties:
          color:
            $ref: '#/definitions/colors'
    
      colors:
        description: Eine Auswahl von Farben
        allOf:
          - $ref: '#/definitions/Color'
          - enum:
              - red: 255    # Red
                green: 0
                blue: 0
              - red: 0      # Green
                green: 255
                blue: 0
              - red: 0      # Blue
                green: 0
                blue: 255
    
      Color:
        type: object
        description: General color object
        properties:
          red:
            type: integer
          green:
            type: integer
          blue:
            type: integer

     


    j2020 wrote:

    The problem is, that the enum values are not shown in the presentation of the swagger.


    This is a limitation of Swagger Editor and Swagger UI - they do not support enums of objects. You can submit an enhancement request here:

    https://github.com/swagger-api/swagger-ui/issues