Browse Source

Return early for Date to BsonDateTime conversion

Add missing return statement to return value early instead of falling back to Codec based conversion.

Closes: #5072
4.4.x
Christoph Strobl 2 months ago
parent
commit
1efc9fe04e
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java
  2. 8
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/util/json/BsonUtilsTest.java

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

@ -332,7 +332,7 @@ public class BsonUtils { @@ -332,7 +332,7 @@ public class BsonUtils {
* @throws IllegalArgumentException if {@literal source} does not correspond to a {@link BsonValue} type.
* @since 4.2
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegistry) {
if (source instanceof BsonValue bsonValue) {
@ -376,7 +376,7 @@ public class BsonUtils { @@ -376,7 +376,7 @@ public class BsonUtils {
}
if (source instanceof Date date) {
new BsonDateTime(date.getTime());
return new BsonDateTime(date.getTime());
}
try {

8
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/util/json/BsonUtilsTest.java

@ -33,6 +33,7 @@ import java.util.Map.Entry; @@ -33,6 +33,7 @@ import java.util.Map.Entry;
import java.util.stream.Stream;
import org.bson.BsonArray;
import org.bson.BsonDateTime;
import org.bson.BsonDouble;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
@ -150,6 +151,13 @@ class BsonUtilsTest { @@ -150,6 +151,13 @@ class BsonUtilsTest {
.isEqualTo(new Document("value", source).toBsonDocument().get("value"));
}
@Test // GH-5072
void convertsJavaDateToBsonDateTime() {
Date source = new Date();
assertThat(BsonUtils.simpleToBsonValue(source)).isEqualTo(new BsonDateTime(source.getTime()));
}
@ParameterizedTest // GH-4432
@MethodSource("collectionLikeInstances")
void convertsCollectionLikeToBsonArray(Object source) {

Loading…
Cancel
Save