Skip to content

Commit 849ce2b

Browse files
authored
Prevent IndexOutOfBoundsException when PutObject tagging is an empty set. (#3158)
1 parent aa7f818 commit 849ce2b

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed
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",
4+
"contributor": "",
5+
"description": "Prevent IndexOutOfBoundsException when PutObject tagging is an empty set."
6+
}

services/s3/src/it/java/software/amazon/awssdk/services/s3/ObjectTaggingIntegrationTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,26 @@ public void testDeleteObjectTagging() {
238238
assertThat(getTaggingResult.size()).isEqualTo(0);
239239
}
240240

241+
@Test
242+
public void testEmptyTagging_shouldNotThrowException() {
243+
Tagging tags = Tagging.builder().build();
244+
245+
String key = makeNewKey();
246+
s3.putObject(PutObjectRequest.builder()
247+
.bucket(BUCKET)
248+
.key(key)
249+
.tagging(tags)
250+
.build(), RequestBody.empty());
251+
252+
List<Tag> getTaggingResult = s3.getObjectTagging(GetObjectTaggingRequest.builder()
253+
.bucket(BUCKET)
254+
.key(key)
255+
.build())
256+
.tagSet();
257+
258+
assertThat(getTaggingResult.size()).isEqualTo(0);
259+
}
260+
241261
private String makeNewKey() {
242262
return KEY_PREFIX + System.currentTimeMillis();
243263
}

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/TaggingAdapter.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,21 @@ private TaggingAdapter() {
3939
public String adapt(Tagging tagging) {
4040
StringBuilder tagBuilder = new StringBuilder();
4141

42-
Tagging taggingClone = tagging.toBuilder().build();
42+
if (tagging != null && !tagging.tagSet().isEmpty()) {
43+
Tagging taggingClone = tagging.toBuilder().build();
4344

44-
Tag firstTag = taggingClone.tagSet().get(0);
45-
tagBuilder.append(SdkHttpUtils.urlEncode(firstTag.key()));
46-
tagBuilder.append("=");
47-
tagBuilder.append(SdkHttpUtils.urlEncode(firstTag.value()));
48-
49-
for (int i = 1; i < taggingClone.tagSet().size(); i++) {
50-
Tag t = taggingClone.tagSet().get(i);
51-
tagBuilder.append("&");
52-
tagBuilder.append(SdkHttpUtils.urlEncode(t.key()));
45+
Tag firstTag = taggingClone.tagSet().get(0);
46+
tagBuilder.append(SdkHttpUtils.urlEncode(firstTag.key()));
5347
tagBuilder.append("=");
54-
tagBuilder.append(SdkHttpUtils.urlEncode(t.value()));
48+
tagBuilder.append(SdkHttpUtils.urlEncode(firstTag.value()));
49+
50+
for (int i = 1; i < taggingClone.tagSet().size(); i++) {
51+
Tag t = taggingClone.tagSet().get(i);
52+
tagBuilder.append("&");
53+
tagBuilder.append(SdkHttpUtils.urlEncode(t.key()));
54+
tagBuilder.append("=");
55+
tagBuilder.append(SdkHttpUtils.urlEncode(t.value()));
56+
}
5557
}
5658

5759
return tagBuilder.toString();

0 commit comments

Comments
 (0)