Skip to content

Commit

Permalink
Merge pull request #2553 from Spissable/bug/nullable-enum-without-enu…
Browse files Browse the repository at this point in the history
…m-type

fix(plugin): Fix nullable enum not having a proper enum type
  • Loading branch information
kamilmysliwiec authored Aug 7, 2023
2 parents dd9bf76 + 6dad8f6 commit b73349e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 24 additions & 1 deletion test/plugin/fixtures/nullable.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const nullableDtoText = `
enum OneValueEnum {
ONE
}
export class NullableDto {
@ApiProperty()
stringValue: string | null;
Expand All @@ -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([
Expand All @@ -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);
`;

0 comments on commit b73349e

Please sign in to comment.