Skip to content

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

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

Closed
1 task done
ryanthon opened this issue Jun 21, 2020 · 3 comments · Fixed by #1948
Closed
1 task done
Labels
dynamodb-enhanced feature-request A feature should be added or improved.

Comments

@ryanthon
Copy link

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
@ryanthon ryanthon added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jun 21, 2020
@debora-ito
Copy link
Member

Thank you for reporting this issue @ryanthon. I see how this is impacting the ones who want to migrate to DynamoDB Enhanced.

@debora-ito debora-ito added dynamodb-enhanced and removed needs-triage This issue or PR still needs to be triaged. labels Jun 27, 2020
millems added a commit that referenced this issue Jul 8, 2020
…n values.

The 1.11.x DynamoDB mapper persisted booleans as 0s and 1s (in the 'number' type). For backwards-compatibility, we should be able to read these values. The values will be converted to boolean types if they are written back to DynamoDB.

Fixes #1912
millems added a commit that referenced this issue Jul 8, 2020
…n values.

The 1.11.x DynamoDB mapper persisted booleans as 0s and 1s (in the 'number' type). For backwards-compatibility, we should be able to read these values. The values will be converted to boolean types if they are written back to DynamoDB.

Fixes #1912
@deathrowe
Copy link

Would it be possible to get this opened back up? I'm still seeing this issue in 2.16.78. The fix was to use the above wrapper that the OP had.

@debora-ito
Copy link
Member

@deathrowe please open a new issue, and provide all the information we ask in the new issue template.

aws-sdk-java-automation added a commit that referenced this issue Jan 25, 2022
…371f5dc3b

Pull request: release <- staging/9bf14c90-ed46-4a40-b554-94e371f5dc3b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamodb-enhanced feature-request A feature should be added or improved.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants