cancel
Showing results for 
Search instead for 
Did you mean: 

Enum of defined objects

SOLVED
Highlighted
New Member

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 other definitions ($ref). The problem is, that the enum values are not shown in the presentation of the swagger. Is it only to be possible to make a description or is there another possibility?

I attach a demo swagger file where you can see that the colors in the Buble definition are empty. I expect the definitions of the three colors as a choice.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: Enum of defined objects

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


Helen Kosova
SmartBear Technical Writer
________________________

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!

View solution in original post

1 REPLY 1
Highlighted
Moderator

Re: Enum of defined objects

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


Helen Kosova
SmartBear Technical Writer
________________________

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!

View solution in original post