From 6dad8f6e8156e655b652ae27874493dee9d48730 Mon Sep 17 00:00:00 2001 From: "Spiss, Lukas" Date: Sat, 5 Aug 2023 09:28:53 +0100 Subject: [PATCH] fix(plugin): Fix nullable enum not having a proper enum type --- lib/plugin/utils/plugin-utils.ts | 2 +- test/plugin/fixtures/nullable.dto.ts | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/plugin/utils/plugin-utils.ts b/lib/plugin/utils/plugin-utils.ts index 2bf55a59b..3608931f3 100644 --- a/lib/plugin/utils/plugin-utils.ts +++ b/lib/plugin/utils/plugin-utils.ts @@ -246,7 +246,7 @@ export function isAutoGeneratedEnumUnion( return undefined; } const undefinedTypeIndex = type.types.findIndex( - (type: any) => type.intrinsicName === 'undefined' + (type: any) => type.intrinsicName === 'undefined' || type.intrinsicName === 'null' ); if (undefinedTypeIndex < 0) { return undefined; diff --git a/test/plugin/fixtures/nullable.dto.ts b/test/plugin/fixtures/nullable.dto.ts index c94ea4819..86572b2c8 100644 --- a/test/plugin/fixtures/nullable.dto.ts +++ b/test/plugin/fixtures/nullable.dto.ts @@ -1,4 +1,8 @@ export const nullableDtoText = ` +enum OneValueEnum { + ONE +} + export class NullableDto { @ApiProperty() stringValue: string | null; @@ -8,13 +12,23 @@ export class NullableDto { optionalString?: string; @ApiProperty() undefinedString: string | undefined; + @ApiProperty() + nullableEnumValue: OneValueEnum | null; + @ApiProperty() + optionalEnumValue?: OneValueEnum; + @ApiProperty() + undefinedEnumValue: OneValueEnum | undefined; } `; export const nullableDtoTextTranspiled = `import * as openapi from "@nestjs/swagger"; +var OneValueEnum; +(function (OneValueEnum) { + OneValueEnum[OneValueEnum["ONE"] = 0] = "ONE"; +})(OneValueEnum || (OneValueEnum = {})); export class NullableDto { static _OPENAPI_METADATA_FACTORY() { - return { stringValue: { required: true, type: () => String, nullable: true }, stringArr: { required: true, type: () => [String], nullable: true }, optionalString: { required: false, type: () => String }, undefinedString: { required: true, type: () => String } }; + return { stringValue: { required: true, type: () => String, nullable: true }, stringArr: { required: true, type: () => [String], nullable: true }, optionalString: { required: false, type: () => String }, undefinedString: { required: true, type: () => String }, nullableEnumValue: { required: true, nullable: true, enum: OneValueEnum }, optionalEnumValue: { required: false, enum: OneValueEnum }, undefinedEnumValue: { required: true, enum: OneValueEnum } }; } } __decorate([ @@ -29,4 +43,13 @@ __decorate([ __decorate([ ApiProperty() ], NullableDto.prototype, "undefinedString", void 0); +__decorate([ + ApiProperty() +], NullableDto.prototype, "nullableEnumValue", void 0); +__decorate([ + ApiProperty() +], NullableDto.prototype, "optionalEnumValue", void 0); +__decorate([ + ApiProperty() +], NullableDto.prototype, "undefinedEnumValue", void 0); `;