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(core): isTaggable function can return undefined instead of false #31600

Merged
merged 6 commits into from
Oct 2, 2024

Conversation

sumupitchayan
Copy link
Contributor

@sumupitchayan sumupitchayan commented Sep 30, 2024

Closes #26495

Reason for this change

The isTaggable function of the TagManager class is currently broken in Python, as it can return undefined instead of true or false.

Description of changes

In JS/TS, the logical AND operator (&&) returns the first falsy value it encounters, even if that value is undefined instead of false - so the current implementation of isTaggable allows for undefined to be returned if tags is undefined:

public static isTaggable(construct: any): construct is ITaggable {
  const tags = (construct as any).tags;
  return tags && typeof tags === 'object' && (tags as any)[TAG_MANAGER_SYM];
}

The fix is simply changing the return line to the following to handle cases where tags is null or undefined:

return tags !== undefined && tags !== null && typeof tags === 'object' && (tags as any)[TAG_MANAGER_SYM];

Description of how you validated changes

Added a unit test to assert that isTaggable returns false, not undefined for a non-taggable Construct (and still returns true for a taggable construct).

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team September 30, 2024 19:57
@github-actions github-actions bot added bug This issue is a bug. p1 labels Sep 30, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Sep 30, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@sumupitchayan sumupitchayan added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Sep 30, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review September 30, 2024 20:06

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
@sumupitchayan sumupitchayan changed the title fix(core): isTaggable function returns undefined instead of false fix(core): isTaggable function can return undefined instead of false Sep 30, 2024
@sumupitchayan sumupitchayan marked this pull request as ready for review September 30, 2024 20:35
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Sep 30, 2024
@@ -303,7 +303,7 @@ export class TagManager {
*/
public static isTaggable(construct: any): construct is ITaggable {
const tags = (construct as any).tags;
return tags && typeof tags === 'object' && (tags as any)[TAG_MANAGER_SYM];
return typeof tags === 'object' && (tags as any)[TAG_MANAGER_SYM];
Copy link
Contributor

Choose a reason for hiding this comment

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

But this now explodes if tags is null. It shouldn't be, but it could be.

Sillily, in JS it's:

typeof null == 'object'

So you can't really get away from the truthiness test. The only thing you have to do is make sure the truth-tested falsy value doesn't leak through.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know - just updated the return statement:

return tags !== undefined && tags !== null && ...

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 1, 2024
@sumupitchayan sumupitchayan requested a review from rix0rrr October 1, 2024 17:13
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: d1c727b
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented Oct 2, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit be70c82 into main Oct 2, 2024
13 checks passed
@mergify mergify bot deleted the sumughan/fix-is-taggable-function branch October 2, 2024 15:23
Copy link

github-actions bot commented Oct 2, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. p1 pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-cdk-lib.TagManager: (is_taggable function stopped working in while migrating from 2.70.0 to 2.88.0)
3 participants