Skip to content

Add support for S3Object getObjectMetadata transform#6630

Merged
davidh44 merged 2 commits into
masterfrom
hdavidh/migration-tool-bugfix-s3object
Dec 17, 2025
Merged

Add support for S3Object getObjectMetadata transform#6630
davidh44 merged 2 commits into
masterfrom
hdavidh/migration-tool-bugfix-s3object

Conversation

@davidh44

@davidh44 davidh44 commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

Motivation and Context

#6469 && #6571

Fixes migration tool bug where error is thrown if code contains S3Object.getObjectMetadata() chained to another method, e.g. object.getObjectMetadata().getCacheControl()

[ERROR] The recipe produced an error. Please report this to the recipe author.
[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.19.0:dryRun (default-cli) on project s3proxy: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.19.0:dryRun failed: Error while visiting src/test/java/org/gaul/s3proxy/AwsSdkAnonymousTest.java: java.lang.ClassCastException: class org.openrewrite.java.tree.J$MethodInvocation cannot be cast to class org.openrewrite.java.tree.J$Identifier (org.openrewrite.java.tree.J$MethodInvocation and org.openrewrite.java.tree.J$Identifier are in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @15e08615)
[ERROR]   software.amazon.awssdk.v2migration.S3PutObjectRequestToV2$Visitor.saveMetadataValueAndRemoveStatement(S3PutObjectRequestToV2.java:417)

The root cause is S3TransformUtils.hasArguments() not working properly and always returning true. This occurs since OpenRewrite's J.MethodInvocation.getArguments() returns a List<Expression> with a J.Empty object when a method has no arguments. So method.getArguments().isEmpty() always returns false. Becuase of this, the recipe attempts to transform the getter (instead of just setters). When this getter is chained to another method, an error is thrown when the recipe attempts to cast J.MethodInvocation.getSelect() (another method) to J.Identifier - https://github.com/aws/aws-sdk-java-v2/blob/master/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3PutObjectRequestToV2.java#L417

The recipe also needs to remove the getObjectMetadata() method. In v1, getObject() returns S3Object. Whereas in v2, getObject() returns ResponseInputStream<GetObjectResponse>. There is no S3Object or ObjectMetadata POJO in v2, the metadata fields are set directly on GetObjectResponse / HeadObjectResponse.

The S3StreamingResponseToV2 recipe already adds the response() method - https://github.com/aws/aws-sdk-java-v2/blob/master/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingResponseToV2.java#L110

Modifications

  • Update S3TransformUtils.hasArguments() to properly check for args in method, by looking for any Expression NOT of type J.Empty
  • Add logic to S3PutObjectRequestToV2 recipe to remove the getObjectMetadata() method
  • Separate minor fix: add imports for Tagging and GetObjectTaggingResponse in S3PojoToV2 recipe, to account for when they are instantiated standalone, not assigned to a variable, e.g., passed as arg

Testing

  • Added getObjectMetadata() chained to another getter method, to the S3Streaming end-to-end test
  • Tested migration tool successfully on customer project S3Proxy , and Gradle sample code

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)

@davidh44 davidh44 requested a review from a team as a code owner December 16, 2025 19:44
@davidh44 davidh44 enabled auto-merge December 16, 2025 20:19
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@davidh44 davidh44 added this pull request to the merge queue Dec 17, 2025
Merged via the queue into master with commit 08f45df Dec 17, 2025
38 of 40 checks passed
@github-actions

Copy link
Copy Markdown

This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 17, 2025
@davidh44 davidh44 deleted the hdavidh/migration-tool-bugfix-s3object branch December 17, 2025 01:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants