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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/core/lib/tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 tags !== undefined && tags !== null && typeof tags === 'object' && (tags as any)[TAG_MANAGER_SYM];
}

/**
Expand Down
37 changes: 37 additions & 0 deletions packages/aws-cdk-lib/core/test/tag-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Construct } from 'constructs';
import * as cdk from '../../../aws-cdk-lib';
import { TagType } from '../lib/cfn-resource';
import { TagManager } from '../lib/tag-manager';

Expand Down Expand Up @@ -225,4 +227,39 @@ describe('tag manager', () => {
expect(true).toEqual(mgr.applyTagAspectHere(['AWS::Fake::Resource'], []));
expect(false).toEqual(mgr.applyTagAspectHere(['AWS::Wrong::Resource'], []));
});

test('isTaggable function works as expected', () => {
const app = new cdk.App();
const taggableConstruct = new TaggableConstruct(app, 'MyConstruct');
const nonTaggableConstruct = new NonTaggableConstruct(app, 'NonTaggableConstruct');

// Assert that isTaggable returns true for a taggable construct
expect(TagManager.isTaggable(taggableConstruct)).toEqual(true);

// Assert that isTaggable returns false (not undefined) for a non-taggable construct
expect(TagManager.isTaggable(nonTaggableConstruct)).not.toEqual(undefined);
expect(TagManager.isTaggable(nonTaggableConstruct)).toEqual(false);
});
});

// `Stack` extends ITaggable so this construct is Taggable by default
class TaggableConstruct extends cdk.Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
new cdk.CfnResource(this, 'Resource', {
type: 'Whatever::The::Type',
properties: {
Tags: this.tags.renderedTags,
},
});
}
}

// Simple Construct that does not extend ITaggable
class NonTaggableConstruct extends Construct {
public readonly id: string;
constructor(scope: Construct, id: string) {
super(scope, id);
this.id = id;
}
}