chore(core): introduce DetachedConstruct base class #36015
Merged
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.
Reason for this change
Refactor repeated pattern of anonymous classes that throw
UnscopedValidationErrorwhen accessingnodeorenvproperties.This pattern exists because certain legacy CDK APIs like
ManagedPolicy.fromAwsManagedPolicyName()and CloudFront'sfromManagedCachePolicy()return construct-like objects without requiring ascopeparameter. These APIs predate modern CDK patterns where all constructs require a scope. They cannot be changed without breaking existing customer code, so they return "detached" constructs that throw errors when used in APIs that require a proper construct tree context.But we also sometimes add new features to resource interfaces like
IManagedPolicy. When this happens, we have to update this fake implementation in a number of places. It also prevents us (and users) from providing augmentations for theIConstructinterface like proposed in #36013.With this change, we have reduced the number of places to update to one. It's now also possible to augment
IConstruct.Description of changes
DetachedConstructbase class incore/lib/private/detached-construct.tsthat encapsulates the pattern of throwingUnscopedValidationErrorwhen accessingnodeorenvCachePolicy,OriginRequestPolicy,ResponseHeadersPolicy) to extendDetachedConstructinstead of implementing the pattern inlineManagedPolicy.fromAwsManagedPolicyName()to useDetachedConstructElasticBeanstalkDeployActionto useDetachedConstructfor managed policy referenceDetachedConstructto verify error throwing behaviorThis change reduces code duplication and makes the pattern more maintainable. The
DetachedConstructclass is marked as internal and documented to discourage use in new APIs.Describe any new or updated permissions being added
No new or updated permissions.
Description of how you validated changes
core/test/private/detached-construct.test.tsthat verifyUnscopedValidationErroris thrown when accessingnodeandenvpropertiesChecklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license