Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nomadtechie opened this issue Aug 15, 2017 · 5 comments
Closed

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

nomadtechie opened this issue Aug 15, 2017 · 5 comments

Comments

@nomadtechie
Copy link

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
@hkosova
Copy link
Contributor

hkosova commented Aug 15, 2017

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'

@nomadtechie
Copy link
Author

nomadtechie commented Aug 15, 2017

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?

@hkosova
Copy link
Contributor

hkosova commented Aug 16, 2017

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.

@riba2101
Copy link

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

@Yaronn44
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants