Skip to content

Commit

Permalink
fix: lookup up property descriptors on the prototype (#1258)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng authored May 22, 2024
1 parent c0ec22a commit a073b5e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/TransformOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export class TransformOperationExecutor {

// if newValue is a source object that has method that match newKeyName then skip it
if (newValue.constructor.prototype) {
const descriptor = Object.getOwnPropertyDescriptor(newValue.constructor.prototype, newValueKey);
const descriptor = this.getPropertyDescriptor(newValue.constructor.prototype, newValueKey);
if (
(this.transformationType === TransformationType.PLAIN_TO_CLASS ||
this.transformationType === TransformationType.CLASS_TO_CLASS) &&
Expand Down Expand Up @@ -544,4 +544,12 @@ export class TransformOperationExecutor {

return this.options.groups.some(optionGroup => groups.includes(optionGroup));
}

private getPropertyDescriptor(obj: any, key: PropertyKey): PropertyDescriptor | undefined {
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor) return descriptor;

const prototype = Object.getPrototypeOf(obj);
return prototype ? this.getPropertyDescriptor(prototype, key) : undefined;
}
}
49 changes: 49 additions & 0 deletions test/functional/basic-functionality.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,55 @@ describe('basic functionality', () => {
expect(transformedUser).toEqual(likeUser);
});

it('should expose inherited method and accessors that have @Expose()', () => {
class User {
firstName: string;
lastName: string;

@Expose()
get name() {
return this.firstName + ' ' + this.lastName;
}

@Expose()
getName() {
return this.firstName + ' ' + this.lastName;
}
}
class Programmer extends User {
language: string;
}

const programmer = new Programmer();
programmer.firstName = 'Umed';
programmer.lastName = 'Khudoiberdiev';
programmer.language = 'en';

const fromPlainProgrammer = {
firstName: 'Umed',
lastName: 'Khudoiberdiev',
language: 'en',
};

const plainProgrammer: any = instanceToPlain(programmer);
expect(plainProgrammer).not.toBeInstanceOf(Programmer);
expect(plainProgrammer).toEqual({
firstName: 'Umed',
lastName: 'Khudoiberdiev',
language: 'en',
name: 'Umed Khudoiberdiev',
getName: 'Umed Khudoiberdiev',
});

const transformedProgrammer = plainToInstance(Programmer, fromPlainProgrammer);
expect(transformedProgrammer).toBeInstanceOf(Programmer);
const likeProgrammer = new Programmer();
likeProgrammer.firstName = 'Umed';
likeProgrammer.lastName = 'Khudoiberdiev';
likeProgrammer.language = 'en';
expect(transformedProgrammer).toEqual(likeProgrammer);
});

it('should transform array', () => {
defaultMetadataStorage.clear();

Expand Down

0 comments on commit a073b5e

Please sign in to comment.