-
Notifications
You must be signed in to change notification settings - Fork 974
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
base: master
Are you sure you want to change the base?
Changes from all commits
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) { | ||
| 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. |
||
| } | ||
| }); | ||
|
|
||
| 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.
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?
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.
Good catch. I updated the PR with this improvement.