swagger icon indicating copy to clipboard operation
swagger copied to clipboard

ApiModelProperty with enum and isArray does not create multi-select

Open dfrechdev opened this issue 7 years ago • 4 comments

I'm submitting a...


[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When you create a query parameter with @ApiImplicitQuery that uses enum and has the property "isArray" set as true, a multi-select is created in the Swagger API, as described and shown in https://docs.nestjs.com/recipes/swagger#working-with-enums:


@ApiImplicitQuery({
    name: 'role',
    enum: ['Admin', 'Moderator', 'User'],
    isArray: true,
})

When you move this to a Dto, the same settings with ApiModelProperty or ApiModelPropertyOptional however do not create a multi-select, but a string array instead:


@ApiModelProperty({
    enum: ['Admin', 'Moderator', 'User'],
    isArray: true,
})
readonly role: string[];

Expected behavior

That the same settings in ApiImplicitQuery and ApiModelProperty result in the same multi-select in the Swagger API.

Minimal reproduction of the problem with instructions

  • create a controller
  • add a parameter implicitly, as the one above with ApiImplicitQuery => results in a multi-select
  • refactor the parameter to a Dto with ApiModelProperty or ApiModelPropertyOptional => results in a string array

What is the motivation / use case for changing the behavior?

I would like to use the Dto for mulit-selecting values, rather than having to define the parameters implicitly. I also found the same question asked on https://stackoverflow.com/questions/55767782/nestjs-swagger-how-to-declare-multiselect-enum-field by another user, but could not find a corresponding issue.

Environment

"@nestjs/common": "^6.1.1", "@nestjs/core": "^6.1.1", "@nestjs/platform-express": "^6.1.1", "@nestjs/swagger": "^3.0.2", "swagger-ui-express": "^4.0.2"

dfrechdev avatar Apr 25 '19 11:04 dfrechdev

Same issue here: no multi-select.

It seems related to the generation of the enum field which should be under items in the Swagger json. See: https://github.com/swagger-api/swagger-ui/issues/4400#issuecomment-378237406

Nest-swagger 3.1.0 Nest: 6.6.3

tomsquest avatar Sep 11 '19 12:09 tomsquest

Fixed in the next version (to install run npm i @nestjs/swagger@next). Note: remember to update @nestjs/common, @nestjs/core and typescript as well to ensure that you're using the latest versions.

Steps to migrate: https://github.com/nestjs/swagger/pull/355#issuecomment-547925879

kamilmysliwiec avatar Oct 30 '19 14:10 kamilmysliwiec

4.0.0 has been published

kamilmysliwiec avatar Dec 03 '19 10:12 kamilmysliwiec

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Apr 25 '20 10:04 lock[bot]