Skip to content

Array of enums not working as expected #2179

Closed
@mrsolarius

Description

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Adding an array of enums with the ApiQuery does not seem to work correctly. For example, given the following enum:

export enum UserRole {
  GHOST = "ghost",
  CANDIDATE = "candidate",
  LITOPIEN = "litopien",
  BAN="ban",
  REFUSED = "refuse",
  LITOGOD = "litogod",
}

I want to use this with a model property as follows:

  @Get('by-roles')
  @ApiQuery({
    name:'userRoles',
    enum:UserRole,
    isArray:true
  })
  async getUserByRoles(@Query('userRoles')userRoles:UserRole[]=[UserRole.GHOST])

This does not render as a multi select in swagger UI like you can see here :
image

Also in the json file it looks like that :

"/api/users/by-roles":{
         "get":{
            "operationId":"UsersController_getUserByRoles",
            "parameters":[
               {
                  "name":"userRoles",
                  "required":true,
                  "in":"query",
                  "schema":{
                     "type":"array",
                     "items":{
                        "type":"string"
                     }
                  }
               }
            ],

Minimum reproduction code

If you really need one, please let me know, I will provide one ^^

Steps to reproduce

No response

Expected behaviour

I expect the json to looks like that :

"/api/users/by-roles":{
      "get":{
        "operationId":"UsersController_getUserByRoles",
        "parameters":[
          {
            "name":"userRoles",
            "required":true,
            "in":"query",
            "schema":{
              "type":"array",
              "items":{
                "type":"string",
                "enum":[
                  "ghost",
                  "candidate",
                  "litopien",
                  "ban",
                  "refuse",
                  "litogod"
                ]
              }
            }
          }
        ]

And the swagger ui to look like this :
image

Package version

@nestjs/swagger : 6.1.3

NestJS version

9.0.0

Node.js version

v16.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

If I do this :

@Get('by-roles')
  @ApiQuery({
    name:'userRoles',
    enum:UserRole,
    isArray:true
  })
  async getUserByRoles(@Query()userRoles:UserRole[]=[UserRole.GHOST])

I got the correct JSON, but userRoles is not an array of UserRole But a weird object that looks like that :

{ userRoles: [ 'ghost', 'candidate', 'litopien', 'litogod' ] }

Also if I juste send one parameter it juste return without array like that :

  { userRoles: 'ghost' }

My example is extremely close to this doc example : https://docs.nestjs.com/openapi/types-and-parameters#enums

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions