From 8350d9ee8c1c07a800822bf358e839a43b6e714f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferencz=20D=C3=A1vid?= Date: Tue, 14 May 2024 22:30:52 +0200 Subject: [PATCH 1/2] fix: do not apply custom name for instanceToPlain on toClassOnly and exposeAll --- src/TransformOperationExecutor.ts | 5 +- test/functional/transformation-option.spec.ts | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/TransformOperationExecutor.ts b/src/TransformOperationExecutor.ts index 0533f03df..0857b17bd 100644 --- a/src/TransformOperationExecutor.ts +++ b/src/TransformOperationExecutor.ts @@ -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; } } diff --git a/test/functional/transformation-option.spec.ts b/test/functional/transformation-option.spec.ts index 9bae57cf8..bfbe3e52f 100644 --- a/test/functional/transformation-option.spec.ts +++ b/test/functional/transformation-option.spec.ts @@ -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', + }); + }); +}); From d6a8f7419d434f020a7cd494c4885c6d851db338 Mon Sep 17 00:00:00 2001 From: "ferencz.david@protonmail.com" Date: Tue, 14 May 2024 22:39:04 +0200 Subject: [PATCH 2/2] docs: prettier --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da6209f20..84b0f4698 100644 --- a/README.md +++ b/README.md @@ -785,11 +785,11 @@ The `@Transform` decorator is given more arguments to let you configure how you ## Other decorators[⬆](#table-of-contents) -| Signature | Example | Description | -| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------- | -| `@TransformClassToPlain` | `@TransformClassToPlain({ groups: ["user"] })` | Transform the method return with instanceToPlain and expose the properties on the class. | +| Signature | Example | Description | +| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------------- | +| `@TransformClassToPlain` | `@TransformClassToPlain({ groups: ["user"] })` | Transform the method return with instanceToPlain and expose the properties on the class. | | `@TransformClassToClass` | `@TransformClassToClass({ groups: ["user"] })` | Transform the method return with instanceToInstance and expose the properties on the class. | -| `@TransformPlainToClass` | `@TransformPlainToClass(User, { groups: ["user"] })` | Transform the method return with plainToInstance and expose the properties on the class. | +| `@TransformPlainToClass` | `@TransformPlainToClass(User, { groups: ["user"] })` | Transform the method return with plainToInstance and expose the properties on the class. | The above decorators accept one optional argument: ClassTransformOptions - The transform options like groups, version, name