1

I want to define enum in openAPI.

I looked at this post:

How to define an enum in OpenAPI (Swagger)?

and I want to be able to see the enum like:

enter image description here

I'm working with components and I define it as:

components:
  
  schemas:
  
    FilterImg:
      type: object
      properties:
          name:
            type: string
            enum: ["img_1", "img_2"]
          value:
            type: string

And I'm using it:

post:
      summary: Add new img
      tags:
        - img
      description: Lets a user post a new img
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FilterImg'
      responses:
        '200':
          description: Successfully
    

But I can't see the enum as enum scroll menu (in the web browser), as we can see it in the example.

What am I missing ?

user3668129
  • 4,318
  • 6
  • 45
  • 87
  • What are you getting right now in output? Why you want to show `enum` in response? It will always show an object., and check your schema `FilterImg` you have defined its `type: object` so it will show in object view – turivishal Jul 02 '20 at 05:41
  • Check the "Schema" tab in the response. – Helen Jul 02 '20 at 06:52
  • i updated the code (set the enum in post and not in get) – user3668129 Jul 02 '20 at 07:07
  • Possible duplicate of [Only first element of Enum list is displayed instead of Entire list in Swagger](https://stackoverflow.com/q/54104552/113116) – Helen Jul 02 '20 at 13:18

2 Answers2

0

You are passing application/json in content type and you want enum dropdown how its possible?

You need to learn more about swagger from this documentation Swagger Docs and Openapi Specification,

Anyway i got an idea, you need this dropdown in body so here i have just added in application/x-www-form-urlencoded content type:

post:
  summary: Add new img
  tags:
    - img
  description: Lets a user post a new img
  requestBody:
    required: true
    content:
      application/x-www-form-urlencoded:
        schema:
          $ref: '#/components/schemas/FilterImg'
  responses:
    '200':
      description: Successfully

Remaining things will be same.

It will looks like:

enter image description here

turivishal
  • 34,368
  • 7
  • 36
  • 59
-1

In case of JSON/XML request and response bodies, the enum values for individual body fields are displayed in the schema documentation, that is on the Schema tab (or Model tab in case of OpenAPI 2.0).

Enum values for a JSON body field

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Dear @Helen, i respect you, the questioner need a scroll menu, for particular image choices or selection purpose, your pointed schema view will just view it will not allow to select particular choice, if i am wrong can you please explain how its my answer not useful? – turivishal Jul 02 '20 at 17:11