Skip to content

Is there is a way to merge enums into one flat enum, or list? #1320

Closed

Description

@nomadtechie

I am using the 2.0 spec and I have a field which I am using in a model as well as a path param where the value can be from one of three different enums. Using the syntax below is turning into an array with 3 enums. However, I would like it to be one flat enum. Is there any way to achieve this?

Thanks so much:

    properties:
      type:
        type: "string"
        enum: 
        - *unitTypes
        - *componentTypes
        - *snootTypes

Activity

  1. hkosova commented on Aug 15, 2017

    @hkosova
    Contributor

    YAML does not support this.

    where the value can be from one of three different enums.

    In OpenAPI 3.0 this can probably be handled using oneOf:

    components:
      schemas:
        Colors1:
          type: string
          enum: [red, green, blue]
        Colors2:
          type: string
          enum: [black, white]
        
        Color:
          oneOf:
            - $ref: '#/components/schemas/Colors1'
            - $ref: '#/components/schemas/Colors2'
  2. nomadtechie commented on Aug 15, 2017

    @nomadtechie
    Author

    Thanks so much @hkosova! That is helpful for my models, but can I also use this for my path param? Also, I'm assuming there is no way to do this in OpenAPI 2.0?

  3. hkosova commented on Aug 16, 2017

    @hkosova
    Contributor

    can I also use this for my path param?

    Yes. In 3.0, all parameters use a schema, so the syntax would be similar to models:

    - in: path
      name: color
      required: true
      schema:
        oneOf:
          - $ref: '#/components/schemas/Colors1'
          - $ref: '#/components/schemas/Colors2'

    I'm assuming there is no way to do this in OpenAPI 2.0?

    Correct. oneOf is a new feature in 3.0.

  4. riba2101 commented on Oct 29, 2020

    @riba2101

    just a note - this does not work, the resulting Color is an empty class, also there is a bug if you use allOf the parent extends the enum.

    the above mentioned is valid for java generator

  5. idromigny commented on Feb 12, 2021

    @idromigny

    Hello, just found this because I was searching for another solution than doing this solution of the oneOf. Just wanted to say that it's working for me at the condition I specify the type at the oneOf level too :

    - in: path
      name: color
      required: true
      schema:
        type: string
        oneOf:
          - $ref: '#/components/schemas/Colors1'
          - $ref: '#/components/schemas/Colors2'
    

    I'm using this for query parameter though. Don't know if it's changing anything about the fact that's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions