Skip to content

Commit

Permalink
Propagated CouchbaseCustomConverters bean into MappingConverter (#1885)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
bipoool authored Jan 10, 2024
1 parent 359ade2 commit 6e17572
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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;
Expand Down Expand Up @@ -67,8 +66,6 @@
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;
Expand Down Expand Up @@ -100,6 +97,7 @@
* @author Subhashni Balakrishnan
* @author Jorge Rodriguez Martin
* @author Michael Reiche
* @author Vipul Gupta
*/
@Configuration
public abstract class AbstractCouchbaseConfiguration {
Expand Down Expand Up @@ -280,8 +278,7 @@ public String typeKey() {
@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Michael Nitschinger
* @author Mark Paluch
* @author Michael Reiche
* @author Vipul Gupta
*/
public abstract class AbstractCouchbaseConverter implements CouchbaseConverter, InitializingBean {

Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
* @author Mark Paluch
* @author Michael Reiche
* @author Remi Bleuse
* @author Vipul Gupta
*/
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implements ApplicationContextAware {

Expand Down Expand Up @@ -140,22 +141,32 @@ public MappingCouchbaseConverter(
this(mappingContext, null);
}

/**
* 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.
*/
public MappingCouchbaseConverter(
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
final String typeKey) {
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) {
super(new DefaultConversionService());
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
// 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,4 +38,21 @@ public CustomMappingCouchbaseConverter(
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will
// use the DocumentType annotation
MappingCouchbaseConverter converter = new AbstractingMappingCouchbaseConverter(couchbaseMappingContext,
typeKey());
converter.setCustomConversions(couchbaseCustomConversions);
typeKey(),
couchbaseCustomConversions);
return converter;
}

Expand Down

0 comments on commit 6e17572

Please sign in to comment.