-
Notifications
You must be signed in to change notification settings - Fork 910
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
Changes from all commits
51eb1e8
f0e5172
748fad0
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 |
---|---|---|
|
@@ -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); | ||
assertThat(attributeMapWithNulls.size(), is(2)); | ||
assertThat(attributeMapWithNulls, hasEntry("documentString", AttributeValue.builder().s("test-string").build())); | ||
assertThat(attributeMapWithNulls, hasEntry("documentInteger", AttributeValue.fromNul(true))); | ||
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. 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 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. New tests added. 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. Thanks 👍 |
||
} | ||
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. 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 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. @joviegas I checked the UpdateBehaviorTest class and it contains tests which covers updating DDB entry with nested and flatten members, with both 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. will have look at it today and update a sample test 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. Yeap I saw it now , then we are good. |
||
|
||
@Test | ||
public void buildAbstractExtends() { | ||
StaticTableSchema<FakeAbstractSuperclass> superclassTableSchema = | ||
|
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; | ||
} | ||
} |
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.
Can we add new test case for this with separate assertions ?
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.
New test added.