Skip to content

Conversation

@mrgrain
Copy link
Contributor

@mrgrain mrgrain commented Nov 11, 2025

Reason for this change

This PR implements the foundational infrastructure for the CDK Mixins framework, introducing a composable abstraction system for applying functionality to CDK constructs. It is based on the current state of the RFC.

While the RFC is not yet approved and finalized, this PR aims to implement it including all its flaws so we can move forward with other implementing depending on this. We will update the package as the RFC evolves.

Description of changes

Core Framework:

  • Implemented IMixin interface and Mixin base class for creating composable abstractions
  • Added Mixins.of() API for applying mixins to constructs with apply() and mustApply() methods
  • Created ConstructSelector for filtering constructs by type, ID pattern, or CloudFormation resource type
  • Added comprehensive error handling and validation support
  • Added .with() augmentation to constructs for fluent mixin application

Testing:

  • Comprehensive unit tests for core framework, selectors, and all built-in mixins
  • Integration tests demonstrating real-world usage patterns
  • Property manipulation utility tests including edge cases

Documentation:

  • Updated README with usage examples, API reference, and best practices
  • Added Rosetta fixture for documentation code examples

Description of how you validated changes

  • All new code is covered by unit tests
  • Integration tests validate end-to-end functionality
  • Rosetta fixture ensures documentation examples are valid

Checklist


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

@github-actions github-actions bot added the p2 label Nov 11, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team November 11, 2025 16:50
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 11, 2025
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 23aad97 to 83cc751 Compare November 11, 2025 18:21
@aws-cdk-automation aws-cdk-automation added the pr/needs-further-review PR requires additional review from our team specialists due to the scope or complexity of changes. label Nov 11, 2025
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 83cc751 to c7ec67c Compare November 11, 2025 19:10
@mrgrain mrgrain requested a review from a team as a code owner November 11, 2025 19:10
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from c7ec67c to bdd296e Compare November 11, 2025 19:24
@aws-cdk-automation aws-cdk-automation removed the pr/needs-further-review PR requires additional review from our team specialists due to the scope or complexity of changes. label Nov 11, 2025
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from bdd296e to 22cf314 Compare November 11, 2025 20:03
@mrgrain mrgrain added pr/do-not-merge This PR should not be merged at this time. pr/request-cli-integ-tests Request CLI integ tests to be run. You will need to review the code and approve the deployment. labels Nov 11, 2025
mergify bot pushed a commit that referenced this pull request Nov 12, 2025
### Reason for this change

Refactor repeated pattern of anonymous classes that throw `UnscopedValidationError` when accessing `node` or `env` properties.

This pattern exists because certain legacy CDK APIs like `ManagedPolicy.fromAwsManagedPolicyName()` and CloudFront's `fromManagedCachePolicy()` return construct-like objects without requiring a `scope` parameter. 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 the `IConstruct` interface 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

- Created `DetachedConstruct` base class in `core/lib/private/detached-construct.ts` that encapsulates the pattern of throwing `UnscopedValidationError` when accessing `node` or `env`
- Refactored CloudFront policy classes (`CachePolicy`, `OriginRequestPolicy`, `ResponseHeadersPolicy`) to extend `DetachedConstruct` instead of implementing the pattern inline
- Refactored `ManagedPolicy.fromAwsManagedPolicyName()` to use `DetachedConstruct`
- Refactored `ElasticBeanstalkDeployAction` to use `DetachedConstruct` for managed policy reference
- Added unit tests for `DetachedConstruct` to verify error throwing behavior

This change reduces code duplication and makes the pattern more maintainable. The `DetachedConstruct` class 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

- Added unit tests in `core/test/private/detached-construct.test.ts` that verify `UnscopedValidationError` is thrown when accessing `node` and `env` properties
- Existing tests continue to pass, ensuring no behavioral changes

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 3a93231 to 94e15f9 Compare November 12, 2025 09:12
@aws-cdk-automation
Copy link
Collaborator

➡️ PR build request submitted to test-main-pipeline ⬅️

A maintainer must now check the pipeline and add the pr-linter/cli-integ-tested label once the pipeline succeeds.

@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 94e15f9 to 56a98db Compare November 12, 2025 17:20
@mrgrain mrgrain added pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested and removed pr/do-not-merge This PR should not be merged at this time. pr/request-cli-integ-tests Request CLI integ tests to be run. You will need to review the code and approve the deployment. labels Nov 12, 2025
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 56a98db to 7861e58 Compare November 13, 2025 11:24
import type { IMixin } from './core';
import { Mixins, ConstructSelector } from './core';

declare module 'constructs' {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work for consumers of this library?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does work if they import the submodule as described in the readme.

/**
* Validates the construct before applying the mixin.
*/
validate?(construct: IConstruct): string[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this not failing the jsii build?

I also still think validate() doesn't need to be here.

Copy link
Contributor Author

@mrgrain mrgrain Nov 14, 2025

Choose a reason for hiding this comment

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

why should it be failing the build (it doesn't)?

Copy link
Contributor

Choose a reason for hiding this comment

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

That might be an oversight. There is approximately 0% chance this gets translated appropriately to Java.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like jsii is silently ignoring it 🙈

        {
          "abstract": true,
          "docs": {
            "stability": "experimental",
            "summary": "Validates the construct before applying the mixin."
          },
          "locationInModule": {
            "filename": "lib/core/mixins.ts",
            "line": 33
          },
          "name": "validate",
          "parameters": [
            {
              "name": "construct",
              "type": {
                "fqn": "constructs.IConstruct"
              }
            }
          ],
          "returns": {
            "type": {
              "collection": {
                "elementtype": {
                  "primitive": "string"
                },
                "kind": "array"
              }
            }
          }
        }

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 13, 2025
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 7861e58 to 1c327df Compare November 14, 2025 11:17
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 1c327df to 95339b8 Compare November 14, 2025 11:26
@mrgrain mrgrain force-pushed the mrgrain/chore/mixins-pt-1 branch from 95339b8 to a903824 Compare November 14, 2025 12:13
@mergify
Copy link
Contributor

mergify bot commented Nov 14, 2025

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).

@mrgrain mrgrain removed the pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested label Nov 14, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 14, 2025
@mergify
Copy link
Contributor

mergify bot commented Nov 14, 2025

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).

1 similar comment
@mergify
Copy link
Contributor

mergify bot commented Nov 14, 2025

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
Copy link
Contributor

mergify bot commented Nov 14, 2025

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 56f22aa into main Nov 14, 2025
18 of 19 checks passed
@mergify mergify bot deleted the mrgrain/chore/mixins-pt-1 branch November 14, 2025 14:01
@github-actions
Copy link
Contributor

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 Nov 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants