Propagated CouchbaseCustomConverters bean into MappingConverter (#1885)

* Propagated CouchbaseCustomConverters bean into MappingConverter.

Just a polishing fix to propagate CouchbaseCustomConversions directly to MappingCouchbaseConverter -> AbstractCouchbaseConverter , So that we won't have to set it explicitly.

* Removed unnecessary constructor.
This commit is contained in:
Vipul Gupta
2024-01-11 05:04:44 +05:30
committed by mikereiche
parent 424736fc19
commit 5eea30e4da
7 changed files with 51 additions and 21 deletions

View File

@@ -35,7 +35,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.PropertyValueConverterRegistrar;
@@ -67,8 +66,6 @@ import org.springframework.data.mapping.model.FieldNamingStrategy;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.transaction.support.TransactionTemplate;
@@ -100,6 +97,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Subhashni Balakrishnan
* @author Jorge Rodriguez Martin
* @author Michael Reiche
* @author Vipul Gupta
*/
@Configuration
public abstract class AbstractCouchbaseConfiguration {
@@ -280,8 +278,7 @@ public abstract class AbstractCouchbaseConfiguration {
@Bean
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
CouchbaseCustomConversions couchbaseCustomConversions) {
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(couchbaseMappingContext, typeKey());
converter.setCustomConversions(couchbaseCustomConversions);
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(couchbaseMappingContext, typeKey(), couchbaseCustomConversions);
couchbaseMappingContext.setSimpleTypeHolder(couchbaseCustomConversions.getSimpleTypeHolder());
return converter;
}

View File

@@ -37,6 +37,7 @@ import org.springframework.data.util.TypeInformation;
* @author Michael Nitschinger
* @author Mark Paluch
* @author Michael Reiche
* @author Vipul Gupta
*/
public abstract class AbstractCouchbaseConverter implements CouchbaseConverter, InitializingBean {
@@ -53,15 +54,17 @@ public abstract class AbstractCouchbaseConverter implements CouchbaseConverter,
/**
* Holds the custom conversions.
*/
protected CustomConversions conversions = new CouchbaseCustomConversions(Collections.emptyList());
protected CustomConversions conversions;
/**
* Create a new converter and hand it over the {@link ConversionService}
* Create a new converter with custom conversions and hand it over the {@link ConversionService}
*
* @param conversionService the conversion service to use.
* @param customConversions the custom conversions to use
*/
protected AbstractCouchbaseConverter(final GenericConversionService conversionService) {
protected AbstractCouchbaseConverter(final GenericConversionService conversionService, final CustomConversions customConversions) {
this.conversionService = conversionService;
this.conversions = customConversions;
}
/**

View File

@@ -90,6 +90,7 @@ import com.couchbase.client.java.json.JsonObject;
* @author Mark Paluch
* @author Michael Reiche
* @author Remi Bleuse
* @author Vipul Gupta
*/
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implements ApplicationContextAware {
@@ -141,8 +142,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
}
/**
* Create a new {@link MappingCouchbaseConverter} that will store class name for complex types in the <i>typeKey</i>
* attribute.
* Create a new {@link MappingCouchbaseConverter}
*
* @param mappingContext the mapping context to use.
* @param typeKey the attribute name to use to store complex types class name.
@@ -150,12 +150,23 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
public MappingCouchbaseConverter(
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
final String typeKey) {
super(new DefaultConversionService());
this(mappingContext, typeKey, new CouchbaseCustomConversions(Collections.emptyList()));
}
/**
* Create a new {@link MappingCouchbaseConverter} that will store class name for complex types in the <i>typeKey</i>
* attribute.
*
* @param mappingContext the mapping context to use.
* @param typeKey the attribute name to use to store complex types class name.
* @param customConversions the custom conversions to use
*/
public MappingCouchbaseConverter(
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
final String typeKey,
final CustomConversions customConversions) {
super(new DefaultConversionService(), customConversions);
this.mappingContext = mappingContext;
// this is how the MappingCouchbaseConverter gets the custom conversions.
// the conversions Service gets them in afterPropertiesSet()
CustomConversions customConversions = new CouchbaseCustomConversions(Collections.emptyList());
this.setCustomConversions(customConversions);
// Don't rely on setSimpleTypeHolder being called in afterPropertiesSet() - some integration tests do not use it
// if the mappingContext does not have the SimpleTypes, it will not know that they have converters, then it will
// try to access the fields of the type and (maybe) fail with InaccessibleObjectException

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.domain;
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
@@ -37,8 +38,9 @@ public class AbstractingMappingCouchbaseConverter extends MappingCouchbaseConver
*/
public AbstractingMappingCouchbaseConverter(
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
final String typeKey) {
super(mappingContext, typeKey);
final String typeKey,
final CouchbaseCustomConversions couchbaseCustomConversions) {
super(mappingContext, typeKey, couchbaseCustomConversions);
this.typeMapper = new AbstractingTypeMapper(typeKey);
}

View File

@@ -211,8 +211,7 @@ public class Config extends AbstractCouchbaseConfiguration {
// that has an getAliasFor(info) that just returns getType().getName().
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will
// use the DocumentType annotation
MappingCouchbaseConverter converter = new CustomMappingCouchbaseConverter(couchbaseMappingContext, typeKey());
converter.setCustomConversions(couchbaseCustomConversions);
MappingCouchbaseConverter converter = new CustomMappingCouchbaseConverter(couchbaseMappingContext, typeKey(), couchbaseCustomConversions);
return converter;
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.domain;
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
@@ -37,4 +38,21 @@ public class CustomMappingCouchbaseConverter extends MappingCouchbaseConverter {
this.typeMapper = new TypeBasedCouchbaseTypeMapper(typeKey);
}
/**
* this constructer creates a TypeBasedCouchbaseTypeMapper with the specified couchbaseCustomConversions and typeKey
* while MappingCouchbaseConverter uses a DefaultCouchbaseTypeMapper typeMapper = new DefaultCouchbaseTypeMapper(typeKey != null ? typeKey :
* TYPEKEY_DEFAULT);
*
* @param mappingContext
* @param typeKey - the typeKey to be used (normally "_class")
* @param couchbaseCustomConversions - custom conversions to use
*/
public CustomMappingCouchbaseConverter(
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
final String typeKey,
final CouchbaseCustomConversions couchbaseCustomConversions) {
super(mappingContext, typeKey, couchbaseCustomConversions);
this.typeMapper = new TypeBasedCouchbaseTypeMapper(typeKey);
}
}

View File

@@ -124,8 +124,8 @@ public class CouchbaseAbstractRepositoryIntegrationTests extends ClusterAwareInt
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will
// use the DocumentType annotation
MappingCouchbaseConverter converter = new AbstractingMappingCouchbaseConverter(couchbaseMappingContext,
typeKey());
converter.setCustomConversions(couchbaseCustomConversions);
typeKey(),
couchbaseCustomConversions);
return converter;
}