Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

core: CfnResource.isCfnResource is too permissive #30473

Open
michanto opened this issue Jun 6, 2024 · 5 comments
Open

core: CfnResource.isCfnResource is too permissive #30473

michanto opened this issue Jun 6, 2024 · 5 comments
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@michanto
Copy link

michanto commented Jun 6, 2024

Describe the bug

I created a construct class with a cfnResourceType property. I use that type to search for a CfnResource under the parent of that construct. But my non-CfnResource class is recognized as a CfnResource by the CDK because it has a cfnResourceType property! Wrong!

Expected Behavior

public static isCfnResource(x: any): x is CfnResource {
    return CfnElement.isCfnElement(x) && x.cfnResourceType !== undefined;
  }

That would be a lot better because isCfnElement has an RTTI symbol property.

Current Behavior

public static isCfnResource(x: any): x is CfnResource {
    return x !== null && typeof(x) === 'object' && x.cfnResourceType !== undefined;
  }

Reproduction Steps

class BucketHelperClass extends Construct {
  readonly cfnResourceType = CfnBucket.CFN_RESOURCE_TYPE_NAME
}
let helper = new BucketHelperClass(new App(), "Helper")
if (CfnResource.isCfnResource(helper)) {
  throw new Error('No it is not a CfnResource!');
}

Possible Solution

Either this:

public static isCfnResource(x: any): x is CfnResource {
    return CfnElement.isCfnElement(x) && x.cfnResourceType !== undefined;
  }

Or maybe:

const CFN_RESOURCE_SYMBOL = Symbol.for('@aws-cdk/core.CfnResource');

Additional Information/Context

When I try to synthesize a stack with a fake CfnResource, I get the following error:

[CDK] TypeError: source.addDependency is not a function

CDK CLI Version

2.120.0 (build 58b90c4)

Framework Version

No response

Node.js Version

10.2.5

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

@michanto michanto added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 6, 2024
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Jun 6, 2024
@pahud
Copy link
Contributor

pahud commented Jun 7, 2024

Agree. But looking at this discussion

#28001 (review)

What do you think about it? @tmokmss @go-to-k ?

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jun 7, 2024
@michanto
Copy link
Author

michanto commented Jun 7, 2024

Both could be supported by just adding isCfnElement to the existing?

Or you could adopt my Construct RTTI class

@tmokmss
Copy link
Contributor

tmokmss commented Jun 7, 2024

I agree that the current state is undesirable and should be fixed. However, I can't think of a way to make sure it's not a breaking change. I think the proposed change will not cause any problem, but there are so many cdk users in the world...

@go-to-k
Copy link
Contributor

go-to-k commented Jun 7, 2024

I have the same opinion with @tmokmss . (But I think it's probably OK.)

@michanto
Copy link
Author

Even if you decide not to fix isCfnResource itself, you should fix the addDependency error on synthesis. That's definitely a bug.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

4 participants