Skip to content

Commit

Permalink
Consider target type for custom converter determination.
Browse files Browse the repository at this point in the history
Closes #1842
Original pull request #1876
  • Loading branch information
ngocnhan-tran1996 authored and schauder committed Sep 16, 2024
1 parent 033ac1f commit 5b76259
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,9 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {

if (getConversions().isSimpleType(value.getClass())) {

Optional<Class<?>> customWriteTarget = getConversions().getCustomWriteTarget(type.getType());
Optional<Class<?>> customWriteTarget = getConversions().hasCustomWriteTarget(value.getClass(), type.getType())
? getConversions().getCustomWriteTarget(value.getClass(), type.getType())
: getConversions().getCustomWriteTarget(type.getType());
if (customWriteTarget.isPresent()) {
return getConversionService().convert(value, customWriteTarget.get());
}
Expand Down Expand Up @@ -725,12 +727,15 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
return writeCollection((Iterable<?>) value, type);
}

RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(value.getClass());
if (getMappingContext().hasPersistentEntityFor(value.getClass())) {

if (persistentEntity != null) {
RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(value.getClass());

Object id = persistentEntity.getIdentifierAccessor(value).getIdentifier();
return writeValue(id, type);
if (persistentEntity != null) {

Object id = persistentEntity.getIdentifierAccessor(value).getIdentifier();
return writeValue(id, type);
}
}

return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
Expand All @@ -26,23 +25,28 @@
import java.util.Objects;
import java.util.Set;

import java.util.UUID;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceCreator;
import org.springframework.data.convert.ConverterBuilder;
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.CustomConversions.StoreConversions;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.projection.EntityProjection;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.domain.RowDocument;
import org.springframework.data.util.TypeInformation;

/**
* Unit tests for {@link MappingRelationalConverter}.
Expand Down Expand Up @@ -210,6 +214,27 @@ void projectShouldReadProjectionWithNestedEntity() {
assertThat(person.getAddresses()).extracting(Address::getStreet).hasSize(1).containsOnly("hwy");
}

@SuppressWarnings("unchecked")
@Test
void shouldApplyGenericTypeConverter() {

converter = new MappingRelationalConverter(converter.getMappingContext(),
new CustomConversions(StoreConversions.NONE, List.of(GenericTypeConverter.INSTANCE)));

var stringResult = (GenericClass<String>) converter.writeValue("test", TypeInformation.of(GenericClass.class));
var uuidResult = (GenericClass<UUID>) converter.writeValue(UUID.fromString("1234567-8910-1112-1314-151617181920"), TypeInformation.of(GenericClass.class));

var stringGeneric = new GenericClass<>("test");
var stringGenericResult = (String) converter.writeValue(stringGeneric, TypeInformation.of(String.class));
var uuidGeneric = new GenericClass<>(UUID.fromString("1234567-8910-1112-1314-151617181920"));
var uuidGenericResult = (UUID) converter.writeValue(uuidGeneric, TypeInformation.of(UUID.class));

assertThat(stringResult.value()).isEqualTo("test");
assertThat(uuidResult.value()).isEqualTo(UUID.fromString("1234567-8910-1112-1314-151617181920"));
assertThat(stringGenericResult).isEqualTo("test");
assertThat(uuidGenericResult).isEqualTo(UUID.fromString("1234567-8910-1112-1314-151617181920"));
}

static class SimpleType {

@Id String id;
Expand Down Expand Up @@ -365,4 +390,31 @@ public MyEnum convert(String source) {

}

@WritingConverter
enum GenericTypeConverter implements GenericConverter {

INSTANCE;

@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Set.of(
new ConvertiblePair(String.class, GenericClass.class),
new ConvertiblePair(UUID.class, GenericClass.class),
new ConvertiblePair(GenericClass.class, String.class),
new ConvertiblePair(GenericClass.class, UUID.class)
);
}

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (targetType.getType() == GenericClass.class)
return new GenericClass<>(source);

return ((GenericClass<?>) source).value();
}

}

public record GenericClass<T>(T value) { }

}

0 comments on commit 5b76259

Please sign in to comment.