Skip to content

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

Open

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

Activity

  1. added
    bugThis issue is a bug.
    needs-triageThis issue or PR still needs to be triaged.
    on Jul 20, 2023
  2. changed the title [-]TableSchema<T>.attributeValue method throws NullPointerException when retrieving value of an attribute of a flattened model [/-] [+]DynamoDB enhanced client: TableSchema<T>.attributeValue method throws NullPointerException when retrieving value of an attribute of a flattened model [/+] on Jul 25, 2023
  3. self-assigned this
    on Aug 7, 2023
  4. debora-ito commented on Aug 9, 2023

    @debora-ito
    Member

    Noted.

    This is actually documented in EnhancedClientUtils#isNullAttributeValue javadoc here:

    A helper method to test if an AttributeValue is a 'null' constant. This will not test if the AttributeValue object is null itself, and in fact will throw a NullPointerException if you pass in null.

    But I understand getting a NPE is not a good experience.

  5. added
    p2This is a standard priority issue
    and removed
    needs-triageThis issue or PR still needs to be triaged.
    on Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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