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(common): when transforming undefined numeric values #12893

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Hareloo
Copy link

@Hareloo Hareloo commented Dec 6, 2023

Transforming numeric values in validationpipe is incorrect when value is undefined

closes #12864

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

This issue occurs when using ValidationPipe with transform: true option set.
When this option is set and query/param arguments on an endpoint are used that are optional/have a default value, when no value/invalid value is passed to this query/URL param, it results in a NaN value, instead of the default value or having it be set to undefined in case it's an optional param.

Issue Number: #12864

What is the new behavior?

Values of the sort mentioned will now be parsed as undefined and converted to undefined.

Does this PR introduce a breaking change?

  • Yes
  • No

Transforming numeric values in validationpipe is incorrect when value is undefined

closes nestjs#12864
@coveralls
Copy link

Pull Request Test Coverage Report for Build 4421a15f-4fad-4fed-a0b6-753367b142e5

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 92.26%

Totals Coverage Status
Change from base Build 2893054b-b3fe-4fee-a01d-9c529a7f6e46: 0.002%
Covered Lines: 6699
Relevant Lines: 7261

💛 - Coveralls

@caiorosa-seenons
Copy link

caiorosa-seenons commented Dec 7, 2023

@Hareloo the same issue happens for other types such as Enums and Strings, on both @query and @param decorations.
I'll try to provide a subset of a project I'm working on (I'm currently migrating to swc, so that's how I caught it).

Small example:

 public async get(
    @Query('status', new ParseEnumPipe(MyStatusEnum, { optional: true })) status?: MyStatusEnum,
  ): Promise<ResponseModel> {
   ...
}

https://github.com/nestjs/nest/blob/master/packages/common/pipes/parse-enum.pipe.ts

Copy link

@benjGam benjGam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change.

Comment on lines 205 to 213
if (metatype === Number) {
if (isUndefined(value)) {
// This is a workaround to deal with optional numeric values since
// optional numerics shouldn't be parsed to a valid number when
// they were not defined
return undefined;
}
return +value;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (metatype === Number) {
if (isUndefined(value)) {
// This is a workaround to deal with optional numeric values since
// optional numerics shouldn't be parsed to a valid number when
// they were not defined
return undefined;
}
return +value;
}
// This is a workaround to deal with optional numeric values since
// optional numerics shouldn't be parsed to a valid number when
// they were not defined
if (metatype === Number && !isUndefined(value)) {
return +value;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original change by @Hareloo is actually a 1:1 match with the implementation checking for Boolean type a few lines above. IMO the code is fine as is as it mirrors that code and follows the same pattern (without saying whether that's a good or a bad pattern 🤷).

If we change it here then we should also change the Boolean implementation as well.

@stuarthimmer-loop
Copy link

Is this change still blocked?

@kamilmysliwiec
Copy link
Member

@stuarthimmer-loop it's a breaking change so we'll need to wait till the next major release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants