Skip to content

Commit 4dc1b64

Browse files
committed
Adapt to change in MongoDB's BigDecimal and UUID default representations
See spring-projects/spring-data-mongodb#5037 See gh-47041
1 parent 0fc1a40 commit 4dc1b64

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

module/spring-boot-data-mongodb/src/main/java/org/springframework/boot/data/mongodb/autoconfigure/DataMongoProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ public static class Representation {
103103
/**
104104
* Representation to use when converting a BigDecimal.
105105
*/
106-
private BigDecimalRepresentation bigDecimal = BigDecimalRepresentation.DECIMAL128;
106+
private @Nullable BigDecimalRepresentation bigDecimal;
107107

108-
public BigDecimalRepresentation getBigDecimal() {
108+
public @Nullable BigDecimalRepresentation getBigDecimal() {
109109
return this.bigDecimal;
110110
}
111111

112-
public void setBigDecimal(BigDecimalRepresentation bigDecimal) {
112+
public void setBigDecimal(@Nullable BigDecimalRepresentation bigDecimal) {
113113
this.bigDecimal = bigDecimal;
114114
}
115115

module/spring-boot-data-mongodb/src/main/java/org/springframework/boot/data/mongodb/autoconfigure/MongoDataConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
3333
import org.springframework.data.mongodb.core.convert.MongoConverter;
3434
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
35+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.BigDecimalRepresentation;
3536
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
3637
import org.springframework.data.mongodb.core.mapping.Document;
3738
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
@@ -76,8 +77,12 @@ MongoMappingContext mongoMappingContext(MongoCustomConversions conversions, Mong
7677
@Bean
7778
@ConditionalOnMissingBean
7879
MongoCustomConversions mongoCustomConversions() {
79-
return MongoCustomConversions
80-
.create((configurer) -> configurer.bigDecimal(this.properties.getRepresentation().getBigDecimal()));
80+
return MongoCustomConversions.create((configurer) -> {
81+
BigDecimalRepresentation bigDecimaRepresentation = this.properties.getRepresentation().getBigDecimal();
82+
if (bigDecimaRepresentation != null) {
83+
configurer.bigDecimal(bigDecimaRepresentation);
84+
}
85+
});
8186
}
8287

8388
@Bean

module/spring-boot-data-mongodb/src/test/java/org/springframework/boot/data/mongodb/autoconfigure/MongoDataAutoConfigurationTests.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ void customConversions() {
119119
}
120120

121121
@Test
122-
void customBigDecimalRepresentation() {
122+
@Deprecated(since = "4.0.0")
123+
void customBigDecimalDeprecatedRepresentation() {
123124
this.contextRunner.withPropertyValues("spring.data.mongodb.representation.big-decimal=string")
124125
.run((context) -> assertThat(context.getBean(MongoCustomConversions.class)).extracting("converters")
125126
.asInstanceOf(InstanceOfAssertFactories.LIST)
@@ -128,14 +129,25 @@ void customBigDecimalRepresentation() {
128129
.anySatisfy((className) -> assertThat(className).contains("BigIntegerToStringConverter")));
129130
}
130131

132+
@Test
133+
void customBigDecimalRepresentation() {
134+
this.contextRunner.withPropertyValues("spring.data.mongodb.representation.big-decimal=decimal128")
135+
.run((context) -> assertThat(context.getBean(MongoCustomConversions.class)).extracting("converters")
136+
.asInstanceOf(InstanceOfAssertFactories.LIST)
137+
.map((converter) -> converter.getClass().getName())
138+
.anySatisfy((className) -> assertThat(className).contains("BigDecimalToDecimal128Converter"))
139+
.anySatisfy((className) -> assertThat(className).contains("BigIntegerToDecimal128Converter")));
140+
}
141+
131142
@Test
132143
void defaultBigDecimalRepresentation() {
133144
this.contextRunner
134145
.run((context) -> assertThat(context.getBean(MongoCustomConversions.class)).extracting("converters")
135146
.asInstanceOf(InstanceOfAssertFactories.LIST)
136147
.map((converter) -> converter.getClass().getName())
137-
.noneSatisfy((className) -> assertThat(className).contains("BigDecimalToStringConverter"))
138-
.noneSatisfy((className) -> assertThat(className).contains("BigIntegerToStringConverter")));
148+
.filteredOn((className) -> className.startsWith("org.springframework.data.mongodb"))
149+
.noneSatisfy((className) -> assertThat(className).contains("BigDecimalTo"))
150+
.noneSatisfy((className) -> assertThat(className).contains("BigIntegerTo")));
139151
}
140152

141153
@Test

module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static class Representation {
212212
/**
213213
* Representation to use when converting a UUID to a BSON binary value.
214214
*/
215-
private UuidRepresentation uuid = UuidRepresentation.STANDARD;
215+
private UuidRepresentation uuid = UuidRepresentation.UNSPECIFIED;
216216

217217
public UuidRepresentation getUuid() {
218218
return this.uuid;

0 commit comments

Comments
 (0)