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 Yargs parsing strings as numbers #2237

Merged
merged 7 commits into from
Aug 23, 2024
Merged

Conversation

awharn
Copy link
Member

@awharn awharn commented Aug 20, 2024

What It Does

Prevents parsing string arguments as numbers if the value provided only consists of numerics.
Resolves #1881

How to Test

Run the zowe files download ds "FAKE.DS" --rto 50 --encoding 1047 --show-inputs-only command and observe the response timeout is blue (number) and encoding is white (string).

Review Checklist
I certify that I have:

Additional Comments

Signed-off-by: Andrew W. Harn <[email protected]>
@awharn
Copy link
Member Author

awharn commented Aug 20, 2024

The reason we took this approach was because of a regression observed if we passed numbers to Yargs as number typed options. If a user supplied an option that took a number and provided no value i.e zowe files list ds "FAKE.DS" --rto --encoding 939, the handler validation would completely ignore the specified numeric option with no value.

That is because yargs returns undefined for number options specified with no value, but returns "" for string options specified with no value. Therefore, our validation can't detect the missing value when it is a number, since it is the same as if it wasn't specified at all.

This behavior would be a regression with no easy fix. In this implementation, we continue to treat numerics as a string until validation is done to confirm the option was not specified with an empty value (""), and the value provided is numeric. Once that validation is completed, the value of the argument is converted from string to number.

Because we disabled the default yargs behavior to convert strings to numbers, we explicitly do the conversion when the option type is a number. Otherwise, it remains a string.

Copy link

codecov bot commented Aug 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.22%. Comparing base (237513b) to head (1de341e).
Report is 9 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2237      +/-   ##
==========================================
+ Coverage   91.20%   91.22%   +0.01%     
==========================================
  Files         632      632              
  Lines       18134    18142       +8     
  Branches     3848     3887      +39     
==========================================
+ Hits        16540    16550      +10     
+ Misses       1593     1591       -2     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@adam-wolfe
Copy link
Contributor

Looks good, but is it safe for us to assume that all numeric values will be integers?

@t1m0thyj t1m0thyj linked an issue Aug 20, 2024 that may be closed by this pull request
Copy link
Member

@t1m0thyj t1m0thyj left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @awharn!

packages/imperative/src/cmd/src/yargs/YargsConfigurer.ts Outdated Show resolved Hide resolved
Copy link
Member

@traeok traeok left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @awharn for the fix!

Copy link
Contributor

@adam-wolfe adam-wolfe left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @awharn

@t1m0thyj t1m0thyj marked this pull request as draft August 21, 2024 13:51
@awharn awharn marked this pull request as ready for review August 21, 2024 17:03
@awharn
Copy link
Member Author

awharn commented Aug 21, 2024

Found a small bug - the change was not converting the values assigned to the alternate option names (aliases and camel case) to numbers, which caused system tests to fail for creating data-sets. Regression tests added, and changed the implementation to use the CliUtils.setOptionValue function to set all options, camel and kebab case options, aliases, and camel and kebab case aliases to the same (number) value.

Re-requested reviews from all reviewers.

Copy link
Member

@t1m0thyj t1m0thyj left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @awharn for updating this to handle aliases too! 😋

Copy link

sonarcloud bot commented Aug 21, 2024

Copy link
Member

@zFernand0 zFernand0 left a comment

Choose a reason for hiding this comment

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

Changes LGTM! 😋

@zFernand0 zFernand0 requested a review from janan07 August 21, 2024 20:51
@t1m0thyj t1m0thyj merged commit 89b4e46 into next Aug 23, 2024
19 checks passed
@t1m0thyj t1m0thyj deleted the yargs-fixes-strings-and-numbers branch August 23, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

Fix yargs parsing string arguments as numbers
7 participants