Repository navigation
fix(intrinsics): compare Fn::Equals values as strings - #9407
Conversation
vicheey
left a comment
There was a problem hiding this comment.
Thanks for picking this up! A couple of small things before merge:
- Could you add a few test cases:
("True", True, False),("Prod", "prod", False),(True, 1, False)and(1, 1.0, False)? Right now a case-insensitive compare would still pass every test, and the last two are behavior changes worth pinning (and calling out in the description). - Nit: type hints on
_equals_as_string, and a one-line comment pointing atConditionResolver._resolve_equalsso the two stay in sync.
Since sam local / sam build will resolve some templates differently after this, it's probably worth a line in the release notes.
|
Thanks for the review. Pushed 6c5c6b7:
All 302 tests in test_intrinsic_resolver.py pass. |
bnusunny
left a comment
There was a problem hiding this comment.
LGTM. I checked the new rule against a real CloudFormation stack, and SAM CLI now gives the same answer in every case I tried. develop got three of them wrong.
| Fn::Equals | CloudFormation | this PR | develop |
|---|---|---|---|
"true" vs yes |
equal | equal | not equal |
"true" vs on |
equal | equal | not equal |
"true" vs True |
equal | equal | not equal |
"True" vs True |
not equal | not equal | not equal |
"yes" vs yes |
not equal | not equal | not equal |
"1" vs 1.0 |
not equal | not equal | not equal |
"1.50" vs 1.50 |
not equal | not equal | not equal |
CloudFormation reads unquoted yes, on and True as the boolean true and compares it as "true", which is the rule this PR uses. It also normalises YAML numbers, so the 1 vs 1.0 change matches CloudFormation.
Optional follow-up, not blocking: str() now applies to lists too, so [a] equals the string "['a']". ConditionResolver rejects list and map arguments, and that check could be shared here.
On release notes: the repo doesn't keep a changelog file. Maintainers write the notes when they cut a release.
Which issue(s) does this change fix?
None filed. Related: #4308 uses the same condition (
!Equals [!Ref SomeParameter, true]), but its deploy error comes from the SAM translator, so this does not fix it.Why is this change necessary?
IntrinsicResolver.handle_fn_equalscompares the two resolved values with Python==. A parameter value is a string, while a YAML literal such astrueor1is a bool or an int, so!Equals [!Ref EnableX, true]is never true and anFn::Ifthat uses it takes the false branch. WithP="false"and!Equals [!Ref P, false]it also returns false, because"false" != False.ConditionResolver._resolve_equalsincfn_language_extensionsalready handles this ("CloudFormation Fn::Equals performs string comparison"), so the two resolvers disagree on the same template.SamBaseProvider.get_templateruns the old resolver last, sosam localandsam buildsee the wrong value for properties set throughFn::If.How does it address the issue?
Both values are converted to strings before comparing, with a bool lowercased to
true/false, the same rule_resolve_equalsuses.Tests: one parameterized test,
test_fn_equals_compares_as_strings, with 11 cases (string, bool and int literals against string parameter values, equal and not equal, including"True"vstrueand"Prod"vs"prod", which stay unequal because the comparison is case-sensitive).What side effects does this have?
Comparisons between values of different types that print the same now succeed, for example the string
"1"and the int1. Two behavior changes in the other direction, both pinned by tests:Truevs1was equal in Python and is now not equal ("true"vs"1"), and1vs1.0was equal and is now not equal ("1"vs"1.0"). Comparisons between two strings, and between two values of the same type, behave as before.sam localandsam buildcan therefore resolve some templates differently, which may be worth a release note.Mandatory Checklist
PRs will only be reviewed after checklist is complete
make prpasses (not run.black --check -l 120passes on both files)make update-reproducible-reqsif dependencies were changed (no dependencies changed)How I checked it
The resolver files are copies of
developbefore and after this change, with a stub for the one unrelated import (get_template_data, used only byFn::Transform).Fn::Ifon aFn::Equals [Ref P, literal]condition: withP="true"andtrue,P="false"andfalse,P="1"and1,P="1.5"and1.5, andP=1against"1", the old code returns the false branch and the new code returns the true branch.P="prod"against"dev"still returns the false branch.test_intrinsic_resolver.pypass after the change. Against the old code, 6 of the 11 new cases fail: the 4 string-comparison cases, and theTruevs1and1vs1.0cases above.make pr. I could not check the string-comparison rule against a real CloudFormation stack; I relied on_resolve_equals.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.