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 ?

4
  • 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 Commented Jul 2, 2020 at 5:41
  • Check the "Schema" tab in the response. Commented Jul 2, 2020 at 6:52
  • i updated the code (set the enum in post and not in get) Commented Jul 2, 2020 at 7:07
  • Possible duplicate of Only first element of Enum list is displayed instead of Entire list in Swagger Commented Jul 2, 2020 at 13:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

-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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.