Skip to content

Commit d51e749

Browse files
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
1 parent 21422f4 commit d51e749

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public static BsonValue simpleToBsonValue(Object source) {
340340
* @throws IllegalArgumentException if {@literal source} does not correspond to a {@link BsonValue} type.
341341
* @since 4.2
342342
*/
343-
@SuppressWarnings("unchecked")
343+
@SuppressWarnings({ "unchecked", "rawtypes" })
344344
public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegistry) {
345345

346346
if (source instanceof BsonValue bsonValue) {
@@ -384,7 +384,7 @@ public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegi
384384
}
385385

386386
if (source instanceof Date date) {
387-
new BsonDateTime(date.getTime());
387+
return new BsonDateTime(date.getTime());
388388
}
389389

390390
try {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Stream;
3434

3535
import org.bson.BsonArray;
36+
import org.bson.BsonDateTime;
3637
import org.bson.BsonDouble;
3738
import org.bson.BsonInt32;
3839
import org.bson.BsonInt64;
@@ -150,6 +151,13 @@ void convertsJavaTimeTypesToBsonDateTime(Temporal source) {
150151
.isEqualTo(new Document("value", source).toBsonDocument().get("value"));
151152
}
152153

154+
@Test // GH-5072
155+
void convertsJavaDateToBsonDateTime() {
156+
157+
Date source = new Date();
158+
assertThat(BsonUtils.simpleToBsonValue(source)).isEqualTo(new BsonDateTime(source.getTime()));
159+
}
160+
153161
@ParameterizedTest // GH-4432
154162
@MethodSource("collectionLikeInstances")
155163
void convertsCollectionLikeToBsonArray(Object source) {

0 commit comments

Comments
 (0)