swagger icon indicating copy to clipboard operation
swagger copied to clipboard

ApiImplicitQuery: collectionFormat ignored when enum is set and isArray is true

Open dfrechdev opened this issue 7 years ago • 5 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 use the ApiImplicitQuery decorator and set enum and isArray to true, the attribute collectionFormat is by default set to multi and cannot be overwritten. When used without enum and isArray only, the attribute collectionFormat is respected.

This


    @ApiImplicitQuery({
        name: 'role',
        isArray: true,
        collectionFormat: 'csv',
    })

for example results in ?role=Admin,Moderator.

In contrast, this


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

results in ?role=Admin&role=Moderator.

Expected behavior

The attribute collectionFormat is respected when set for enum arrays.

Minimal reproduction of the problem with instructions

See above examples

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

The collection format should also be selectable when enums are used. This has also been asked on stack overflow at https://stackoverflow.com/questions/55767782/nestjs-swagger-how-to-declare-multiselect-enum-field.

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 12:04 dfrechdev

Found that the reason is that multi is currently hard-coded in https://github.com/nestjs/swagger/blob/master/lib/decorators/api-implicit-query.decorator.ts at line 43. Is there a reason for this? If not, I can fix this and create a PR if you want.

dfrechdev avatar Apr 25 '19 12:04 dfrechdev

@dfrechdev I wasn't so sure when I submitted that PR for ApiImplicitQuery. I thought mainly because it had something to do with swagger-ui-express at the time regarding collectionFormat being multi.

nartc avatar Apr 27 '19 23:04 nartc

I can confirm this issue. @dfrechdev, could you open a PR with your fix?

fwoelffel avatar Jun 17 '19 10:06 fwoelffel

Fixed in 3.1.0

kamilmysliwiec avatar Jul 14 '19 14:07 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 12:04 lock[bot]