Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/TransformOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ export class TransformOperationExecutor {
finalValue = this.transform(subSource, finalValue, type, arrayType, isSubValueMap, level + 1);
} else {
if (subValue === undefined) {
// Set default value if nothing provided
finalValue = newValue[newValueKey];
const defaultValue = newValue[newValueKey];
// Skip field if default value not set
if (defaultValue === undefined) continue;
// Set default value
finalValue = defaultValue;
} else {
finalValue = this.transform(subSource, subValue, type, arrayType, isSubValueMap, level + 1);
finalValue = this.applyCustomTransformations(finalValue, (targetType as Function), transformKey, value, this.transformationType);
Expand Down
4 changes: 1 addition & 3 deletions test/functional/basic-functionality.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe("basic functionality", () => {
const transformedUser = plainToClass(User, fromPlainUser);
transformedUser.should.be.instanceOf(User);
transformedUser.should.have.property("age");
transformedUser.should.have.property("id").that.is.undefined;
transformedUser.should.not.have.property("id");

const transformedUserWithoutExtra = plainToClass(User, fromPlainUser, { excludeExtraneousValues: true });
transformedUserWithoutExtra.should.be.instanceOf(User);
Expand Down Expand Up @@ -1858,9 +1858,7 @@ describe("basic functionality", () => {
transformedUser.should.be.instanceOf(User);

transformedUser.should.be.eql({
age: undefined,
ageWithDefault: 18,
firstName: undefined,
firstNameWithDefault: "default first name",
adminWithDefault: false,
lastNameWithDefault: "default last name",
Expand Down