Skip to content

DynamoDB enhanced client: TableSchema<T>.attributeValue method throws NullPointerException when retrieving value of an attribute of a flattened model  #4218

Open
@fedorovg

Description

@fedorovg

Describe the bug

When calling TableSchema<T>.attributeValue on an attribute of a flattened model, method throws a NullPointerException if the value of the attribute is currently set to null. If attribute's value is not null or the whole flattened model is null, method behaves as expected.

Expected Behavior

Expected behavior for TableSchema<T>.attributeValue would be to return null or AttributeValue wrapped null.

Current Behavior

Currently TableSchema<T>.attributeValue throws a NullPointerException

Reproduction Steps

@DynamoDbBean
public class FlattenedModel {
    private String attribute;

    public FlattenedModel() {
    }

    public FlattenedModel(String attribute) {
        this.attribute = attribute;
    }

    public String getAttribute() {
        return attribute;
    }

    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
}

@DynamoDbBean
public class Model {
    private FlattenedModel flattenedModel;

    public Model() {
    }

    public Model(FlattenedModel flattenedModel) {
        this.flattenedModel = flattenedModel;
    }

    @DynamoDbFlatten
    public FlattenedModel getFlattenedModel() {
        return flattenedModel;
    }

    public void setFlattenedModel(FlattenedModel flattenedModel) {
        this.flattenedModel = flattenedModel;
    }
}

public class Main {
    public static void main(String[] args)  {
        TableSchema<Model> schema = TableSchema.fromClass(Model.class);
        var model = new Model();
        model.setFlattenedModel(new FlattenedModel(null));
        var value = schema.attributeValue(model, "attribute");
    }
}

Possible Solution

No response

Additional Information/Context

Exception in thread "main" java.lang.NullPointerException
	at software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.isNullAttributeValue(EnhancedClientUtils.java:187)
	at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema$FlattenedMapper.attributeValue(StaticImmutableTableSchema.java:144)
	at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema$FlattenedMapper.access$1300(StaticImmutableTableSchema.java:91)
	at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema.attributeValue(StaticImmutableTableSchema.java:559)
	at software.amazon.awssdk.enhanced.dynamodb.mapper.WrappedTableSchema.attributeValue(WrappedTableSchema.java:77)
	at software.amazon.awssdk.enhanced.dynamodb.mapper.WrappedTableSchema.attributeValue(WrappedTableSchema.java:77)
	at org.example.Main.main(Main.java:22)

AWS Java SDK version used

2.20.105

JDK version used

openjdk version "11.0.18" 2023-01-17 LTS OpenJDK Runtime Environment Corretto-11.0.18.10.1 (build 11.0.18+10-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.18.10.1 (build 11.0.18+10-LTS, mixed mode)

Operating System and version

Linux pc 6.3.8-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 14 Jun 2023 20:10:31 +0000 x86_64 GNU/Linux

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.dynamodb-enhancedp2This is a standard priority issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions