Skip to content

Fixed DynamoDbEnhancedClient TableSchema::itemToMap to handle flatten… #5832

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -45,6 +45,7 @@
import software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.AbstractBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.AbstractImmutable;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.AbstractNestedImmutable;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.AttributeConverterBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.AttributeConverterNoConstructorBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.CommonTypesBean;
Expand All @@ -55,6 +56,7 @@
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.ExtendedBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.FlattenedBeanBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.FlattenedImmutableBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.FlattenedNestedImmutableBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.FluentSetterBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.IgnoredAttributeBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.InvalidBean;
Expand Down Expand Up @@ -257,6 +259,67 @@ public void dynamoDbFlatten_correctlyFlattensImmutableAttributes() {
assertThat(itemMap, hasEntry("attribute2", stringValue("two")));
}

@Test
public void dynamoDbFlatten_correctlyFlattensNullImmutableAttributes() {
BeanTableSchema<FlattenedImmutableBean> beanTableSchema = BeanTableSchema.create(FlattenedImmutableBean.class);
AbstractImmutable abstractImmutable = AbstractImmutable.builder().build();
FlattenedImmutableBean flattenedImmutableBean = new FlattenedImmutableBean();
flattenedImmutableBean.setId("id-value");
flattenedImmutableBean.setAbstractImmutable(abstractImmutable);

Map<String, AttributeValue> itemMap = beanTableSchema.itemToMap(flattenedImmutableBean, false);
assertThat(itemMap.size(), is(3));
assertThat(itemMap, hasEntry("id", stringValue("id-value")));
assertThat(itemMap, hasEntry("attribute1", AttributeValue.fromNul(true)));
assertThat(itemMap, hasEntry("attribute2", AttributeValue.fromNul(true)));
}

@Test
public void dynamoDbFlatten_correctlyFlattensNestedImmutableAttributes() {
BeanTableSchema<FlattenedNestedImmutableBean> beanTableSchema =
BeanTableSchema.create(FlattenedNestedImmutableBean.class);
AbstractNestedImmutable abstractNestedImmutable2 =
AbstractNestedImmutable.builder().attribute2("nested-two").build();
AbstractNestedImmutable abstractNestedImmutable1 =
AbstractNestedImmutable.builder().attribute2("two").abstractNestedImmutableOne(abstractNestedImmutable2).build();
FlattenedNestedImmutableBean flattenedNestedImmutableBean = new FlattenedNestedImmutableBean();
flattenedNestedImmutableBean.setId("id-value");
flattenedNestedImmutableBean.setAttribute1("one");
flattenedNestedImmutableBean.setAbstractNestedImmutable(abstractNestedImmutable1);

Map<String, AttributeValue> nestedAttributesMap = new HashMap<>();
nestedAttributesMap.put("abstractNestedImmutableOne", AttributeValue.fromNul(true));
nestedAttributesMap.put("attribute2", stringValue("nested-two"));

AttributeValue expectedNestedAttribute =
AttributeValue.builder().m(Collections.unmodifiableMap(nestedAttributesMap)).build();

Map<String, AttributeValue> itemMap = beanTableSchema.itemToMap(flattenedNestedImmutableBean, false);
assertThat(itemMap.size(), is(4));
assertThat(itemMap, hasEntry("id", stringValue("id-value")));
assertThat(itemMap, hasEntry("attribute1", stringValue("one")));
assertThat(itemMap, hasEntry("attribute2", stringValue("two")));
assertThat(itemMap, hasEntry("abstractNestedImmutableOne", expectedNestedAttribute));
}

@Test
public void dynamoDbFlatten_correctlyFlattensNullNestedImmutableAttributes() {
BeanTableSchema<FlattenedNestedImmutableBean> beanTableSchema =
BeanTableSchema.create(FlattenedNestedImmutableBean.class);
AbstractNestedImmutable abstractNestedImmutable = AbstractNestedImmutable.builder().build();
FlattenedNestedImmutableBean flattenedNestedImmutableBean = new FlattenedNestedImmutableBean();
flattenedNestedImmutableBean.setId("id-value");
flattenedNestedImmutableBean.setAttribute1("one");
flattenedNestedImmutableBean.setAbstractNestedImmutable(abstractNestedImmutable);

Map<String, AttributeValue> itemMap = beanTableSchema.itemToMap(flattenedNestedImmutableBean, false);
assertThat(itemMap.size(), is(4));
assertThat(itemMap, hasEntry("id", stringValue("id-value")));
assertThat(itemMap, hasEntry("attribute1", stringValue("one")));
assertThat(itemMap, hasEntry("attribute2", AttributeValue.fromNul(true)));
assertThat(itemMap, hasEntry("abstractNestedImmutableOne", AttributeValue.fromNul(true)));
}

@Test
public void documentBean_correctlyMapsBeanAttributes() {
BeanTableSchema<DocumentBean> beanTableSchema = BeanTableSchema.create(DocumentBean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,24 @@ public void buildAbstractWithFlatten() {
is(singletonMap("documentString", AttributeValue.builder().s("test-string").build())));
}

@Test
public void buildAbstractWithFlattenAndIgnoreNullAsFalse() {
StaticTableSchema<FakeMappedItem> tableSchema =
StaticTableSchema.builder(FakeMappedItem.class)
.flatten(FAKE_DOCUMENT_TABLE_SCHEMA,
FakeMappedItem::getAFakeDocument,
FakeMappedItem::setAFakeDocument)
.build();

FakeDocument document = FakeDocument.of("test-string", null);
FakeMappedItem item = FakeMappedItem.builder().aFakeDocument(document).build();

Map<String, AttributeValue> attributeMapWithNulls = tableSchema.itemToMap(item, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add new test case for this with separate assertions ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test added.

assertThat(attributeMapWithNulls.size(), is(2));
assertThat(attributeMapWithNulls, hasEntry("documentString", AttributeValue.builder().s("test-string").build()));
assertThat(attributeMapWithNulls, hasEntry("documentInteger", AttributeValue.fromNul(true)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case to check what happens when there is Inner Nested classe and self refering nested class

@DynamoDbBean
public class FlattenedImmutableBean {
    private String id;
    private String attribute1;
    @DynamoDbFlatten
    private AbstractImmutable abstractImmutable;

}


@DynamoDbImmutable(builder = AbstractImmutable.Builder.class)
public class AbstractImmutable {
    private final String attribute2;
    private final AbstractImmutable abstractImmutableOne;
}

We have some existing tests classes like src/test/java/software/amazon/awssdk/enhanced/dynamodb/mapper/testbeans/AbstractImmutable.java
can we add more cases here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍
Will run the codebuilds , and then approve and merge once code build passes

}
Copy link
Contributor

@joviegas joviegas Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test case for UpdateItem where we update DDB entry similar as #5380 , to make sure it works for nested classes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joviegas I checked the UpdateBehaviorTest class and it contains tests which covers updating DDB entry with nested and flatten members, with both IgnoreNullsMode.SCALAR_ONLY and IgnoreNullsMode.DEFAULT modes. Do you have specific scenario in mind which requires more coverage?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will have look at it today and update a sample test

Copy link
Contributor

@joviegas joviegas Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap I saw it now , then we are good.


@Test
public void buildAbstractExtends() {
StaticTableSchema<FakeAbstractSuperclass> superclassTableSchema =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans;

import java.util.Objects;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbImmutable;

@DynamoDbImmutable(builder = AbstractNestedImmutable.Builder.class)
public class AbstractNestedImmutable {
private final String attribute2;
private final AbstractNestedImmutable abstractNestedImmutableOne;

private AbstractNestedImmutable(Builder b) {
this.attribute2 = b.attribute2;
this.abstractNestedImmutableOne = b.abstractNestedImmutableOne;
}

public String attribute2() {
return attribute2;
}

public AbstractNestedImmutable abstractNestedImmutableOne() {
return abstractNestedImmutableOne;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AbstractNestedImmutable that = (AbstractNestedImmutable) o;

if (!Objects.equals(attribute2, that.attribute2)) {
return false;
}
return Objects.equals(abstractNestedImmutableOne, that.abstractNestedImmutableOne);
}

@Override
public int hashCode() {
int result = attribute2 != null ? attribute2.hashCode() : 0;
result = 31 * result + (abstractNestedImmutableOne != null ? abstractNestedImmutableOne.hashCode() : 0);
return result;
}

public static Builder builder() {
return new Builder();
}

public static final class Builder {
private String attribute2;
private AbstractNestedImmutable abstractNestedImmutableOne;

public Builder attribute2(String attribute2) {
this.attribute2 = attribute2;
return this;
}

public Builder abstractNestedImmutableOne(AbstractNestedImmutable abstractNestedImmutableOne) {
this.abstractNestedImmutableOne = abstractNestedImmutableOne;
return this;
}

public AbstractNestedImmutable build() {
return new AbstractNestedImmutable(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans;

import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbFlatten;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;

@DynamoDbBean
public class FlattenedNestedImmutableBean {
private String id;
private String attribute1;
private AbstractNestedImmutable abstractNestedImmutable;

@DynamoDbPartitionKey
public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getAttribute1() {
return attribute1;
}

public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}

@DynamoDbFlatten
public AbstractNestedImmutable getAbstractNestedImmutable() {
return abstractNestedImmutable;
}

public void setAbstractNestedImmutable(AbstractNestedImmutable abstractNestedImmutable) {
this.abstractNestedImmutable = abstractNestedImmutable;
}
}
Loading