Skip to content

Commit 1efc9fe

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 0bc50ec commit 1efc9fe

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
@@ -332,7 +332,7 @@ public static BsonValue simpleToBsonValue(Object source) {
332332
* @throws IllegalArgumentException if {@literal source} does not correspond to a {@link BsonValue} type.
333333
* @since 4.2
334334
*/
335-
@SuppressWarnings("unchecked")
335+
@SuppressWarnings({ "unchecked", "rawtypes" })
336336
public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegistry) {
337337

338338
if (source instanceof BsonValue bsonValue) {
@@ -376,7 +376,7 @@ public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegi
376376
}
377377

378378
if (source instanceof Date date) {
379-
new BsonDateTime(date.getTime());
379+
return new BsonDateTime(date.getTime());
380380
}
381381

382382
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)