Skip to content

MappingRelationalConverter Add Converter Condition #1876

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

Closed
wants to merge 5 commits into from
Closed
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
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) { }

}
Loading