Skip to content

Commit

Permalink
fix(plugin): fix boolean field marked as object (strict) nestjs#814
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 22, 2020
1 parent 358692d commit b1059a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export function getTypeReferenceAsString(
if (text === Date.name) {
return text;
}
if (isOptionalBoolean(text)) {
return Boolean.name;
}
if (
isAutoGeneratedTypeUnion(type) ||
isAutoGeneratedEnumUnion(type, typeChecker)
Expand Down Expand Up @@ -241,3 +244,11 @@ export function extractTypeArgumentIfArray(type: ts.Type) {
isArray: false
};
}

/**
* when "strict" mode enabled, TypeScript transform optional boolean properties to "boolean | undefined"
* @param text
*/
function isOptionalBoolean(text: string) {
return typeof text === 'string' && text === 'boolean | undefined';
}
4 changes: 2 additions & 2 deletions test/plugin/fixtures/create-cat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CreateCatDto {
readonly breed?: string;
nodes: Node[];
optionalBoolean?: boolean;
date: Date;
@ApiHideProperty()
Expand All @@ -58,7 +58,7 @@ let CreateCatDto = /** @class */ (() => {
this.status = Status.ENABLED;
}
static _OPENAPI_METADATA_FACTORY() {
return { name: { required: true, type: () => String }, age: { required: true, type: () => Number, default: 3, minimum: 0, maximum: 10 }, tags: { required: true, type: () => [String] }, status: { required: true, default: Status.ENABLED, enum: Status }, status2: { required: false, enum: Status }, statusArr: { required: false, enum: Status, isArray: true }, oneValueEnum: { required: false, enum: OneValueEnum }, oneValueEnumArr: { required: false, enum: OneValueEnum }, breed: { required: false, type: () => String }, nodes: { required: true, type: () => [Object] }, date: { required: true, type: () => Date } };
return { name: { required: true, type: () => String }, age: { required: true, type: () => Number, default: 3, minimum: 0, maximum: 10 }, tags: { required: true, type: () => [String] }, status: { required: true, default: Status.ENABLED, enum: Status }, status2: { required: false, enum: Status }, statusArr: { required: false, enum: Status, isArray: true }, oneValueEnum: { required: false, enum: OneValueEnum }, oneValueEnumArr: { required: false, enum: OneValueEnum }, breed: { required: false, type: () => String }, nodes: { required: true, type: () => [Object] }, optionalBoolean: { required: false, type: () => Boolean }, date: { required: true, type: () => Date } };
}
}
__decorate([
Expand Down

0 comments on commit b1059a7

Please sign in to comment.