Skip to content

docs: replace object-literal IResourceWithPolicyV2 example with named class - #38461

Merged
mergify[bot] merged 4 commits into
aws:mainfrom
AmariahAK:docs/s3-kms-replace-object-literal
Sep 29, 2026
Merged

mergify[bot] merged 4 commits into
aws:mainfrom
AmariahAK:docs/s3-kms-replace-object-literal

Conversation

@AmariahAK

@AmariahAK AmariahAK commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #38452

Reason for this change

The IResourcePolicyFactory / IResourceWithPolicyV2 usage example in the S3
and KMS READMEs used a TypeScript object-literal-with-inline-method pattern
for the return value of forResource():

return {
  env: resource.env,
  addToResourcePolicy(statement) { ... }
};

While this satisfies the structural interface in TypeScript, jsii/Rosetta
cannot translate anonymous objects with methods to other languages. The
generated Ruby docs showed an inline def inside a hash, Python had raw
function bodies inside dicts, and Java produced Map.of() with a broken
method body. See the full breakdown in #38452.

Description of changes

Updated the nofixture code blocks in both READMEs to use a proper named
class MyResourceWithPolicy that explicitly implements IResourceWithPolicyV2:

  • packages/aws-cdk-lib/aws-s3/README.md — replaced object-literal with
    MyResourceWithPolicy class
  • packages/aws-cdk-lib/aws-kms/README.md — identical fix, adapted for
    AWS::KMS::Key

Both now follow the same class-based pattern as CfnBucketWithPolicy and
CfnKeyWithPolicy in their respective default-traits.ts source files.

Additional imports added: AddToResourcePolicyResult (from
aws-cdk-lib/aws-iam) and ResourceEnvironment (from
aws-cdk-lib/interfaces). Removed unused IConstruct import.

Note: this is NOT the full fix for #38452. This PR only addresses the
README examples. The Rosetta transpiler itself still has bugs in the C#,
Golang, and Java targets (e.g. resource = resource; self-assignment in C#,
incorrect pointer usage in Go, missing getEnv() in Java) that will need a
separate PR in the Rosetta codebase. See @omarqureshi's analysis in the issue
for details on those remaining transpiler issues.

Describe any new or updated permissions being added

None — documentation-only change.

Description of how you validated changes

  • Verified the new class structure matches the existing CfnBucketWithPolicy
    and CfnKeyWithPolicy implementations in default-traits.ts exactly
  • Confirmed all imported types (AddToResourcePolicyResult,
    ResourceEnvironment, IResourceWithPolicyV2, IResourcePolicyFactory,
    PolicyStatement, ResourceWithPolicies) are exported from their
    respective modules
  • Confirmed the issue author's Rosetta translation output shows clean,
    idiomatic Ruby, Python, and Java for the class-based pattern
  • Full Rosetta extraction (yarn rosetta:extract --strict) should be run
    on a machine with ≥16GB RAM to verify cross-language translation before
    merge

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 July 31, 2026 12:57
@github-actions github-actions Bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 labels Jul 31, 2026

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The pull request linter fails with the following errors:

❌ The title prefix of this pull request must be one of "feat|fix|build|chore|ci|docs|style|refactor|perf|test|revert"

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.

@omarqureshi

Copy link
Copy Markdown

@AmariahAK - for cleanliness, instead of a merge from main, could you rebase instead and push (force with lease) a single squashed commit?

…th named class

The object-literal pattern with inline methods in the forResource()
return value does not translate across jsii/rosetta to non-TypeScript
languages (Ruby, Python, Java, C#, Go).

Replace it with a proper MyResourceWithPolicy class that implements
IResourceWithPolicyV2, matching the existing CfnBucketWithPolicy and
CfnKeyWithPolicy patterns in default-traits.ts.

closes aws#38452

Co-authored-by: atlarix-agent <[email protected]>
@AmariahAK
AmariahAK force-pushed the docs/s3-kms-replace-object-literal branch from 79fe45c to ac26af6 Compare August 2, 2026 12:10
@AmariahAK

Copy link
Copy Markdown
Contributor Author

@omarqureshi should be good now

@AmariahAK

Copy link
Copy Markdown
Contributor Author

@omarqureshi not too sure, ive just done a merge with changes from main so that it appears something is being done
Hopefully this fixes it

pull Bot pushed a commit to sjoerd2025/jsii-rosetta that referenced this pull request Sep 9, 2026
Fixes aws#3772.

Renderer bugs exposed while investigating aws/aws-cdk#38452: rewriting
the `aws-s3`/`aws-kms` README example as a named class (the doc-side
fix, aws/aws-cdk#38461) showed the C# and Go visitors emit non-compiling
output for ordinary class shapes.

### C#: dropped `this.` produces a self-assignment

`propertyAccessExpression` suppressed `this.` unconditionally. Public
members are PascalCased and can never collide with a camelCase
parameter, but private fields keep their camelCase name — so a
constructor assigning a same-named field rendered:

```csharp
public MyResource(string resource)
{
    resource = resource;   // CS1717 — the field is never assigned
}
```

The qualifier is now kept exactly when the member's *rendered* name (via
the existing `privatePropertyNames` context — the same mechanism that
decides casing) matches a parameter of an enclosing function:

```csharp
this.resource = resource;
```

All other output is unchanged — PascalCased members still drop `this.`.

### Go: constructor naming mismatch

`newExpression` hardcoded the `New` prefix (the code carried `// Should
this be "new" if the class is unexported?`) while
`constructorDeclaration` renders `new` for unexported classes — so a
snippet-local class defined `func newMyResource` but was called as the
non-existent `NewMyResource`. The call site now resolves the class
identifier and matches the declaration. Classes that model library types
(jsii-resolvable, including `/// fake-from-jsii` test fixtures) keep the
exported call form, since only their call sites render — verified
against the 6 existing corpus snippets that rely on this.

### Go: assignment wrapped the wrong side of the pointer

Parameters and properties are both pointer-valued in Go, but they do not
*render* alike, and what the right-hand side has to be follows the
rendering rather than the declaration:

- a **property** renders as the pointer field itself — `this.env`, a
`*string`;
- a **parameter** renders dereferenced — `*name`, a `string` (the
identifier rule at the bottom of `go.ts`).

So a property target wants a pointer on the right (leave a pointer
alone, wrap a literal) and a parameter target wants a value (dereference
a pointer, leave a literal raw) — opposite rules. The visitor set
`isPtrAssignmentRValue` for both kinds and wrapped for neither, which
broke each of them in one direction:

| TypeScript | rendered | why it fails |
| --- | --- | --- |
| `this.env = 'production'` | `this.env = "production"` | `*string =
string` |
| `next = other` (both parameters) | `*next = other` | `string =
*string` |

Both flags now key off whether the *target* renders as a pointer, which
only a property does:

| TypeScript | rendered |
| --- | --- |
| `this.env = 'production'` | `this.env = jsii.String("production")` |
| `this.env = env` | `this.env = env` |
| `name = 'fallback'` | `*name = "fallback"` |
| `next = other` | `*next = *other` |

> **Review follow-up.** The first version of this change keyed off the
declaration kind, and so wrapped literals for parameter targets too —
producing `*name = jsii.String("fallback")`, the same mismatch in the
opposite direction. Thanks @dgandhi62 for catching it. Corrected in
58de1d9, which also picks up the parameter-to-parameter case (`*next =
other`) that the corrected rule covers and that was broken before this
PR as well.

### Testing

Developed test-first, with two translations-corpus snippets carrying
expectations for **all four languages** — Python, Java and C# pinned at
their existing output, so the diff itself shows each change is confined
to its own target:

- `classes/constructor_parameter_shadows_field` — the C# `this.` fix and
the Go constructor naming fix;
- `expressions/assignment_to_parameter` — a parameter assigned from
another parameter, from a string literal and from a number literal.

Each was run red before the corresponding change. Full suite passes: 25
suites, 549 tests, 6 pre-existing skips (corpus files that predate this
PR and lack an expectation for one language). `CSharpVisitor.VERSION`
and `GoVisitor.VERSION` are bumped to invalidate cached translations.

---

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5 <[email protected]>
@gjurova gjurova assigned kumvprat and alvazjor and unassigned kumvprat Sep 28, 2026
@alvazjor alvazjor removed the pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run label Sep 29, 2026
@alvazjor

Copy link
Copy Markdown
Contributor

@Mergifyio queue

@mergify

mergify Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

🛑 Queue command has been cancelled

@alvazjor
alvazjor dismissed aws-cdk-automation’s stale review September 29, 2026 15:00

Title fixed; validate-pr passes on the current head.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(This review is outdated)

@mergify

mergify Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

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

@alvazjor alvazjor changed the title docs(s3,kms): replace object-literal IResourceWithPolicyV2 example with named class docs: replace object-literal IResourceWithPolicyV2 example with named class Sep 29, 2026
@github-actions github-actions Bot added the effort/medium Medium work item – several days of effort label Sep 29, 2026
@aws-cdk-automation
aws-cdk-automation dismissed their stale review September 29, 2026 15:06

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

@mergify

mergify Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

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

@alvazjor

Copy link
Copy Markdown
Contributor

@Mergifyio queue

@mergify

mergify Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-29 15:06 UTC · Rule: default-squash · triggered by @alvazjor with the @mergifyio queue command
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-09-29 15:07 UTC · at efa49114fc734c8fdaa54c0812fc9a414909cf6b · squash

This pull request spent 13 seconds in the queue, including 1 second running CI.

Required conditions to merge

@mergify
mergify Bot merged commit efa4911 into aws:main Sep 29, 2026
42 checks passed
@github-actions

Copy link
Copy Markdown
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 Sep 29, 2026

This branch was previously deployed

1 inactive deployment
automation — a5eaa1a8 Deployed Sep 29, 2026 by alvazjor via validate-pr #369890
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/medium Medium work item – several days of effort p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

s3/kms: IAM IResourcePolicyFactory, function in object

6 participants