Adapt to change in MongoDB's BigDecimal and UUID default representations

See https://github.com/spring-projects/spring-data-mongodb/issues/5037
See gh-47041
This commit is contained in:
Stéphane Nicoll
2025-09-09 16:25:26 +02:00
parent 0fc1a40f3b
commit 4dc1b644fe
4 changed files with 26 additions and 9 deletions
@@ -103,13 +103,13 @@ public class DataMongoProperties {
/**
* Representation to use when converting a BigDecimal.
*/
private BigDecimalRepresentation bigDecimal = BigDecimalRepresentation.DECIMAL128;
private @Nullable BigDecimalRepresentation bigDecimal;
public BigDecimalRepresentation getBigDecimal() {
public @Nullable BigDecimalRepresentation getBigDecimal() {
return this.bigDecimal;
}
public void setBigDecimal(BigDecimalRepresentation bigDecimal) {
public void setBigDecimal(@Nullable BigDecimalRepresentation bigDecimal) {
this.bigDecimal = bigDecimal;
}
@@ -32,6 +32,7 @@ import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.BigDecimalRepresentation;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
@@ -76,8 +77,12 @@ class MongoDataConfiguration {
@Bean
@ConditionalOnMissingBean
MongoCustomConversions mongoCustomConversions() {
return MongoCustomConversions
.create((configurer) -> configurer.bigDecimal(this.properties.getRepresentation().getBigDecimal()));
return MongoCustomConversions.create((configurer) -> {
BigDecimalRepresentation bigDecimaRepresentation = this.properties.getRepresentation().getBigDecimal();
if (bigDecimaRepresentation != null) {
configurer.bigDecimal(bigDecimaRepresentation);
}
});
}
@Bean
@@ -119,7 +119,8 @@ class MongoDataAutoConfigurationTests {
}
@Test
void customBigDecimalRepresentation() {
@Deprecated(since = "4.0.0")
void customBigDecimalDeprecatedRepresentation() {
this.contextRunner.withPropertyValues("spring.data.mongodb.representation.big-decimal=string")
.run((context) -> assertThat(context.getBean(MongoCustomConversions.class)).extracting("converters")
.asInstanceOf(InstanceOfAssertFactories.LIST)
@@ -128,14 +129,25 @@ class MongoDataAutoConfigurationTests {
.anySatisfy((className) -> assertThat(className).contains("BigIntegerToStringConverter")));
}
@Test
void customBigDecimalRepresentation() {
this.contextRunner.withPropertyValues("spring.data.mongodb.representation.big-decimal=decimal128")
.run((context) -> assertThat(context.getBean(MongoCustomConversions.class)).extracting("converters")
.asInstanceOf(InstanceOfAssertFactories.LIST)
.map((converter) -> converter.getClass().getName())
.anySatisfy((className) -> assertThat(className).contains("BigDecimalToDecimal128Converter"))
.anySatisfy((className) -> assertThat(className).contains("BigIntegerToDecimal128Converter")));
}
@Test
void defaultBigDecimalRepresentation() {
this.contextRunner
.run((context) -> assertThat(context.getBean(MongoCustomConversions.class)).extracting("converters")
.asInstanceOf(InstanceOfAssertFactories.LIST)
.map((converter) -> converter.getClass().getName())
.noneSatisfy((className) -> assertThat(className).contains("BigDecimalToStringConverter"))
.noneSatisfy((className) -> assertThat(className).contains("BigIntegerToStringConverter")));
.filteredOn((className) -> className.startsWith("org.springframework.data.mongodb"))
.noneSatisfy((className) -> assertThat(className).contains("BigDecimalTo"))
.noneSatisfy((className) -> assertThat(className).contains("BigIntegerTo")));
}
@Test
@@ -212,7 +212,7 @@ public class MongoProperties {
/**
* Representation to use when converting a UUID to a BSON binary value.
*/
private UuidRepresentation uuid = UuidRepresentation.STANDARD;
private UuidRepresentation uuid = UuidRepresentation.UNSPECIFIED;
public UuidRepresentation getUuid() {
return this.uuid;