Skip to content

DynamoDB Enhanced BooleanAttributeConverter cannot convert N value of 0 and 1 to Boolean #1912

Closed
@ryanthon

Description

@ryanthon

Describe the Feature

DynamoDBMapper of the v1 SDK used to store boolean as N values of 0 and 1 in the table.

However, the default BooleanAttributeConverter is unable to convert those values to Boolean, and throws an IllegalStateException when attempting to do so.

I had to write my own wrapper class to check for a N type manually:

public class BooleanAttributeConverterWrapper implements AttributeConverter<Boolean>, PrimitiveConverter<Boolean> {

    private final BooleanAttributeConverter awsConverter = BooleanAttributeConverter.create();

    @Override
    public AttributeValue transformFrom(Boolean aBoolean) {
        return awsConverter.transformFrom(aBoolean);
    }

    @Override
    public Boolean transformTo(AttributeValue attributeValue) {
        if (!StringUtils.isBlank(attributeValue.n())) {
            boolean boolValue = Integer.parseInt(attributeValue.n()) != 0;
            var corrected = AttributeValue.builder()
                    .bool(boolValue)
                    .build();
            return awsConverter.transformTo(corrected);
        } else {
            return awsConverter.transformTo(attributeValue);
        }
    }

    @Override
    public EnhancedType<Boolean> type() {
        return awsConverter.type();
    }

    @Override
    public AttributeValueType attributeValueType() {
        return awsConverter.attributeValueType();
    }

    @Override
    public EnhancedType<Boolean> primitiveType() {
        return awsConverter.primitiveType();
    }
}

Is your Feature Request related to a problem?

As a result of the DynamoDBMapper behavior, I currently have a bunch of rows that have these values in my table.

I feel like DynamoDB-Enhanced should support that type of conversion, especially since that's how it was with DynamoDBMapper, and I think a lot of people will have this issue when migrating.

Proposed Solution

Have BooleanAttributeConverter support converting N values of 0 and 1 to Java Boolean.

  • I may be able to implement this feature request

Your Environment

  • AWS Java SDK version used: 2.13.39
  • JDK version used: Corretto 11
  • Operating System and version: Mac OS X 10.15.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions