Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dfe16bd

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 a18556f commit dfe16bd

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
@@ -344,7 +344,7 @@ public static BsonValue simpleToBsonValue(@Nullable Object source) {
344344
* @throws IllegalArgumentException if {@literal source} does not correspond to a {@link BsonValue} type.
345345
* @since 4.2
346346
*/
347-
@SuppressWarnings("unchecked")
347+
@SuppressWarnings({ "unchecked", "rawtypes" })
348348
@Contract("null, _ -> !null")
349349
public static BsonValue simpleToBsonValue(@Nullable Object source, CodecRegistry codecRegistry) {
350350

@@ -393,7 +393,7 @@ public static BsonValue simpleToBsonValue(@Nullable Object source, CodecRegistry
393393
}
394394

395395
if (source instanceof Date date) {
396-
new BsonDateTime(date.getTime());
396+
returnnew BsonDateTime(date.getTime());
397397
}
398398

399399
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;
@@ -151,6 +152,13 @@ void convertsJavaTimeTypesToBsonDateTime(Temporal source) {
151152
.isEqualTo(new Document("value", source).toBsonDocument().get("value"));
152153
}
153154

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /