aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

DynamoDBMapper can store List<CustomObject> but not Set<CustomObject>

Open ccoffey opened this issue 11 years ago • 21 comments

Why is it that DynamoDBMapper can marshal a List but not a Set when a Set could easily be stored in Dynamo without loss of precision using a List?

Please see the below example which works perfectly until you replace List with Set, however when you do this you get the following exception.

com.amazonaws.services.dynamodbv2.datamodeling.marshallers.ObjectSetToStringSetMarshaller: Marshaling a set of non-String objects to a DynamoDB StringSet. You won't be able to read these objects back out of DynamoDB unless you REALLY know what you're doing: it's probably a bug. If you DO know what you're doing feelfree to ignore this warning, but consider using a custom marshaler for this instead.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/application-context.xml")
public class ProductIntegrationTest {
    @Autowired
    AmazonDynamoDBClient dynamoDBClient;

    @Test
    public void shouldStoreNewProduct() {

        DynamoDBMapper productMapper = new DynamoDBMapper(dynamoDBClient);

        User user1 = new User();
        user1.setUserId(1);
        user1.setFirstName("user1");

        User user2 = new User();
        user2.setUserId(2);
        user2.setFirstName("user2");

        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);

        Product p = new Product();
        p.setProductName("productA");
        p.setUsers(users);

        productMapper.save(p);
    }
}

Product

@DynamoDBTable(tableName = "products")
public class Product {
    private String productName;
    private List<User> users;

    public Product() {

    }

    @DynamoDBHashKey(attributeName = "productName")
    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @DynamoDBAttribute(attributeName = "users")
    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

}

User

@DynamoDBDocument
public class User {
    private String firstName;
    private long userId;

    public User(){

    }

    @DynamoDBAttribute(attributeName = "firstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @DynamoDBAttribute(attributeName = "userId")
    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }
}

ccoffey avatar Dec 23 '14 18:12 ccoffey

I can confirm the issue.

Neither

new DynamoDBMapper(dynamoDB,DynamoDBMapperConfig.builder().withConversionSchema(ConversionSchemas.V2).build());

nor

new DynamoDBMapper(dynamoDB, new DynamoDBMapperConfig(ConversionSchemas.V2));

works as expected. DynamoDBMapper still invokes toString() for objects in Set making it become a StringSet.

akoshman avatar Sep 20 '17 11:09 akoshman