Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "Amazon DynamoDB Enhanced Client",
"contributor": "",
"description": "Improve missing converter error handling in DynamoDB Enhanced Client"
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ZoneIdAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ZoneOffsetAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ZonedDateTimeAsStringAttributeConverter;
import software.amazon.awssdk.utils.CollectionUtils;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.Validate;

Expand Down Expand Up @@ -157,17 +158,22 @@ private <T> Optional<AttributeConverter<T>> findConverterInternal(EnhancedType<T
return Optional.of(converter);
}

if (type.rawClass().isAssignableFrom(Map.class)) {
converter = createMapConverter(type);
} else if (type.rawClass().isAssignableFrom(Set.class)) {
converter = createSetConverter(type);
} else if (type.rawClass().isAssignableFrom(List.class)) {
EnhancedType<T> innerType = (EnhancedType<T>) type.rawClassParameters().get(0);
AttributeConverter<?> innerConverter = findConverter(innerType)
.orElseThrow(() -> new IllegalStateException("Converter not found for " + type));
return Optional.of((AttributeConverter<T>) ListAttributeConverter.create(innerConverter));
} else if (type.rawClass().isEnum()) {
return Optional.of(EnumAttributeConverter.create(((EnhancedType<? extends Enum>) type).rawClass()));
if (!Object.class.equals(type.rawClass())) {
if (type.rawClass().isAssignableFrom(Map.class)) {
requireTypeParameters(type);
converter = createMapConverter(type);
} else if (type.rawClass().isAssignableFrom(Set.class)) {
requireTypeParameters(type);
converter = createSetConverter(type);
} else if (type.rawClass().isAssignableFrom(List.class)) {
requireTypeParameters(type);
EnhancedType<T> innerType = (EnhancedType<T>) type.rawClassParameters().get(0);
AttributeConverter<?> innerConverter = findConverter(innerType)
.orElseThrow(() -> new IllegalStateException("Converter not found for " + type));
return Optional.of((AttributeConverter<T>) ListAttributeConverter.create(innerConverter));
} else if (type.rawClass().isEnum()) {
return Optional.of(EnumAttributeConverter.create(((EnhancedType<? extends Enum>) type).rawClass()));
}
}

if (type.tableSchema().isPresent()) {
Expand All @@ -186,6 +192,12 @@ private boolean shouldCache(Class<?> type) {
return !type.isAnonymousClass();
}

private static void requireTypeParameters(EnhancedType<?> type) {
if (CollectionUtils.isNullOrEmpty(type.rawClassParameters())) {
throw new IllegalStateException("Converter not found for " + type + ". Type parameters are required for this type.");
}
}

@SuppressWarnings("unchecked")
private <T> AttributeConverter<T> createMapConverter(EnhancedType<T> type) {
EnhancedType<?> keyType = type.rawClassParameters().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.utils.CollectionUtils;
import software.amazon.awssdk.utils.Lazy;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.Validate;
Expand Down Expand Up @@ -89,15 +90,22 @@ public static Builder builder() {
public static <T> AttributeConverter<T> converterForClass(EnhancedType<T> type,
ChainConverterProvider chainConverterProvider) {

if (type.rawClass().isAssignableFrom(List.class)) {
return (AttributeConverter<T>) ListAttributeConverter
.create(converterForClass(type.rawClassParameters().get(0), chainConverterProvider));
}
if (type.rawClass().isAssignableFrom(Map.class)) {
return (AttributeConverter<T>) MapAttributeConverter.mapConverter(
StringConverterProvider.defaultProvider().converterFor(type.rawClassParameters().get(0)),
converterForClass(type.rawClassParameters().get(1), chainConverterProvider));
if (!Object.class.equals(type.rawClass())) {
if (type.rawClass().isAssignableFrom(List.class)) {
requireTypeParameters(type);
return (AttributeConverter<T>) ListAttributeConverter
.create(converterForClass(type.rawClassParameters().get(0), chainConverterProvider));
}
if (type.rawClass().isAssignableFrom(Map.class)) {
requireTypeParameters(type);
return (AttributeConverter<T>) MapAttributeConverter.mapConverter(
StringConverterProvider.defaultProvider().converterFor(type.rawClassParameters().get(0)),
converterForClass(type.rawClassParameters().get(1), chainConverterProvider));
}
}
// TODO: what about the case when type.rawClass() is Object or Unsupported type
// and we have overridden the default converter provider with a custom one that can / cannot handle this type?
// We should check for that case here.
return Optional.ofNullable(chainConverterProvider.converterFor(type))
.orElseThrow(() -> new IllegalStateException(
"AttributeConverter not found for class " + type
Expand Down Expand Up @@ -496,4 +504,10 @@ private static void checkAndValidateClass(Class<?> type, boolean isPut) {
String.format(VALIDATE_TYPE_ERROR, "Map", isPut ? "put" : "get", "Map"));

}

private static void requireTypeParameters(EnhancedType<?> type) {
if (CollectionUtils.isNullOrEmpty(type.rawClassParameters())) {
throw new IllegalStateException("Converter not found for " + type + ". Type parameters are required for this type.");
}
}
}
Loading