Skip to content

Provide Default CouchbaseCustomConversions with Enum Converters. #1856

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

Merged
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 @@ -452,7 +452,7 @@ public CustomConversions customConversions(CryptoManager cryptoManager, ObjectMa
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand All @@ -39,6 +40,7 @@
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;
Expand Down Expand Up @@ -145,6 +147,41 @@ public static CouchbaseConverterConfigurationAdapter from(List<?> converters) {

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;
}

Expand Down