Repository navigation
fix(intrinsics): accept a string index in Fn::Select - #9406
Open
Shaurya55555 wants to merge 1 commit into
Open
Shaurya55555 wants to merge 1 commit into
Shaurya55555 wants to merge 1 commit into
Conversation
CloudFormation accepts the index of Fn::Select as a string, and its documentation example is {"Fn::Select": ["1", ["apples", "grapes", "oranges", "mangoes"]]}. IntrinsicResolver rejected it, so with ignore_errors=True the property was left unresolved. The newer Fn::Select resolver in cfn_language_extensions already accepts it. The existing test that asserted a string index fails is changed to use a non-numeric string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue(s) does this change fix?
None filed. I found it while comparing
IntrinsicResolverwith the newerFn::Selectresolver incfn_language_extensions.Why is this change necessary?
CloudFormation accepts the index of
Fn::Selectas a string. The example in its documentation is{ "Fn::Select" : [ "1", [ "apples", "grapes", "oranges", "mangoes" ] ] }, which returns "grapes" (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html).IntrinsicResolver.handle_fn_selectonly accepts anint, so it raisesInvalidIntrinsicExceptionfor"1".SamBaseProvider.get_templatecalls the resolver withignore_errors=True, so the property is silently left unresolved, for example aCodeUriof{"Fn::Select": ["0", {"Fn::Split": [",", {"Ref": "Dirs"}]}]}stays a dict.FnSelectResolverincfn_language_extensionsalready accepts a string index, so the two resolvers disagree on the same template.How does it address the issue?
An index that is a string is converted with
int()before the existing type check. A non-numeric string still raises the same error.Tests: the existing
test_fn_select_index_invalid_index_typelisted"3"as an invalid index, which encoded the same assumption, so I changed that value to"three". I addedtest_fn_select_string_index, using the documentation example.What side effects does this change have?
Templates that pass a quoted index now resolve instead of being left as they are. Integer indexes and all other types behave as before. As in
FnSelectResolver,int()also accepts surrounding whitespace and a sign.Mandatory Checklist
PRs will only be reviewed after checklist is complete
make prpasses (not run.black --check -l 120passes on both files, andruff checkshows only a long line in an unrelated existing test that is the same ondevelop)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::Select": ["1", ["apples", "grapes", "oranges", "mangoes"]]}raisesInvalidIntrinsicExceptionbefore and returnsgrapesafter."three",None,True,"7"and"-1"still raise.resolve_template(ignore_errors=True)on aCodeUriof{"Fn::Select": ["0", {"Fn::Split": [",", {"Ref": "Dirs"}]}]}leaves the dict unresolved before and givessrcafter (integer index 0 gavesrcbefore too).Fn::Selectunit tests pass after the change. Running the new test file against the old code,test_fn_select_string_indexfails withThe argument to Fn::Select must resolve to a int type.make pr.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.