Provide Default CouchbaseCustomConversions with Enum Converters. (#1856)

Closes #1837.
This commit is contained in:
Michael Reiche
2023-10-24 17:32:41 -07:00
committed by GitHub
parent 8404616bcc
commit 19fddc939f
2 changed files with 38 additions and 1 deletions

View File

@@ -452,7 +452,7 @@ public abstract class AbstractCouchbaseConfiguration {
return customConversions;
}
Map<Class<? extends Annotation>, Class<?>> annotationToConverterMap() {
public static Map<Class<? extends Annotation>, Class<?>> annotationToConverterMap() {
Map<Class<? extends Annotation>, Class<?>> map = new HashMap();
map.put(Encrypted.class, CryptoConverter.class);
map.put(JsonValue.class, JsonValueConverter.class);

View File

@@ -31,6 +31,7 @@ import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
@@ -39,6 +40,7 @@ import org.springframework.data.convert.PropertyValueConverter;
import org.springframework.data.convert.PropertyValueConverterFactory;
import org.springframework.data.convert.PropertyValueConverterRegistrar;
import org.springframework.data.convert.SimplePropertyValueConversions;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.mapping.CouchbaseSimpleTypes;
import org.springframework.data.mapping.PersistentProperty;
@@ -145,6 +147,41 @@ public class CouchbaseCustomConversions extends org.springframework.data.convert
CouchbaseConverterConfigurationAdapter converterConfigurationAdapter = new CouchbaseConverterConfigurationAdapter();
converterConfigurationAdapter.registerConverters(converters);
// The following
ObjectMapper om = new AbstractCouchbaseConfiguration() {
@Override
public String getConnectionString() {
return null;
}
@Override
public String getUserName() {
return null;
}
@Override
public String getPassword() {
return null;
}
@Override
public String getBucketName() {
return null;
}
}.getObjectMapper();
List<Object> newConverters = new ArrayList();
newConverters.add(new OtherConverters.EnumToObject(om));
newConverters.add(new IntegerToEnumConverterFactory(om));
newConverters.add(new StringToEnumConverterFactory(om));
newConverters.add(new BooleanToEnumConverterFactory(om));
SimplePropertyValueConversions valueConversions = new SimplePropertyValueConversions();
valueConversions.setConverterFactory(
new CouchbasePropertyValueConverterFactory(null, AbstractCouchbaseConfiguration.annotationToConverterMap(), om));
valueConversions.setValueConverterRegistry(new PropertyValueConverterRegistrar().buildRegistry());
valueConversions.afterPropertiesSet(); // wraps the CouchbasePropertyValueConverterFactory with CachingPVCFactory
converterConfigurationAdapter.setPropertyValueConversions(valueConversions);
converterConfigurationAdapter.registerConverters(newConverters);
converterConfigurationAdapter.registerConverters(newConverters);
return converterConfigurationAdapter;
}