typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.66k stars 487 forks source link

plainToClass - discriminator is deleted in plain/source object #239

Open hohler opened 5 years ago

hohler commented 5 years ago

When using plainToClass with a plain object in combination with @Type and a discriminator, it will delete the discriminator from that plain object. A workaround is either to clone the object, or to have keepDiscriminatorProperty set to true on the @Type definition. But then it will also be kept in the target object.

I think the root cause is in TransformOperationExecutor.ts on line 51: if (!targetType.options.keepDiscriminatorProperty) delete subValue[targetType.options.discriminator.property];

I can't make a fix / PR right now because of lack of time, but I like to have the issue documented here.

alexpls commented 5 years ago

@hohler I could replicate this by adding the following test: https://github.com/typestack/class-transformer/commit/b402d62d354f2decd8cdffbf415184cff37c19d5

I'd be happy to work on a PR to fix this once the project is under active maintenance again.