Skip to content

Commit

Permalink
fix: do not apply custom name for instanceToPlain on toClassOnly and …
Browse files Browse the repository at this point in the history
…exposeAll
  • Loading branch information
diffy0712 committed May 14, 2024
1 parent c0ec22a commit 8350d9e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/TransformOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ export class TransformOperationExecutor {
this.transformationType === TransformationType.CLASS_TO_CLASS
) {
const exposeMetadata = defaultMetadataStorage.findExposeMetadata(targetType as Function, key);
if (exposeMetadata && exposeMetadata.options && exposeMetadata.options.name) {
const shouldRunOnToPlain = !(
exposeMetadata?.options?.toPlainOnly === false || exposeMetadata?.options?.toClassOnly === true
);
if (exposeMetadata && exposeMetadata.options && exposeMetadata.options.name && shouldRunOnToPlain) {
newValueKey = exposeMetadata.options.name;
}
}
Expand Down
72 changes: 72 additions & 0 deletions test/functional/transformation-option.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,75 @@ describe('filtering by transformation option', () => {
});
});
});

describe('Filtering by transform options, name using ExposeAll strategy', () => {
it('@Expose with custom name and toClassOnly set to true then it should be exposed only during plainToInstance', () => {
defaultMetadataStorage.clear();

class User {
@Expose()
firstName: string;

@Expose()
lastName: string;

@Expose({ name: 'pass', toClassOnly: true })
password: string;
}

const plainUser = {
firstName: 'Umed',
lastName: 'Khudoiberdiev',
pass: 'imnosuperman',
};
const classedUser = plainToInstance(User, plainUser, { strategy: 'exposeAll' });
expect(classedUser).toBeInstanceOf(User);
expect(classedUser).toEqual({
firstName: 'Umed',
lastName: 'Khudoiberdiev',
password: 'imnosuperman',
});

const plainedUser = instanceToPlain(classedUser, { strategy: 'exposeAll' });
expect(plainedUser).toEqual({
firstName: 'Umed',
lastName: 'Khudoiberdiev',
password: 'imnosuperman',
});
});

it('@Expose with custom name and toPlainOnly set to true should be exposed only during instanceToPlain and classToPlainFromExist operations', () => {
defaultMetadataStorage.clear();

class User {
@Expose()
firstName: string;

@Expose()
lastName: string;

@Expose({ name: 'pass', toPlainOnly: true })
password: string;
}

const plainUser = {
firstName: 'Umed',
lastName: 'Khudoiberdiev',
password: 'imnosuperman',
};
const classedUser = plainToInstance(User, plainUser, { strategy: 'exposeAll' });
expect(classedUser).toBeInstanceOf(User);
expect(classedUser).toEqual({
firstName: 'Umed',
lastName: 'Khudoiberdiev',
password: 'imnosuperman',
});

const plainedUser = instanceToPlain(classedUser, { strategy: 'exposeAll' });
expect(plainedUser).toEqual({
firstName: 'Umed',
lastName: 'Khudoiberdiev',
pass: 'imnosuperman',
});
});
});

0 comments on commit 8350d9e

Please sign in to comment.