Browse Source

Add UNSPECIFIED to BigDecimalRepresentation.

Closes: #5054
pull/5008/merge
Christoph Strobl 3 months ago
parent
commit
4a43f6c59e
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java
  2. 18
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

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

@ -175,7 +175,7 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
LocalDateTime.class); LocalDateTime.class);
private boolean useNativeDriverJavaTimeCodecs = false; private boolean useNativeDriverJavaTimeCodecs = false;
private @Nullable BigDecimalRepresentation bigDecimals; private BigDecimalRepresentation bigDecimals = BigDecimalRepresentation.UNSPECIFIED;
private final List<Object> customConverters = new ArrayList<>(); private final List<Object> customConverters = new ArrayList<>();
private final PropertyValueConversions internalValueConversion = PropertyValueConversions.simple(it -> {}); private final PropertyValueConversions internalValueConversion = PropertyValueConversions.simple(it -> {});
@ -476,7 +476,13 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
/** /**
* Store numbers using {@link org.bson.types.Decimal128} (default). Requires MongoDB Server 3.4 or later. * Store numbers using {@link org.bson.types.Decimal128} (default). Requires MongoDB Server 3.4 or later.
*/ */
DECIMAL128 DECIMAL128,
/**
* Pass on values to the MongoDB Java Driver without any prior conversion.
* @since 5.0
*/
UNSPECIFIED
} }

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

@ -2239,6 +2239,7 @@ class MappingMongoConverterUnitTests {
static Stream<Arguments> representations() { static Stream<Arguments> representations() {
return Stream.of(Arguments.argumentSet("None (default)", new Object[] { null }), // return Stream.of(Arguments.argumentSet("None (default)", new Object[] { null }), //
Arguments.argumentSet("UNSPECIFIED", BigDecimalRepresentation.UNSPECIFIED), //
Arguments.argumentSet("STRING", BigDecimalRepresentation.STRING), // Arguments.argumentSet("STRING", BigDecimalRepresentation.STRING), //
Arguments.argumentSet("DECIMAL128", BigDecimalRepresentation.DECIMAL128)); Arguments.argumentSet("DECIMAL128", BigDecimalRepresentation.DECIMAL128));
} }
@ -2260,6 +2261,23 @@ class MappingMongoConverterUnitTests {
assertThat(target.get("bigDecimal")).isEqualTo(source.bigDecimal); assertThat(target.get("bigDecimal")).isEqualTo(source.bigDecimal);
} }
@Test // GH-5037, GH-5054
void shouldWriteBigNumbersAsIsWhenUsingUnspecified() {
converter = createConverter(BigDecimalRepresentation.UNSPECIFIED);
WithoutExplicitTargetTypes source = new WithoutExplicitTargetTypes();
source.bigInteger = BigInteger.TWO;
source.bigDecimal = new BigDecimal("123.456");
org.bson.Document target = new org.bson.Document();
converter.write(source, target);
assertThat(target.get("bigInteger")).isEqualTo(source.bigInteger);
assertThat(target.get("bigDecimal")).isEqualTo(source.bigDecimal);
}
@Test // GH-5037 @Test // GH-5037
void shouldReadTypedBigNumbersFromDecimal128() { void shouldReadTypedBigNumbersFromDecimal128() {

Loading…
Cancel
Save