Fixed DynamoDbEnhancedClient TableSchema::itemToMap to handle null fl…#6137
Conversation
…attened members when ignoreNulls is false
020dacd to
f95780f
Compare
…l-flattened-members
| if (item != null) { | ||
| attributeValueMap.putAll(flattenedMapper.itemToMap(item, ignoreNulls)); | ||
| } else if (!ignoreNulls) { | ||
| attributeValueMap.put(name, AttributeValue.fromNul(true)); |
There was a problem hiding this comment.
Is this the right behavior ? Here name is an attribute name from the flattened object, not the flattened field name itself.
There was a problem hiding this comment.
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.
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?
There was a problem hiding this comment.
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.
|
|
||
| flattenedObjectMappers.forEach((name, flattenedMapper) -> { | ||
| attributeValueMap.putAll(flattenedMapper.itemToMap(item, ignoreNulls)); | ||
| if (item != null) { |
There was a problem hiding this comment.
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?
if (processedMappers.add(flattenedMapper)) {
attributeValueMap.putAll(flattenedMapper.itemToMap(item, ignoreNulls));
}
There was a problem hiding this comment.
Good catch. I updated the PR with this improvement.
…l-flattened-members
| AttributeValue attributeValue = attributeMapper.attributeGetterMethod().apply(item); | ||
| AttributeValue attributeValue = item == null ? | ||
| AttributeValue.fromNul(true) : | ||
| attributeMapper.attributeGetterMethod().apply(item); |
There was a problem hiding this comment.
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't item never be null? This seems different from the flattened object case where checking for null makes sense.
There was a problem hiding this comment.
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.
|
Looks like some of the CI is failing, looking at logs it appears to be because of checkstyle: You should be able to check locally with: |
0065d5f to
0499dd0
Compare
alextwoods
left a comment
There was a problem hiding this comment.
Nice - looks good and agree this addresses the issue from #6037
…l-flattened-members
…l-flattened-members
|



Fixed [DynamoDbEnhancedClient]
TableSchema::itemToMapdoesn't respect ignoreNulls with null@DynamoDbFlattenedmembers #2540.Description
Current behavior:
TableSchema.fromBean(Bean.class).itemToMap(bean, false)returns a map without members of any @DynamoDbFlatten'd members if the flattened member is null even though ignoreNulls is set to false.Expected behavior:
TableSchema::itemToMapshould return a map that contains a consistent representation of null top-level (non-flattened) attributes and flattened attributes when their "parent" member is null and ignoreNulls is set to false.Motivation and Context
#2540
Modifications
Updated the
StaticImmutableTableSchema -> Map<String, AttributeValue> itemToMap(T item, boolean ignoreNulls)logic to properly account for the ignoreNulls check and generateAttributeValue.fromNul(true)values for attributes of@DynamoDbFlattenedmembers that are null.Testing
The changes have already been tested by running the existing tests and also added new unit tests for the fixed behavior.
Test Coverage Checklist
Screenshots (if appropriate)
Types of changes
Checklist
mvn installsucceedsscripts/new-changescript and following the instructions. Commit the new file created by the script in.changes/next-releasewith your changes.License