diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataProperties.java index 04435708289..f7930803b8c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.data.couchbase; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.data.couchbase.core.convert.DefaultCouchbaseTypeMapper; import org.springframework.data.couchbase.core.query.Consistency; /** @@ -39,6 +40,12 @@ public class CouchbaseDataProperties { */ private Consistency consistency = Consistency.READ_YOUR_OWN_WRITES; + /** + * Name of the field that stores the type information for complex types when using + * "MappingCouchbaseConverter". + */ + private String typeKey = DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY; + public boolean isAutoIndex() { return this.autoIndex; } @@ -55,4 +62,12 @@ public class CouchbaseDataProperties { this.consistency = consistency; } + public String getTypeKey() { + return this.typeKey; + } + + public void setTypeKey(String typeKey) { + this.typeKey = typeKey; + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/SpringBootCouchbaseDataConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/SpringBootCouchbaseDataConfiguration.java index 1c1b1294255..b6b4dd48dbb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/SpringBootCouchbaseDataConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/SpringBootCouchbaseDataConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,11 @@ class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfigur return new EntityScanner(this.applicationContext).scan(Document.class, Persistent.class); } + @Override + public String typeKey() { + return this.properties.getTypeKey(); + } + @Override @ConditionalOnMissingBean(name = BeanNames.COUCHBASE_TEMPLATE) @Bean(name = BeanNames.COUCHBASE_TEMPLATE) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java index 93f420cfe55..19305d0f173 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,7 @@ import org.springframework.data.couchbase.config.BeanNames; import org.springframework.data.couchbase.config.CouchbaseConfigurer; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions; +import org.springframework.data.couchbase.core.convert.DefaultCouchbaseTypeMapper; import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext; import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; import org.springframework.data.couchbase.core.query.Consistency; @@ -119,6 +120,19 @@ class CouchbaseDataAutoConfigurationTests { assertThat(initialEntitySet).containsOnly(City.class); } + @Test + void typeKeyDefault() { + load(CouchbaseTestConfigurer.class); + assertThat(this.context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()) + .isEqualTo(DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY); + } + + @Test + void typeKeyCanBeCustomized() { + load(CouchbaseTestConfigurer.class, "spring.data.couchbase.type-key=_custom"); + assertThat(this.context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()).isEqualTo("_custom"); + } + @Test void customConversions() { load(CustomConversionsConfig.class); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataPropertiesTests.java new file mode 100644 index 00000000000..3a26c7c9229 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataPropertiesTests.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.data.couchbase; + +import org.junit.jupiter.api.Test; + +import org.springframework.data.couchbase.config.CouchbaseConfigurationSupport; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CouchbaseDataProperties}. + * + * @author Stephane Nicoll + */ +class CouchbaseDataPropertiesTests { + + @Test + void typeKeyHasConsistentDefault() { + assertThat(new CouchbaseDataProperties().getTypeKey()).isEqualTo(new CouchbaseConfigurationSupport().typeKey()); + } + +}