Skip to content

Commit

Permalink
Fixed DynamoDbEnhancedClient TableSchema::itemToMap to handle flatten…
Browse files Browse the repository at this point in the history
…ed members when ignoreNulls is false
  • Loading branch information
iulianbudau committed Feb 25, 2025
1 parent 76d3d0b commit e74034a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "Amazon DynamoDB Enhanced Client",
"contributor": "",
"description": "Fixed DynamoDbEnhancedClient TableSchema::itemToMap to return a map that contains a consistent representation of null top-level (non-flattened) attributes and flattened attributes when their enclosing member is null and ignoreNulls is set to false."
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private B mapToItem(B thisBuilder,
private Map<String, AttributeValue> itemToMap(T item, boolean ignoreNulls) {
T1 otherItem = this.otherItemGetter.apply(item);

if (otherItem == null) {
if (otherItem == null && ignoreNulls) {
return Collections.emptyMap();
}

Expand Down Expand Up @@ -515,7 +515,9 @@ public Map<String, AttributeValue> itemToMap(T item, boolean ignoreNulls) {

attributeMappers.forEach(attributeMapper -> {
String attributeKey = attributeMapper.attributeName();
AttributeValue attributeValue = attributeMapper.attributeGetterMethod().apply(item);
AttributeValue attributeValue = item == null ?
AttributeValue.fromNul(true) :
attributeMapper.attributeGetterMethod().apply(item);

if (!ignoreNulls || !isNullAttributeValue(attributeValue)) {
attributeValueMap.put(attributeKey, attributeValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,11 @@ public void buildAbstractWithFlatten() {

assertThat(tableSchema.itemToMap(item, true),
is(singletonMap("documentString", AttributeValue.builder().s("test-string").build())));

Map<String, AttributeValue> attributeMapWithNulls = tableSchema.itemToMap(item, false);
assertThat(attributeMapWithNulls.size(), is(2));
assertThat(attributeMapWithNulls, hasEntry("documentString", AttributeValue.builder().s("test-string").build()));
assertThat(attributeMapWithNulls, hasEntry("documentInteger", AttributeValue.fromNul(true)));
}

@Test
Expand Down

0 comments on commit e74034a

Please sign in to comment.