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 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.

 

  • 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

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    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

    • lawnchairs's avatar
      lawnchairs
      New Member

      I want to take this one step further. I want to specify a finite list of values as enumerable that are derived from a single property on an array of existing objects. I have two files: the schema, and an external json file containing reference data.

       

      In my reference file, I have data:

      “Countries”: [
      	{
      		“name”: “United States of America”,
      		“code3”: “USA”,
      		“code2”: “US”
      	},
      	{
      		“name”: “Canada”,
      		“code3”: “CAN”,
      		“code2”: “CA”
      	},
      	{
      		“name”: “Mexico”,
      		“code3”: “MEX”,
      		“code2”: “MX”
      	}
      ]

       

      And what I want to do is specify that the correct set of enumerable values are the values in "code2". Is there a way to specify this without having to rip apart my reference files? Like so:

      “get”: {
      	“parameters”: [
      		“countryCode2”: {
      			“in”: “query”,
      			“schema”: {
      				“$ref”: “referencefile.json/#/Countries/code2”
      			}
      		}
      	]
      }