-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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(cfn-include): handle numbers expressed as strings in templates #9525
Conversation
Title does not follow the guidelines of Conventional Commits. Please adjust title before merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little to permissive right now.
Also, @rix0rrr already said he'd veto this change. Please add him as a reviewer as well.
…strings-as-numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said to Adam before, I am not okay with this, for the simple reason that we have type contracts to uphold. Both inside TypeScript as well as across our jsii language barrier.
What you are saying is that you are cool with the following:
interface CorsRule {
// ...
readonly maxAge: number;
}
const rule: CorsRule = readRuleFromTemplate("some-user-controlled-template.json");
console.log(rule.maxAge + 1); // <-- Type error: cannot add a string and a number
// What do you mean maxAge is a "string"? It says "number" in the type right there!
I appreciate you are trying to roundtrip the quotation here. I have 2 responses to that:
- Do your bookkeeping another way. Make a subclass that keeps track of whether properties have been modified, and just outputs the original unparsed string if not. Or (nasty nasty suggestion) tack a magic property onto that
number
you parsed to tell it to render out with quotes again. - Why do we even want so desperately to roundtrip those quotes? If it's only for the benefit of
cdk diff
, then shouldn't we be updatingcdk diff
to consider10
and"10"
the same value? That might even be useful in more cases than just the one you are trying to solve here.
Personally I'm leaning towards the second solution. Anything but destroying the value of our type system guarantees for --honestly-- little benefit.
@@ -218,8 +218,11 @@ export function validateString(x: any): ValidationResult { | |||
|
|||
export function validateNumber(x: any): ValidationResult { | |||
if (canInspect(x) && typeof x !== 'number') { | |||
return new ValidationResult(`${JSON.stringify(x)} should be a number`); | |||
if (typeof x === 'string' && isNaN(parseInt(x))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why must it be an int? Is "3.141592654"
not allowed? Why does "10"
get preferential treatment?
…strings-as-numbers
…hey parse too, are considered the same. (eg 10 and '10')
...-cdk/cloudformation-include/test/test-templates/invalid/invalid-string-passed-to-number.json
Outdated
Show resolved
Hide resolved
...-cdk/cloudformation-include/test/test-templates/invalid/invalid-string-passed-to-number.json
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/cloudformation-include/test/test-templates/parsing-as-numbers.json
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Calvin! I like this much better.
Got some niggles about some comments: I'd like to see some clarification on code that's deviates from what you'd expect, so in the future I don't have to go and do code archeology when I pass by there again.
In this PR:
deepEqual
: has a looser definition of "equality" now than you would expect from the name -- why is that?getNumber
: doesn't actually always return a number -- why is that?
These kinds of comments make it easier for me to decide whether this kind of unexpectedness was intentional or an oversight/bug.
Other than that, ready to ship it!
} | ||
} | ||
|
||
// otherwise, return the input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...and let the validator handle the fact that it's not the type we want.
That's a pretty important detail to understanding the behavior of the parser here. At first glance this code would be wrong, but it becomes correct in combination with some other code somewhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Closes #9524
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license