Skip to content

Fixed NullPointerException in S3 thrown when null metadata value is p… #5238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AmazonS3-cf52d1d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "Amazon S3",
"contributor": "",
"description": "Fixed NullPointerException in S3 thrown when null metadata value is provided."
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

package software.amazon.awssdk.services.s3.internal.handlers;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.interceptor.Context;
Expand Down Expand Up @@ -69,6 +69,6 @@ private CreateMultipartUploadRequest trimMetadataNames(CreateMultipartUploadRequ

private Map<String, String> trimKeys(Map<String, String> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> StringUtils.trim(e.getKey()), Map.Entry::getValue));
.collect(HashMap::new, (m, e) -> m.put(StringUtils.trim(e.getKey()), e.getValue()), HashMap::putAll);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -71,6 +72,26 @@ public void modifyRequest_putObject_metadataKeysAreTrimmed(TestCase tc) {
assertThat(modified.metadata().keySet()).containsExactlyElementsOf(tc.expectedKeys);
}

@Test
public void modifyRequest_putObjectMetadataValueNull_shouldNotThrowException() {
Map<String, String> metadata = new HashMap<>();
metadata.put("key", null);

Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class);

PutObjectRequest put = PutObjectRequest.builder()
.metadata(metadata)
.build();

when(ctx.request()).thenReturn(put);

ExecutionAttributes attrs = new ExecutionAttributes();
attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "PutObject");

PutObjectRequest modified = (PutObjectRequest) INTERCEPTOR.modifyRequest(ctx, attrs);
assertThat(modified.metadata().entrySet()).containsExactlyElementsOf(metadata.entrySet());
}

@ParameterizedTest
@MethodSource("testCases")
public void modifyRequest_creatMultipartUpload_metadataKeysAreTrimmed(TestCase tc) {
Expand Down
Loading