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: exposeDefaultValues does not work #1609

Open
js-d-coder opened this issue Aug 31, 2023 · 5 comments
Open

fix: exposeDefaultValues does not work #1609

js-d-coder opened this issue Aug 31, 2023 · 5 comments
Labels
type: fix Issues describing a broken feature.

Comments

@js-d-coder
Copy link

js-d-coder commented Aug 31, 2023

Description

Please consider this code:

import "reflect-metadata";
import { Expose, plainToInstance } from "class-transformer";
import { IsNumber } from "class-validator";

class IMDB {
    @IsNumber()
    @Expose()
    rating: number = 0;

    @IsNumber()
    @Expose()
    votes: number;

    @IsNumber()
    @Expose()
    id: number;
}

const plainObj = {
    votes: 1000,
    id: 1
};

const movie1 = plainToInstance(IMDB, plainObj, {
    excludeExtraneousValues: true,
    enableImplicitConversion: true,
    exposeUnsetFields: false,
    exposeDefaultValues: false,
});

console.log(movie1); // IMDB { rating: 0, votes: 1000, id: 1 }

As you can notice IMDB class has a property rating with default value. Sometimes I want the instance object to not have default value if the property is missing and sometimes I do.
To achieve this using exposeDefaultValues: false option if I don't want default value for the missing property.
exposeDefaultValues: false seems to not be working in above code snippet.

Expected behavior

Default value should not be set to rating property i.e. rating property should not have value of 0 and should not be present in movie1 object.

Actual behavior

'ratingproperty has default value set inmovie1` object.

@js-d-coder js-d-coder added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Aug 31, 2023
@pahuta
Copy link
Contributor

pahuta commented Oct 2, 2023

Hey @js-d-coder ,
I faced with similar issue during upgrading to Node 18 and using "target": "es2022" instead of es2021 in tsconfig. Switching back to es2021 helped me 🤷‍♂️

@clapacik-circle
Copy link

clapacik-circle commented Feb 7, 2024

I'm seeing the same issue where exposeDefaultValues is being ignored for plainToClass.

@diffy0712
Copy link

It seems that, when used together with exposeUnsetFields:false, exposeDefaultValues:false does not work as expected.
I think this should be checked.

@diffy0712
Copy link

diffy0712 commented May 17, 2024

The same error is making test cases fail on #1505 (comment). These should pass with the fix.

cc: @ahochsteger

@TonyDo99
Copy link

TonyDo99 commented Aug 25, 2024

Do we have any updates on this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: fix Issues describing a broken feature.
Development

No branches or pull requests

5 participants