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 98795cb

Browse files
Convert BsonUndefined to null value.
Register a reading converter that returns null when attempting to read a value of type BsonUndefined. Prior to this change users faced a ConverterNotFoundException when source documents contained BsonUndefined. Resolves: #2350
1 parent fa63efc commit 98795cb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.bson.BsonReader;
3535
import org.bson.BsonTimestamp;
36+
import org.bson.BsonUndefined;
3637
import org.bson.BsonWriter;
3738
import org.bson.Document;
3839
import org.bson.codecs.Codec;
@@ -104,6 +105,7 @@ static Collection<Object> getConvertersToRegister() {
104105
converters.add(BinaryToByteArrayConverter.INSTANCE);
105106
converters.add(BsonTimestampToInstantConverter.INSTANCE);
106107

108+
converters.add(reading(BsonUndefined.class, Object.class, it -> null));
107109
converters.add(reading(String.class, URI.class, URI::create).andWriting(URI::toString));
108110

109111
return converters;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.time.temporal.ChronoUnit;
3535
import java.util.*;
3636

37+
import org.bson.BsonUndefined;
3738
import org.bson.types.Binary;
3839
import org.bson.types.Code;
3940
import org.bson.types.Decimal128;
@@ -2843,6 +2844,13 @@ void shouldConvertTypesToStringTargetType() {
28432844
assertThat(converter.read(Address.class, source).city).isEqualTo("Gotham,Metropolis");
28442845
}
28452846

2847+
@Test // GH-2350
2848+
void shouldConvertBsonUndefinedToNull() {
2849+
2850+
org.bson.Document source = new org.bson.Document("s", "hallway drive").append("city", new BsonUndefined());
2851+
assertThat(converter.read(Address.class, source).city).isNull();
2852+
}
2853+
28462854
static class GenericType<T> {
28472855
T content;
28482856
}

0 commit comments

Comments
(0)

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