class-transformer: fix: ExposeAll does not work with excludeExtraneousValues
Description
Strategy ‘exposeAll’ does not work as expected with true excludeExtraneousValues flag. Actual behaviour is as all properties were not exposed.
Minimal code-snippet showcasing the problem
class Dog {
name: string;
}
const plainDoggie = {
name: 'Azor',
surname: 'dogs does not have surnames :('
};
const doggie = plainToClass(Dog, plainDoggie, {
strategy: 'exposeAll',
excludeExtraneousValues: true
});
console.log('doggie', doggie);
// should be {name:'Azor'}, but is {} instead
Expected behavior
All properties are exposed. In above example doggie = {name: ‘Azor’}
Actual behavior
Properties are not exposed. In above example doggie = {}
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 63
- Comments: 33
strategy: 'exposeAll'is a strategy: it should mean that by default, everything is exposed.excludeExtraneousValues: truemeans if something outside the class is in the object, then remove it. The combination of these two options should logically be that you don’t need to decorate anything in the class with@Exposebecause everything will be exposed, but extraneous values should be removed.Thus, this is a bug.
Yes! 👍
Bump!
you can use
@Exposefor each property that you wanna expose itI ended up creating a class decorator that applies expose decorator to its properties instead of manually adding expose decorator to every property.
Here is what i do:
It has been 2 years and this has yet to be resolved… Why?
I faced the same issue so I created an extension method to achieve the wanted behavior:
const myInstance = transformToDto(myDTO, myData);This still doesn’t work with
excludeExtraneousValues: true. You must decorate each property either with@Expose()or@Exclude()to get any properties.Any news on this issue? This simple change could be very beneficial for a lot of users.
It’s still happening now, combining
exposeAllandexcludeExtraneousValuesstill doesn’t expose any field. I think this is a 4 year-old bug.The whitelist option in the class-validator package gives the expected behavior for request parsing.
Namely, it drops properties that weren’t defined in the DTO.
It does this with no need for additional property- or class-specific decorators.
Any news on this issue?
Sorry for disturbing. @NoNameProvided Could you plz have a look at this issue?
Option
strategy: 'excludeAll'behave allmost the same asexcludeExtraneousValues: true, is that by design?keysare the same exceptisMapandignoreDecorators