2

I am trying to create an endpoint that returns the possible values of an enum with OpenAPI specification and I don't see how to do it reusing the already defined enum.

Is it possible to do it and if so what should be the best practice?

Edit:

This is how I am doing it now. It is creating correct clients but the examples are wrong as it is creating an array with just one of the elements from the enumeration.

Enum:

DateGenerationRule:
  type: string
  enum: &DATEGENERATIONRULE
    - Backward
    - CDS
    - Forward
    - OldCDS
    - ThirdWednesday
    - Twentieth
    - TwentiethIMM
    - Zero

Response:

GetDateGenerationRulesResponse:
  type: object
  properties:
    message:
      type: object
      properties:
        ...
        BusinessDayConvention:
          type: array
          items:
            $ref: '#/components/schemas/DateGenerationRule'
        ...
joseprupi
  • 317
  • 4
  • 13
  • 1
    Can you post some examples of what you've tried so far? Does [this](https://swagger.io/docs/specification/data-models/enums/) help? – Helen May 04 '20 at 12:09
  • Thanks @Helen. I've added how I am doing it now. – joseprupi May 04 '20 at 17:45
  • 1
    Your example is correct. As for "it is creating an array with just one of the elements from the enumeration", [see here](https://stackoverflow.com/a/46580685/113116) to learn how to define/display an array _example_ with multiple items. – Helen May 04 '20 at 18:43
  • I know how to create an example with all the elements from the enum the thing is my API has few enumerations and with this solution I have to maintain two "lists" in my specs. I guess my question was if there is some way to just have one list with something lime a constant – joseprupi May 04 '20 at 19:25
  • Is there a specific reason you want to list _all_ of the enum values in the _examples_? Documentation renderers (Swagger UI, ReDoc, etc) typically display all data types, including possible enum values, as part of the schema documentation. – Helen May 05 '20 at 13:50
  • Actually is just a documentation requirement for me, I mean I think it looks better if you can see in the examples the correct values. I don't see defining constant values something is gonna happen in openapi specs though, you can not even define a single string constant (you have to create an array with just one value), which accomplishes its purpose but IMHO to document and maintain the API it is a little bit awkward. Without being able to define constants for lists the examples are wrong and for a single value you will see in the schema an object that is an array with just one element. – joseprupi May 05 '20 at 15:21

0 Answers0