DynamoDbEnhancedClient: BeanTableSchema.mapToItem returns null when given an empty map or an all-null map, should return an uninitialized bean
See original GitHub issueExpected Behavior
A map with all null attributes, or with no non-null attributes, should return an uninitialized bean. Instead it returns null. Especially when beans contain other beans, this differences is significant and can be hard to work around.
Current Behavior
This application shows the problem. It takes an all-null bean, encodes it, then decodes it; instead of getting an all-null bean back, it gets just null:
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import java.util.Map;
public class MainClass {
@DynamoDbBean
public static class BeanClass {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
public static void main(String[] args) {
TableSchema<BeanClass> schema = TableSchema.fromBean(BeanClass.class);
BeanClass b = new BeanClass();
System.out.println("All-null bean cannot be decoded, returns null instead: " +
schema.mapToItem(schema.itemToMap(b, true)));
System.out.println("Should print an empty bean, will print null: " +
schema.mapToItem(Map.of()));
}
}
Output:
All-null bean cannot be decoded, returns null instead: null
Should print an empty bean, will print null: null
Possible Solution
The bug is in StaticImmutableTableSchema.mapToItem. It lazily constructs the bean, which means that if no setters ever need to be called, the bean is never constructed and null is returned. If it is changed to construct the bean at the start of the call, then the problem is solved.
If this seems acceptable I would be happy to write a pull request.
Steps to Reproduce (for bugs)
Compile and run the example code above.
Context
This is blocking us from moving to SDK 2. SDK 1 does not have this bug, and we do store objects with uninitialized beans, and we need to preserve the objects as-is, not null out these beans.
Your Environment
- AWS Java SDK version used: 2.15.79
- JDK version used: 1.3
- Operating System and version: MacOS, and whatever AWS Lambdas runs
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)

Top Related StackOverflow Question
Thanks so much Zoe! We appreciate your work here, it will let us switch over to SDK v2 much more easily.
If I have questions, then is it OK to ask you directly, or on github, or elsewhere? E.g., I can’t figure out why DynamoDbTable.query() returns a PageIterable, but DynamoDbIndex.query() returns a SdkIterable<Page>. It would be much more straightforward if they returned the same thing. (It would be really nice if Table and Index were the same class, so we could use primary indexes and GSIs interchangeably in the code, but it doesn’t seem to be set up that way.)
On Thu, Apr 8, 2021 at 4:19 PM Zoe Wang @.***> wrote:
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.