Skip to content

Commit 82d1798

Browse files
bipooolmikereiche
authored andcommitted
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.
1 parent 9a0dd72 commit 82d1798

File tree

7 files changed

+50
-20
lines changed

7 files changed

+50
-20
lines changed

src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
3636
import org.springframework.context.annotation.Configuration;
3737
import org.springframework.context.annotation.Role;
38-
import org.springframework.core.convert.converter.GenericConverter;
3938
import org.springframework.core.type.filter.AnnotationTypeFilter;
4039
import org.springframework.data.convert.CustomConversions;
4140
import org.springframework.data.convert.PropertyValueConverterRegistrar;
@@ -67,8 +66,6 @@
6766
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
6867
import org.springframework.transaction.TransactionManager;
6968
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
70-
import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;
71-
import org.springframework.transaction.config.TransactionManagementConfigUtils;
7269
import org.springframework.transaction.interceptor.TransactionAttributeSource;
7370
import org.springframework.transaction.interceptor.TransactionInterceptor;
7471
import org.springframework.transaction.support.TransactionTemplate;
@@ -100,6 +97,7 @@
10097
* @author Subhashni Balakrishnan
10198
* @author Jorge Rodriguez Martin
10299
* @author Michael Reiche
100+
* @author Vipul Gupta
103101
*/
104102
@Configuration
105103
public abstract class AbstractCouchbaseConfiguration {
@@ -280,8 +278,7 @@ public String typeKey() {
280278
@Bean
281279
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
282280
CouchbaseCustomConversions couchbaseCustomConversions) {
283-
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(couchbaseMappingContext, typeKey());
284-
converter.setCustomConversions(couchbaseCustomConversions);
281+
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(couchbaseMappingContext, typeKey(), couchbaseCustomConversions);
285282
couchbaseMappingContext.setSimpleTypeHolder(couchbaseCustomConversions.getSimpleTypeHolder());
286283
return converter;
287284
}

src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Michael Nitschinger
3838
* @author Mark Paluch
3939
* @author Michael Reiche
40+
* @author Vipul Gupta
4041
*/
4142
public abstract class AbstractCouchbaseConverter implements CouchbaseConverter, InitializingBean {
4243

@@ -53,15 +54,17 @@ public abstract class AbstractCouchbaseConverter implements CouchbaseConverter,
5354
/**
5455
* Holds the custom conversions.
5556
*/
56-
protected CustomConversions conversions = new CouchbaseCustomConversions(Collections.emptyList());
57+
protected CustomConversions conversions;
5758

5859
/**
59-
* Create a new converter and hand it over the {@link ConversionService}
60+
* Create a new converter with custom conversions and hand it over the {@link ConversionService}
6061
*
6162
* @param conversionService the conversion service to use.
63+
* @param customConversions the custom conversions to use
6264
*/
63-
protected AbstractCouchbaseConverter(final GenericConversionService conversionService) {
65+
protected AbstractCouchbaseConverter(final GenericConversionService conversionService, final CustomConversions customConversions) {
6466
this.conversionService = conversionService;
67+
this.conversions = customConversions;
6568
}
6669

6770
/**

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
* @author Mark Paluch
9191
* @author Michael Reiche
9292
* @author Remi Bleuse
93+
* @author Vipul Gupta
9394
*/
9495
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implements ApplicationContextAware {
9596

@@ -140,22 +141,32 @@ public MappingCouchbaseConverter(
140141
this(mappingContext, null);
141142
}
142143

144+
/**
145+
* Create a new {@link MappingCouchbaseConverter}
146+
*
147+
* @param mappingContext the mapping context to use.
148+
* @param typeKey the attribute name to use to store complex types class name.
149+
*/
150+
public MappingCouchbaseConverter(
151+
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
152+
final String typeKey) {
153+
this(mappingContext, typeKey, new CouchbaseCustomConversions(Collections.emptyList()));
154+
}
155+
143156
/**
144157
* Create a new {@link MappingCouchbaseConverter} that will store class name for complex types in the <i>typeKey</i>
145158
* attribute.
146159
*
147160
* @param mappingContext the mapping context to use.
148161
* @param typeKey the attribute name to use to store complex types class name.
162+
* @param customConversions the custom conversions to use
149163
*/
150164
public MappingCouchbaseConverter(
151165
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
152-
final String typeKey) {
153-
super(new DefaultConversionService());
166+
final String typeKey,
167+
final CustomConversions customConversions) {
168+
super(new DefaultConversionService(), customConversions);
154169
this.mappingContext = mappingContext;
155-
// this is how the MappingCouchbaseConverter gets the custom conversions.
156-
// the conversions Service gets them in afterPropertiesSet()
157-
CustomConversions customConversions = new CouchbaseCustomConversions(Collections.emptyList());
158-
this.setCustomConversions(customConversions);
159170
// Don't rely on setSimpleTypeHolder being called in afterPropertiesSet() - some integration tests do not use it
160171
// if the mappingContext does not have the SimpleTypes, it will not know that they have converters, then it will
161172
// try to access the fields of the type and (maybe) fail with InaccessibleObjectException

src/test/java/org/springframework/data/couchbase/domain/AbstractingMappingCouchbaseConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.domain;
1717

18+
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
1819
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
1920
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
2021
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
@@ -37,8 +38,9 @@ public class AbstractingMappingCouchbaseConverter extends MappingCouchbaseConver
3738
*/
3839
public AbstractingMappingCouchbaseConverter(
3940
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
40-
final String typeKey) {
41-
super(mappingContext, typeKey);
41+
final String typeKey,
42+
final CouchbaseCustomConversions couchbaseCustomConversions) {
43+
super(mappingContext, typeKey, couchbaseCustomConversions);
4244
this.typeMapper = new AbstractingTypeMapper(typeKey);
4345
}
4446

src/test/java/org/springframework/data/couchbase/domain/Config.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
211211
// that has an getAliasFor(info) that just returns getType().getName().
212212
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will
213213
// use the DocumentType annotation
214-
MappingCouchbaseConverter converter = new CustomMappingCouchbaseConverter(couchbaseMappingContext, typeKey());
215-
converter.setCustomConversions(couchbaseCustomConversions);
214+
MappingCouchbaseConverter converter = new CustomMappingCouchbaseConverter(couchbaseMappingContext, typeKey(), couchbaseCustomConversions);
216215
return converter;
217216
}
218217

src/test/java/org/springframework/data/couchbase/domain/CustomMappingCouchbaseConverter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.domain;
1717

18+
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
1819
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
1920
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
2021
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
@@ -37,4 +38,21 @@ public CustomMappingCouchbaseConverter(
3738
this.typeMapper = new TypeBasedCouchbaseTypeMapper(typeKey);
3839
}
3940

41+
/**
42+
* this constructer creates a TypeBasedCouchbaseTypeMapper with the specified couchbaseCustomConversions and typeKey
43+
* while MappingCouchbaseConverter uses a DefaultCouchbaseTypeMapper typeMapper = new DefaultCouchbaseTypeMapper(typeKey != null ? typeKey :
44+
* TYPEKEY_DEFAULT);
45+
*
46+
* @param mappingContext
47+
* @param typeKey - the typeKey to be used (normally "_class")
48+
* @param couchbaseCustomConversions - custom conversions to use
49+
*/
50+
public CustomMappingCouchbaseConverter(
51+
final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext,
52+
final String typeKey,
53+
final CouchbaseCustomConversions couchbaseCustomConversions) {
54+
super(mappingContext, typeKey, couchbaseCustomConversions);
55+
this.typeMapper = new TypeBasedCouchbaseTypeMapper(typeKey);
56+
}
57+
4058
}

src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
124124
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will
125125
// use the DocumentType annotation
126126
MappingCouchbaseConverter converter = new AbstractingMappingCouchbaseConverter(couchbaseMappingContext,
127-
typeKey());
128-
converter.setCustomConversions(couchbaseCustomConversions);
127+
typeKey(),
128+
couchbaseCustomConversions);
129129
return converter;
130130
}
131131

0 commit comments

Comments
 (0)