Andrey Radkevich
Andrey Radkevich

Reputation: 927

@ApiImplicitQuery swagger - can't configure 'enum'

You can see in the nest swagger documentation how to configure enums, but it isn't working.

import {SwaggerEnumType} from '../types/swagger-enum.type';

export declare const ApiImplicitQuery: (metadata: {
    name: string;
    description?: string;
    required?: boolean;
    type?: 'String' | 'Number' | 'Boolean' | any;
    enum?: SwaggerEnumType;
    isArray?: boolean;
    collectionFormat?: "csv" | "ssv" | "tsv" | "pipes" | "multi";
}) => MethodDecorator;

Above you can see what I've changed in the code of the swagger module. Below you can see my code.

 @ApiImplicitQuery({name: "orderBy", enum: ['Admin', 'Moderator', 'User']})
    @ApiResponse({status: 200, description: "Successful getting registration list for admin"})
    @Get('registrations/list')
    public async getAdmin(@Req() req, @Res() res: Response,
                          @Query('page') page: number,
                          @Query('perPage') perPage: number,
                          @Query('orderBy') orderBy: UserRole = UserRole.User,
                          @Query('orderDir') orderDir: number
    ) {
        try {
            let token = await getCompaignIdFromAdminToken(req.headers['authorization']);
            let helper: HelperClass = new HelperClass();
            await helper.isUserAdmin(token);
            let users = await this.adminFacade.getAllUsersList(orderBy, orderDir);
            let entity = await this.adminFacade.parseRegistrationUsersList(users);
            let pagePar = (page) ? page : 0;
            let perPagePar = (perPage) ? perPage : users.length;
            let response = await returnResponseByPageAndPerPageValues(entity, pagePar, perPagePar);
            res.status(HttpStatus.OK).json({response: response, entries: users.length});
        } catch (err) {
            errorResponse(res, err.message, HttpStatus.BAD_REQUEST);
        }
    }

export enum UserRole {
    Admin = 'Admin',
    Moderator = 'Moderator',
    User = 'User'
}

And below you can see the generated swagger docs: Sample Image

What am I doing wrong?

Upvotes: 3

Views: 2464

Answers (2)

Kim Kern
Kim Kern

Reputation: 60557

Update August 18

The pull request was just merged and is available in version v5.2.0.

Original post

The pull request introducing enum support for ApiImplicitQuery is not yet merged, hence the feature is not yet available. Unfortunately, the docs were updated too early:

The doc is already update but not the code.

Follow the pull request to get notified when the feature will be available. Then you can update the @nestjs/swagger package.

Upvotes: 2

Chau Tran
Chau Tran

Reputation: 5108

Not exactly an answer and also I want this to be seen a bit more explicitly than a comment.

I am the author of the PR for this feature. I submitted the PR even before submitting the PR for the related portion on the Documentations. However, the related docs' PR was merged without the implementation PR being merged. It has been over a month and I haven't heard anything from Kamil (NestJS's author). I assume he is just too busy at the moment as there are only two people working on the whole framework: Kamil himself and ThomRick (NestCLI).

I hope, with enough requests and pressure, Kamil would actually look at the PRs on @nestjs/swagger. For now, I'd suggest you to handle your Controller to expect an exact string unfortunately.

Upvotes: 1

Related Questions