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
This commit is contained in:
@@ -33,6 +33,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.bson.BsonReader;
|
||||
import org.bson.BsonTimestamp;
|
||||
import org.bson.BsonUndefined;
|
||||
import org.bson.BsonWriter;
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.Codec;
|
||||
@@ -104,6 +105,7 @@ abstract class MongoConverters {
|
||||
converters.add(BinaryToByteArrayConverter.INSTANCE);
|
||||
converters.add(BsonTimestampToInstantConverter.INSTANCE);
|
||||
|
||||
converters.add(reading(BsonUndefined.class, Object.class, it -> null));
|
||||
converters.add(reading(String.class, URI.class, URI::create).andWriting(URI::toString));
|
||||
|
||||
return converters;
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
|
||||
import org.bson.BsonUndefined;
|
||||
import org.bson.types.Binary;
|
||||
import org.bson.types.Code;
|
||||
import org.bson.types.Decimal128;
|
||||
@@ -2843,6 +2844,13 @@ class MappingMongoConverterUnitTests {
|
||||
assertThat(converter.read(Address.class, source).city).isEqualTo("Gotham,Metropolis");
|
||||
}
|
||||
|
||||
@Test // GH-2350
|
||||
void shouldConvertBsonUndefinedToNull() {
|
||||
|
||||
org.bson.Document source = new org.bson.Document("s", "hallway drive").append("city", new BsonUndefined());
|
||||
assertThat(converter.read(Address.class, source).city).isNull();
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user