Skip to content

Commit 3c4b2b8

Browse files
authored
Strip quotes in getETag response during transform (#6771)
1 parent 1ac242b commit 3c4b2b8

5 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2 Migration Tool",
4+
"contributor": "",
5+
"description": "Strip quotes in getETag response"
6+
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/S3Streaming.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void getObject(String bucket, String key, File file) throws Exception {
4747

4848
GetObjectResponse objectMetadata = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key)
4949
.build(), file.toPath());
50+
51+
String etag = /*AWS SDK for Java v2 migration: NOTE: V2's eTag() preserves surrounding quotes in the response, whereas V1's getETag() strips them - https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/migration-s3-client.html#V1s-ObjectMetadata-using-V1s-getETag*/
52+
objectMetadata.eTag().replaceAll("^\"|\"$", "");
5053
}
5154

5255
void putObject_bucketKeyContent(String bucket, String key, String content) {

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/S3Streaming.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void getObject(String bucket, String key, File file) throws Exception {
4343
String cacheControl = s3Object.getObjectMetadata().getCacheControl();
4444

4545
ObjectMetadata objectMetadata = s3.getObject(new GetObjectRequest(bucket, key), file);
46+
47+
String etag = objectMetadata.getETag();
4648
}
4749

4850
void putObject_bucketKeyContent(String bucket, String key, String content) {

v2-migration/src/main/java/software/amazon/awssdk/v2migration/V1GetterToV2.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package software.amazon.awssdk.v2migration;
1717

1818
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.changeBucketNameToBucket;
19+
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isS3ETagGetter;
20+
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.transformETagGetter;
1921
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isV2ModelClass;
2022

2123
import org.openrewrite.ExecutionContext;
@@ -63,6 +65,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6365
return method;
6466
}
6567

68+
if (isS3ETagGetter(methodName, fullyQualified)) {
69+
return transformETagGetter(getCursor(), method);
70+
}
71+
6672
methodName = changeBucketNameToBucket(methodName);
6773

6874
if (NamingUtils.isGetter(methodName)) {

v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import java.util.Locale;
2323
import java.util.Map;
2424
import java.util.Set;
25+
import org.openrewrite.Cursor;
26+
import org.openrewrite.java.JavaTemplate;
2527
import org.openrewrite.java.MethodMatcher;
2628
import org.openrewrite.java.tree.Comment;
2729
import org.openrewrite.java.tree.Expression;
2830
import org.openrewrite.java.tree.J;
31+
import org.openrewrite.java.tree.JavaType;
2932
import org.openrewrite.java.tree.TextComment;
3033
import org.openrewrite.java.tree.TypeUtils;
3134
import org.openrewrite.marker.Markers;
@@ -296,6 +299,22 @@ public static boolean isUnsupportedHttpMethod(String httpMethod) {
296299
return Arrays.asList("Head", "Post", "Patch").contains(httpMethod);
297300
}
298301

302+
public static boolean isS3ETagGetter(String methodName, JavaType.FullyQualified declaringType) {
303+
return "getETag".equals(methodName)
304+
&& declaringType.getFullyQualifiedName().startsWith(V2_S3_MODEL_PKG);
305+
}
306+
307+
public static J.MethodInvocation transformETagGetter(Cursor cursor, J.MethodInvocation method) {
308+
String comment = "NOTE: V2's eTag() preserves surrounding quotes in the response, whereas V1's getETag() strips them - "
309+
+ "https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/migration-s3-client.html"
310+
+ "#V1s-ObjectMetadata-using-V1s-getETag";
311+
312+
String template = "#{any()}.eTag().replaceAll(\"^\\\"|\\\"$\", \"\")";
313+
return JavaTemplate.builder(template).build()
314+
.apply(cursor, method.getCoordinates().replace(), method.getSelect())
315+
.withComments(createCommentsWithNewline(comment));
316+
}
317+
299318
public static List<Comment> inputStreamBufferingWarningComment() {
300319
String warning = "When using InputStream to upload with S3Client, Content-Length should be specified and used "
301320
+ "with RequestBody.fromInputStream(). Otherwise, the entire stream will be buffered in memory. If"

0 commit comments

Comments
 (0)