-
Notifications
You must be signed in to change notification settings - Fork 989
Fixed DynamoDbEnhancedClient TableSchema::itemToMap to handle null fl… #6137
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
Changes from all commits
f95780f
7df0c6a
aeb683c
366ccde
0499dd0
3bfc458
9bfc991
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
|
|
@@ -127,7 +127,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(); | ||
| } | ||
|
|
||
|
|
@@ -612,15 +612,24 @@ 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); | ||
| } | ||
| }); | ||
|
|
||
| Set<FlattenedMapper<T, B, ?>> processedMappers = new LinkedHashSet<>(); | ||
| flattenedObjectMappers.forEach((name, flattenedMapper) -> { | ||
| attributeValueMap.putAll(flattenedMapper.itemToMap(item, ignoreNulls)); | ||
| if (item != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also when item != null, the same flattenedMapper.itemToMap() is called multiple times for the same flattened object. Is my understanding correct? Can we add this so it loops just once for every flattenMapper?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I updated the PR with this improvement. |
||
| if (processedMappers.add(flattenedMapper)) { | ||
| attributeValueMap.putAll(flattenedMapper.itemToMap(item, ignoreNulls)); | ||
| } | ||
| } else if (!ignoreNulls) { | ||
| attributeValueMap.put(name, AttributeValue.fromNul(true)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the right behavior ? Here name is an attribute name from the flattened object, not the flattened field name itself.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes the behavior is correct. The whole idea with the flattened is to bring the attributes from all the flattened members to root in the returned map, as like they belong to the parent root level. The flattened fields themself containing those attributes will actually not be visible in the DynamoDb table. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a flattened object is null, this puts NULL for each of its attributes individually. But what if the flattened object has nested flattened objects? Does this correctly handle multi-level nesting, or will it only create nulls for the immediate level?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does handle multi-level flattening correctly. FlattenedObjectMappers is keyed by final flattened attribute names, and itemToMap is invoked recursively for each nested level via the corresponding FlattenedMapper. As a result, when item == null and ignoreNulls == false, NULL values are created for all flattened attributes across all levels, as validated by the unit tests added in this PR. |
||
| } | ||
| }); | ||
|
|
||
| if (flattenedMapMapper != null) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we checking if
item(the top-level object being mapped) is null here?In the context of
itemToMap(T item, boolean ignoreNulls), shouldn'titemnever be null? This seems different from the flattened object case where checking for null makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Item can be null in this context because itemToMap is invoked recursively when processing FlattenedMappers. If a nested flattened object is null but itself contains another @DynamoDbFlatten member, we still need to traverse it to emit NULL values for its flattened attributes when ignoreNulls == false. This mirrors the flattened-object behavior and ensures consistent null propagation across all nesting levels.