-
Notifications
You must be signed in to change notification settings - Fork 910
DefaultStringConverterProvider can't convert Map with Enum as a key #2102
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
Comments
@bbednarek the default MapAttributeConverter only supports StringAttributeConverter for keys: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/internal/converter/attribute/MapAttributeConverter.html I'm marking this as a feature request. In the meantime to get unblocked you can write a custom Attribute converter, check the DynamoDB Enhanced Client Overview for examples: https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced#control-attribute-conversion |
Recently solved a similar task using the official dynamodb-enhanced client which contains DynamoDbConvertedBy annotation. There you can specify a converter class that implements the AttributeConverter interface for your data type. Below is just my example of converter implementation for Map<EnumClass, String> type:
Class itself:
|
…4aba387b9 Pull request: release <- staging/99eafe6d-0258-434f-81e0-a5b4aba387b9
@antukhov Can you provide a working example pls? I added an AttributeConverterClass, Getter-Method and the Annotation - still the same IllegalArgument No string converter exists ... exception. Thanks! |
@antukhov I did something similar just for complicated map value which is also annotated with public class EnumMapAttributeConverter implements AttributeConverter<Map<MVDayOfWeek, SeoLineFrequency>> {
private static final BeanTableSchema<SeoLineFrequency> seoLineFrequencyBeanTableSchema = TableSchema.fromBean(SeoLineFrequency.class);
@Override
public AttributeValue transformFrom(Map<MVDayOfWeek, SeoLineFrequency> input) {
return AttributeValue.fromM(input.entrySet().stream()
.collect(Collectors.toMap(
entry -> entry.getKey().name(),
entry -> AttributeValue.fromM(seoLineFrequencyBeanTableSchema.itemToMap(entry.getValue(), true))
)));
}
@Override
public Map<MVDayOfWeek, SeoLineFrequency> transformTo(AttributeValue input) {
return input.m().entrySet().stream()
.collect(Collectors.toMap(
entry -> MVDayOfWeek.valueOf(entry.getKey()),
entry -> seoLineFrequencyBeanTableSchema.mapToItem(entry.getValue().m())
));
}
@Override
public EnhancedType<Map<MVDayOfWeek, SeoLineFrequency>> type() {
return EnhancedType.mapOf(MVDayOfWeek.class, SeoLineFrequency.class);
}
@Override
public AttributeValueType attributeValueType() {
return AttributeValueType.M;
}
} |
Mapping of the
Map<K,String>
whereK
is enum doesn't work.Example entity:
Example
TableSchema
Describe the bug
Mapping doesn't work.
Expected Behavior
Mapping works.
Current Behavior
It fails with the following error:
Full stacktrace:
If I swap Map key and value:
it works!
Steps to Reproduce
Follow the steps above.
Possible Solution
Support Enum as a key in a
Map
.Context
I am trying to save a map with enum as key.
Your Environment
2.15.2
14
MacOS 10.15.7
Related to the comment #35 (comment)
The text was updated successfully, but these errors were encountered: