nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.7k stars 476 forks source link

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

Closed dfrechdev closed 5 years ago

dfrechdev commented 5 years ago

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 commented 5 years ago

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.

nartc commented 5 years ago

@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.

fwoelffel commented 5 years ago

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

kamilmysliwiec commented 5 years ago

Fixed in 3.1.0

lock[bot] commented 4 years ago

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.