Skip to content

Commit ec8a948

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1302 - Allow ConverterFactory to be registered in CustomConversions.
We now allow registration of ConverterFactory within CustomConversions by inspecting the generic type arguments for determining the conversion source and target types. Original pull request: #330.
1 parent 38fc764 commit ec8a948

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
import org.springframework.data.mapping.model.SimpleTypeHolder;
4545
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
4646
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter;
47-
import org.springframework.data.mongodb.core.convert.MongoConverters.NamedMongoScriptToDBObjectConverter;
4847
import org.springframework.data.mongodb.core.convert.MongoConverters.DBObjectToNamedMongoScriptCoverter;
4948
import org.springframework.data.mongodb.core.convert.MongoConverters.DBObjectToStringConverter;
49+
import org.springframework.data.mongodb.core.convert.MongoConverters.NamedMongoScriptToDBObjectConverter;
5050
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
5151
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
5252
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToURLConverter;
@@ -192,8 +192,8 @@ public void registerConvertersIn(GenericConversionService conversionService) {
192192
}
193193

194194
/**
195-
* Registers a conversion for the given converter. Inspects either generics or the {@link ConvertiblePair}s returned
196-
* by a {@link GenericConverter}.
195+
* Registers a conversion for the given converter. Inspects either generics of {@link Converter} and
196+
* {@link ConverterFactory} or the {@link ConvertiblePair}s returned by a {@link GenericConverter}.
197197
*
198198
* @param converter
199199
*/
@@ -208,6 +208,10 @@ private void registerConversion(Object converter) {
208208
for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
209209
register(new ConverterRegistration(pair, isReading, isWriting));
210210
}
211+
} else if (converter instanceof ConverterFactory) {
212+
213+
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), ConverterFactory.class);
214+
register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));
211215
} else if (converter instanceof Converter) {
212216
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
213217
register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/CustomConversionsUnitTests.java

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,9 @@
2121
import java.net.URL;
2222
import java.text.DateFormat;
2323
import java.text.Format;
24+
import java.text.SimpleDateFormat;
2425
import java.util.Arrays;
26+
import java.util.Collections;
2527
import java.util.Date;
2628
import java.util.Locale;
2729
import java.util.UUID;
@@ -32,8 +34,10 @@
3234
import org.junit.Test;
3335
import org.springframework.aop.framework.ProxyFactory;
3436
import org.springframework.core.convert.converter.Converter;
37+
import org.springframework.core.convert.converter.ConverterFactory;
3538
import org.springframework.core.convert.support.DefaultConversionService;
3639
import org.springframework.core.convert.support.GenericConversionService;
40+
import org.springframework.data.convert.WritingConverter;
3741
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
3842
import org.threeten.bp.LocalDateTime;
3943

@@ -43,12 +47,11 @@
4347
* Unit tests for {@link CustomConversions}.
4448
*
4549
* @author Oliver Gierke
46-
* @auhtor Christoph Strobl
50+
* @author Christoph Strobl
4751
*/
4852
public class CustomConversionsUnitTests {
4953

5054
@Test
51-
@SuppressWarnings("unchecked")
5255
public void findsBasicReadAndWriteConversions() {
5356

5457
CustomConversions conversions = new CustomConversions(Arrays.asList(FormatToStringConverter.INSTANCE,
@@ -62,7 +65,6 @@ public void findsBasicReadAndWriteConversions() {
6265
}
6366

6467
@Test
65-
@SuppressWarnings("unchecked")
6668
public void considersSubtypesCorrectly() {
6769

6870
CustomConversions conversions = new CustomConversions(Arrays.asList(NumberToStringConverter.INSTANCE,
@@ -132,6 +134,7 @@ public void populatesConversionServiceCorrectly() {
132134
*/
133135
@Test
134136
public void doesNotConsiderTypeSimpleIfOnlyReadConverterIsRegistered() {
137+
135138
CustomConversions conversions = new CustomConversions(Arrays.asList(StringToFormatConverter.INSTANCE));
136139
assertThat(conversions.isSimpleType(Format.class), is(false));
137140
}
@@ -257,6 +260,17 @@ public void registersConvertersForThreeTenBackPort() {
257260
assertThat(customConversions.hasCustomWriteTarget(LocalDateTime.class), is(true));
258261
}
259262

263+
/**
264+
* @see DATAMONGO-1302
265+
*/
266+
@Test
267+
public void registersConverterFactoryCorrectly() {
268+
269+
CustomConversions customConversions = new CustomConversions(Collections.singletonList(new FormatConverterFactory()));
270+
271+
assertThat(customConversions.getCustomWriteTarget(String.class, SimpleDateFormat.class), notNullValue());
272+
}
273+
260274
private static Class<?> createProxyTypeFor(Class<?> type) {
261275

262276
ProxyFactory factory = new ProxyFactory();
@@ -331,4 +345,37 @@ public String convert(Object source) {
331345
}
332346

333347
}
348+
349+
@WritingConverter
350+
static class FormatConverterFactory implements ConverterFactory<String, Format> {
351+
352+
@Override
353+
public <T extends Format> Converter<String, T> getConverter(Class<T> targetType) {
354+
return new StringToFormat<T>(targetType);
355+
}
356+
357+
private static final class StringToFormat<T extends Format> implements Converter<String, T> {
358+
359+
private final Class<T> targetType;
360+
361+
public StringToFormat(Class<T> targetType) {
362+
this.targetType = targetType;
363+
}
364+
365+
@Override
366+
public T convert(String source) {
367+
368+
if (source.length() == 0) {
369+
return null;
370+
}
371+
372+
try {
373+
return targetType.newInstance();
374+
} catch (Exception e) {
375+
throw new IllegalArgumentException(e.getMessage(), e);
376+
}
377+
}
378+
}
379+
380+
}
334381
}

0 commit comments

Comments
 (0)