Skip to content

Propagated CouchbaseCustomConverters bean into MappingConverter #1885

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
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 @@ -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