Skip to content

Commit

Permalink
fix(plugin): ignore non-primitive default values
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 27, 2023
1 parent 32a10d9 commit 7230948
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
24 changes: 22 additions & 2 deletions lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,28 @@ export function canReferenceNode(node: ts.Node, options: PluginOptions) {
if (!options.readonly) {
return true;
}
if (ts.isIdentifier(node) || ts.isCallExpression(node)) {
if (ts.isCallExpression(node) || ts.isIdentifier(node)) {
return false;
}
return true;
if (ts.isNewExpression(node)) {
if ((node.expression as ts.Identifier)?.escapedText === 'Date') {
return true;
}
return false;
}
if (
node.kind === ts.SyntaxKind.FalseKeyword ||
node.kind === ts.SyntaxKind.TrueKeyword ||
node.kind === ts.SyntaxKind.NullKeyword
) {
return true;
}
if (
ts.isNumericLiteral(node) ||
ts.isPrefixUnaryExpression(node) ||
ts.isStringLiteral(node)
) {
return true;
}
return false;
}
5 changes: 5 additions & 0 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ export class ModelClassVisitor extends AbstractFileVisitor {
this.clonePrimitiveLiteral(factory, initializer) ?? initializer;

if (!canReferenceNode(initializer, options)) {
const parentFilePath = node.getSourceFile().fileName;
const propertyName = node.name.getText();
pluginDebugLogger.debug(
`Skipping registering default value for "${propertyName}" property in "${parentFilePath}" file because it is not a referenceable value ("${initializer.getText()}").`
);
return undefined;
}
return factory.createPropertyAssignment(key, initializer);
Expand Down
9 changes: 8 additions & 1 deletion test/plugin/fixtures/project/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConsoleLogger } from '@nestjs/common';
import {
IsIn,
IsNegative,
Expand Down Expand Up @@ -44,13 +45,15 @@ export class CreateCatDto {
negative: number = -1;

@Length(2)
lengthMin: string;
lengthMin: string | null = null;

@Length(3, 5)
lengthMinMax: string;

date = new Date();

active: boolean = false;

@ApiProperty()
name: string = randomUUID();

Expand Down Expand Up @@ -121,6 +124,10 @@ export class CreateCatDto {
second: number;
};

// Both props should be ignored
nonExportedEnum: NonExportedEnum;
nonExportedClass: NonExportedClass;

// Default value should be ignored
logger = new ConsoleLogger();
}
12 changes: 10 additions & 2 deletions test/plugin/fixtures/serialized-meta.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,21 @@ export default async () => {
default: -1,
maximum: -1
},
lengthMin: { required: true, type: () => String, minLength: 2 },
lengthMin: {
required: true,
type: () => String,
nullable: true,
default: null,
minLength: 2
},
lengthMinMax: {
required: true,
type: () => String,
minLength: 3,
maxLength: 5
},
date: { required: true, type: () => Object, default: new Date() },
active: { required: true, type: () => Boolean, default: false },
name: { required: true, type: () => String },
age: {
required: true,
Expand Down Expand Up @@ -156,7 +163,8 @@ export default async () => {
first: { required: true, type: () => String },
second: { required: true, type: () => Number }
})
}
},
logger: { required: true, type: () => Object }
}
}
]
Expand Down

0 comments on commit 7230948

Please sign in to comment.