From 5eea30e4da55975955b4201e7563c44678dfe8c3 Mon Sep 17 00:00:00 2001 From: Vipul Gupta <86769501+bipoool@users.noreply.github.com> Date: Thu, 11 Jan 2024 05:04:44 +0530 Subject: [PATCH] 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. --- .../AbstractCouchbaseConfiguration.java | 7 ++---- .../convert/AbstractCouchbaseConverter.java | 9 ++++--- .../convert/MappingCouchbaseConverter.java | 25 +++++++++++++------ .../AbstractingMappingCouchbaseConverter.java | 6 +++-- .../data/couchbase/domain/Config.java | 3 +-- .../CustomMappingCouchbaseConverter.java | 18 +++++++++++++ ...aseAbstractRepositoryIntegrationTests.java | 4 +-- 7 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java b/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java index 29a91548..c97f3d8a 100644 --- a/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java +++ b/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java @@ -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; } diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java b/src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java index 06f71990..f22e1337 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java @@ -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; } /** diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java b/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java index 50e8852f..070750d6 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java @@ -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 typeKey - * 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, 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 typeKey + * 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, 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 diff --git a/src/test/java/org/springframework/data/couchbase/domain/AbstractingMappingCouchbaseConverter.java b/src/test/java/org/springframework/data/couchbase/domain/AbstractingMappingCouchbaseConverter.java index 9a084137..74d68039 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/AbstractingMappingCouchbaseConverter.java +++ b/src/test/java/org/springframework/data/couchbase/domain/AbstractingMappingCouchbaseConverter.java @@ -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, CouchbasePersistentProperty> mappingContext, - final String typeKey) { - super(mappingContext, typeKey); + final String typeKey, + final CouchbaseCustomConversions couchbaseCustomConversions) { + super(mappingContext, typeKey, couchbaseCustomConversions); this.typeMapper = new AbstractingTypeMapper(typeKey); } diff --git a/src/test/java/org/springframework/data/couchbase/domain/Config.java b/src/test/java/org/springframework/data/couchbase/domain/Config.java index 6bd6bae4..0d9cbc04 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/Config.java +++ b/src/test/java/org/springframework/data/couchbase/domain/Config.java @@ -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; } diff --git a/src/test/java/org/springframework/data/couchbase/domain/CustomMappingCouchbaseConverter.java b/src/test/java/org/springframework/data/couchbase/domain/CustomMappingCouchbaseConverter.java index d06141c3..ff4e0782 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/CustomMappingCouchbaseConverter.java +++ b/src/test/java/org/springframework/data/couchbase/domain/CustomMappingCouchbaseConverter.java @@ -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, CouchbasePersistentProperty> mappingContext, + final String typeKey, + final CouchbaseCustomConversions couchbaseCustomConversions) { + super(mappingContext, typeKey, couchbaseCustomConversions); + this.typeMapper = new TypeBasedCouchbaseTypeMapper(typeKey); + } + } diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java index 60929c35..ba67e95d 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java @@ -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; }