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

TableSchema.converterForAttribute() returns wrong generic type

Open rkennedy-mode opened this issue 2 years ago • 3 comments

Describe the bug

The TableSchema<T> interface has a method, converterForAttribute(Object key), which returns an AttributeConverter<T>. This AttributeConverter is meant to convert the attribute behind the given key. Unfortunately, the generic type here (T) is the type for the table record, not for the attribute.

If I have the following DynamoDbBean (non-compilable, just for explanation purposes):

@DynamoDbBean
public class ShoppingCartItem{
    public static final String CUSTOMER_ID_SECONDARY_INDEX_NAME = "shopping_cart_item_by_customer_id";

    private UUID customerId;

    @DynamoDbSecondaryPartitionKey(indexNames = CUSTOMER_ID_SECONDARY_INDEX_NAME)
    public UUID getCustomerId() {
        return customerId;
    }
}

If then create a corresponding DynamoDbTable<ShoppingCartItem> and try fetching the converter for the secondary partition key:

DynamoDbTable<ShoppingCartItem> table = ...;

String partitionKey = table.tableSchema().tableMetadata().indexPartitionKey(ShoppingCartItem.CUSTOMER_ID_SECONDARY_INDEX_NAME);

AttributeConverter<ShoppingCartItem> converter = table.tableSchema().converterForAttribute(partitionKey);

The AttributeConverter returned is of type AttributeConverter<ShoppingCartItem> instead of type AttributeConverter<UUID>. This makes it impossible to use the TableSchema to fetch the corresponding AttributeConverter, because it will always return an AttributeConverter scoped to the type of the table, not of the attribute.

Expected Behavior

TableSchema.converterForAttribute(Object key) should return a generic type other than AttributeConverter<T> so callers can use it properly.

Current Behavior

Described above.

Reproduction Steps

Described above.

Possible Solution

Change the return type of TableSchema.converterForAttribute(Object key).

Additional Information/Context

No response

AWS Java SDK version used

dynamodb-enhanced-2.20.56

JDK version used

1.8

Operating System and version

macOS 13.3.1

rkennedy-mode avatar May 02 '23 22:05 rkennedy-mode

@rkennedy-mode Apologies, I thought I had posted a comment here before, but clearly I didn't.

It's definitely an odd behavior, and we think there's a reason behind it when we made the design, but the reason was not documented. Right now this is a bug.

debora-ito avatar Aug 04 '23 19:08 debora-ito

Any updates on this issue?

Paramood avatar Aug 30 '24 11:08 Paramood

Having the same problem. I can't see what the reason would be, it just seems plain wrong. It should return AttributeConverter<?>.

sean-hernon avatar Sep 05 '24 12:09 sean-hernon