Skip to content

Commit 2281ba2

Browse files
committed
Provide Default CouchbaseCustomConversions with Enum Converters. (#1856)
Closes #1837.
1 parent 97a37a5 commit 2281ba2

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public CustomConversions customConversions(CryptoManager cryptoManager, ObjectMa
452452
return customConversions;
453453
}
454454

455-
Map<Class<? extends Annotation>, Class<?>> annotationToConverterMap() {
455+
public static Map<Class<? extends Annotation>, Class<?>> annotationToConverterMap() {
456456
Map<Class<? extends Annotation>, Class<?>> map = new HashMap();
457457
map.put(Encrypted.class, CryptoConverter.class);
458458
map.put(JsonValue.class, JsonValueConverter.class);

src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseCustomConversions.java

+37
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Set;
3232
import java.util.function.Consumer;
3333

34+
import com.fasterxml.jackson.databind.ObjectMapper;
3435
import org.springframework.core.convert.converter.Converter;
3536
import org.springframework.core.convert.converter.ConverterFactory;
3637
import org.springframework.core.convert.converter.GenericConverter;
@@ -39,6 +40,7 @@
3940
import org.springframework.data.convert.PropertyValueConverterFactory;
4041
import org.springframework.data.convert.PropertyValueConverterRegistrar;
4142
import org.springframework.data.convert.SimplePropertyValueConversions;
43+
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
4244
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
4345
import org.springframework.data.couchbase.core.mapping.CouchbaseSimpleTypes;
4446
import org.springframework.data.mapping.PersistentProperty;
@@ -145,6 +147,41 @@ public static CouchbaseConverterConfigurationAdapter from(List<?> converters) {
145147

146148
CouchbaseConverterConfigurationAdapter converterConfigurationAdapter = new CouchbaseConverterConfigurationAdapter();
147149
converterConfigurationAdapter.registerConverters(converters);
150+
// The following
151+
ObjectMapper om = new AbstractCouchbaseConfiguration() {
152+
@Override
153+
public String getConnectionString() {
154+
return null;
155+
}
156+
157+
@Override
158+
public String getUserName() {
159+
return null;
160+
}
161+
162+
@Override
163+
public String getPassword() {
164+
return null;
165+
}
166+
167+
@Override
168+
public String getBucketName() {
169+
return null;
170+
}
171+
}.getObjectMapper();
172+
List<Object> newConverters = new ArrayList();
173+
newConverters.add(new OtherConverters.EnumToObject(om));
174+
newConverters.add(new IntegerToEnumConverterFactory(om));
175+
newConverters.add(new StringToEnumConverterFactory(om));
176+
newConverters.add(new BooleanToEnumConverterFactory(om));
177+
SimplePropertyValueConversions valueConversions = new SimplePropertyValueConversions();
178+
valueConversions.setConverterFactory(
179+
new CouchbasePropertyValueConverterFactory(null, AbstractCouchbaseConfiguration.annotationToConverterMap(), om));
180+
valueConversions.setValueConverterRegistry(new PropertyValueConverterRegistrar().buildRegistry());
181+
valueConversions.afterPropertiesSet(); // wraps the CouchbasePropertyValueConverterFactory with CachingPVCFactory
182+
converterConfigurationAdapter.setPropertyValueConversions(valueConversions);
183+
converterConfigurationAdapter.registerConverters(newConverters);
184+
converterConfigurationAdapter.registerConverters(newConverters);
148185
return converterConfigurationAdapter;
149186
}
150187

0 commit comments

Comments
 (0)