swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Swagger plugin v4 not recognizing enum as a key in validated object

Open karer opened this issue 5 years ago • 7 comments

Bug Report

Current behavior

I've updated my code to use swagger v4 with plugin, so it exposes DTO validations automatically. Everything work correctly, unless i use enum as a key of validated object. In output code, enum is not being imported and nest assumes it is global.

Input Code

import { IsBoolean, IsOptional } from 'class-validator';
import { ReservationReminderType } from '../../../shared/reservation/enums/reservation.reminder.type.enum';

export class UpdateShopNotifiactionsDto {
  @IsBoolean()
  @IsOptional()
  [ReservationReminderType.OneDayBefore]?: boolean;
}

Output Code (compiled)

const openapi = require("@nestjs/swagger");
const class_validator_1 = require("class-validator");
const reservation_reminder_type_enum_1 = require("../../../shared/reservation/enums/reservation.reminder.type.enum");

class UpdateShopNotifiactionsDto {
    static _OPENAPI_METADATA_FACTORY() {
        return { [ReservationReminderType.OneDayBefore]: { required: false, type: () => Boolean }, };
    }
}

Expected behavior

Output code should use reservation_reminder_type_enum_1 in its static _OPENAPI_METADATA_FACTORY() method.

Error

(node:22659) UnhandledPromiseRejectionWarning: ReferenceError: ReservationReminderType is not defined
    at Function._OPENAPI_METADATA_FACTORY ([...]/shop.notifications.update.dto.js:18:19)

Environment


Nest version: 6.10.13

 
For Tooling issues:
- Node version: 10.16.0  
- Platform:  Mac, Linux

Others:
Build works correctly, error happens at runtime (dev & prod start)

karer avatar Jan 02 '20 17:01 karer

4.1.7 should ignore computed properties. That means, the error won't happen at runtime anymore but you'll have to manually add @ApiProperty.

kamilmysliwiec avatar Jan 08 '20 11:01 kamilmysliwiec

I'm encountering a similar issue while trying to uprade from @nestjs/swagger 3.x to 4.x. In our case we aren't using enums as a key, but just as the type of the variable. It occurs when we use an enum that is imported from another npm module. The error is similar, and it seems to not be able to resolve the source file.

arvinsingla avatar Jan 20 '20 14:01 arvinsingla

@arvinsingla, please, create a separate issue with a repository which reproduces your problem.

kamilmysliwiec avatar Jan 20 '20 14:01 kamilmysliwiec

It seems to happen when importing any type of value. In my case when importing constants as the value for MinLength().

IGassmann avatar Feb 01 '20 15:02 IGassmann

@IGassmann, please, create a separate issue with a repository which reproduces your problem.

kamilmysliwiec avatar Feb 02 '20 11:02 kamilmysliwiec

@kamilmysliwiec done here https://github.com/nestjs/swagger/issues/543

IGassmann avatar Feb 02 '20 16:02 IGassmann

@karer is this still an issue with the latest version?

nartc avatar Nov 14 '20 04:11 nartc