Skip to content

Commit

Permalink
Merge pull request aws#3190 from aws/zoewang/fixNPEInBatchWriteResult
Browse files Browse the repository at this point in the history
Fix NPE in BatchWriteResult
  • Loading branch information
zoewangg authored Aug 21, 2024
2 parents 1a2529a + 8a0e5ce commit ead7af3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.createKeyFromMap;
import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -56,9 +58,14 @@ public final class BatchWriteResult {
private final Map<String, List<ItemCollectionMetrics>> itemCollectionMetrics;

private BatchWriteResult(Builder builder) {
this.unprocessedRequests = Collections.unmodifiableMap(builder.unprocessedRequests);
this.consumedCapacity = Collections.unmodifiableList(builder.consumedCapacity);
this.itemCollectionMetrics = builder.itemCollectionMetrics;
this.unprocessedRequests = builder.unprocessedRequests == null ?
Collections.unmodifiableMap(new HashMap<>()) :
Collections.unmodifiableMap(builder.unprocessedRequests);
this.consumedCapacity = builder.consumedCapacity == null ? Collections.unmodifiableList(new ArrayList<>()) :
Collections.unmodifiableList(builder.consumedCapacity);
this.itemCollectionMetrics =
builder.itemCollectionMetrics == null ? Collections.unmodifiableMap(new HashMap<>()) :
Collections.unmodifiableMap(builder.itemCollectionMetrics);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public void setupMappedTables() {
@Test
public void builder_minimal() {
BatchWriteResult builtObject =
BatchWriteResult.builder().unprocessedRequests(Collections.EMPTY_MAP).consumedCapacity(ImmutableList.of()).build();
BatchWriteResult.builder().unprocessedRequests(Collections.EMPTY_MAP).build();

Assertions.assertThat(builtObject.itemCollectionMetrics()).isNull();
Assertions.assertThat(builtObject.itemCollectionMetrics()).isEmpty();
Assertions.assertThat(builtObject.consumedCapacity()).isEmpty();
}

Expand Down

0 comments on commit ead7af3

Please sign in to comment.