Skip to content

Commit 4a43f6c

Browse files
Add UNSPECIFIED to BigDecimalRepresentation.
Closes: #5054
1 parent 9d3c914 commit 4a43f6c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static class MongoConverterConfigurationAdapter {
175175
LocalDateTime.class);
176176

177177
private boolean useNativeDriverJavaTimeCodecs = false;
178-
private @Nullable BigDecimalRepresentation bigDecimals;
178+
private BigDecimalRepresentation bigDecimals = BigDecimalRepresentation.UNSPECIFIED;
179179
private final List<Object> customConverters = new ArrayList<>();
180180

181181
private final PropertyValueConversions internalValueConversion = PropertyValueConversions.simple(it -> {});
@@ -476,7 +476,13 @@ public enum BigDecimalRepresentation {
476476
/**
477477
* Store numbers using {@link org.bson.types.Decimal128} (default). Requires MongoDB Server 3.4 or later.
478478
*/
479-
DECIMAL128
479+
DECIMAL128,
480+
481+
/**
482+
* Pass on values to the MongoDB Java Driver without any prior conversion.
483+
* @since 5.0
484+
*/
485+
UNSPECIFIED
480486

481487
}
482488

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,7 @@ void shouldApplyExplicitDecimal128Conversion(BigDecimalRepresentation representa
22392239
static Stream<Arguments> representations() {
22402240

22412241
return Stream.of(Arguments.argumentSet("None (default)", new Object[] { null }), //
2242+
Arguments.argumentSet("UNSPECIFIED", BigDecimalRepresentation.UNSPECIFIED), //
22422243
Arguments.argumentSet("STRING", BigDecimalRepresentation.STRING), //
22432244
Arguments.argumentSet("DECIMAL128", BigDecimalRepresentation.DECIMAL128));
22442245
}
@@ -2260,6 +2261,23 @@ void shouldWriteBigNumbersAsIsWithoutConfiguration() {
22602261
assertThat(target.get("bigDecimal")).isEqualTo(source.bigDecimal);
22612262
}
22622263

2264+
@Test // GH-5037, GH-5054
2265+
void shouldWriteBigNumbersAsIsWhenUsingUnspecified() {
2266+
2267+
converter = createConverter(BigDecimalRepresentation.UNSPECIFIED);
2268+
2269+
WithoutExplicitTargetTypes source = new WithoutExplicitTargetTypes();
2270+
source.bigInteger = BigInteger.TWO;
2271+
source.bigDecimal = new BigDecimal("123.456");
2272+
2273+
org.bson.Document target = new org.bson.Document();
2274+
2275+
converter.write(source, target);
2276+
2277+
assertThat(target.get("bigInteger")).isEqualTo(source.bigInteger);
2278+
assertThat(target.get("bigDecimal")).isEqualTo(source.bigDecimal);
2279+
}
2280+
22632281
@Test // GH-5037
22642282
void shouldReadTypedBigNumbersFromDecimal128() {
22652283

0 commit comments

Comments
 (0)