Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not apply custom name for instanceToPlain on toClassOnly and exposeAll #1701

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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',
});
});
});
Loading