Skip to content

DATACMNS-1597 - Skip non-simple type converter registrations. #413

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 2 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATACMNS-1597-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Anshul Mehra
* @since 2.0
*/
@Slf4j
Expand Down Expand Up @@ -107,7 +108,9 @@ public CustomConversions(StoreConversions storeConversions, Collection<?> conver
toRegister.addAll(DEFAULT_CONVERTERS);

toRegister.stream()//
.flatMap(it -> storeConversions.getRegistrationsFor(it).stream())//
.flatMap(converter -> storeConversions.getRegistrationsFor(converter).stream())//
.filter(registration -> registration.isReading() ? registration.isSimpleSourceType() : true)//
.filter(registration -> registration.isWriting() ? registration.isSimpleTargetType() : true)//
.forEach(this::register);

Collections.reverse(toRegister);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@

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

import java.math.BigInteger;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;

import org.joda.time.DateTime;
import org.junit.Test;
Expand All @@ -46,6 +53,7 @@
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
* @author Anshul Mehra
* @since 2.0
*/
public class CustomConversionsUnitTests {
Expand Down Expand Up @@ -156,6 +164,16 @@ public void registersConvertersForJsr310() {
assertThat(customConversions.hasCustomWriteTarget(java.time.LocalDateTime.class)).isTrue();
}

@Test // DATACMNS-1597
public void skipsRegisteringConvertersForNonSimpleTypes() {

CustomConversions customConversions = new CustomConversions(StoreConversions.of(CustomSimpleTypes.HOLDER,
Collections.singletonList(ZonedDateTimeToInstantConverter.INSTANCE)), Collections.emptyList());

assertThat(customConversions.hasCustomWriteTarget(ZonedDateTime.class, Instant.class)).isFalse();
assertThat(customConversions.hasCustomReadTarget(java.time.LocalDateTime.class, Instant.class)).isFalse();
}

@Test // DATAMONGO-1131, DATACMNS-1035
public void registersConvertersForThreeTenBackPort() {

Expand Down Expand Up @@ -279,6 +297,17 @@ public String convert(Object source) {

}

@WritingConverter
enum ZonedDateTimeToInstantConverter implements Converter<ZonedDateTime, Instant> {

INSTANCE;

@Override
public Instant convert(ZonedDateTime source) {
return source.toInstant();
}
}

@WritingConverter
static class FormatConverterFactory implements ConverterFactory<String, Format> {

Expand Down Expand Up @@ -312,4 +341,39 @@ public T convert(String source) {
}

static class CustomType {}

static class CustomSimpleTypes {

public static final Set<Class<?>> AUTOGENERATED_ID_TYPES;

static {
Set<Class<?>> classes = new HashSet<>();
classes.add(String.class);
classes.add(BigInteger.class);
AUTOGENERATED_ID_TYPES = Collections.unmodifiableSet(classes);

Set<Class<?>> simpleTypes = new HashSet<>();
simpleTypes.add(Pattern.class);
simpleTypes.add(UUID.class);

CUSTOM_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
}

private static final Set<Class<?>> CUSTOM_SIMPLE_TYPES;

public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(CUSTOM_SIMPLE_TYPES, true) {

@Override
public boolean isSimpleType(Class<?> type) {

if (type.getName().startsWith("java.time")) {
return false;
}

return super.isSimpleType(type);
}
};

private CustomSimpleTypes() {}
}
}