Skip to content

Commit f2dc772

Browse files
committed
Extra Pojo with nulls values in collections and maps fixes
JAVA-2756
1 parent 3813249 commit f2dc772

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

bson/src/main/org/bson/codecs/pojo/CollectionPropertyCodecProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ public Collection<T> decode(final BsonReader reader, final DecoderContext contex
6767
Collection<T> collection = getInstance();
6868
reader.readStartArray();
6969
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
70-
collection.add(codec.decode(reader, context));
70+
if (reader.getCurrentBsonType() == BsonType.NULL) {
71+
collection.add(null);
72+
reader.readNull();
73+
} else {
74+
collection.add(codec.decode(reader, context));
75+
}
7176
}
7277
reader.readEndArray();
7378
return collection;

bson/src/main/org/bson/codecs/pojo/MapPropertyCodecProvider.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public void encode(final BsonWriter writer, final Map<String, T> map, final Enco
5959
writer.writeStartDocument();
6060
for (final Entry<String, T> entry : map.entrySet()) {
6161
writer.writeName(entry.getKey());
62-
codec.encode(writer, entry.getValue(), encoderContext);
62+
if (entry.getValue() == null) {
63+
writer.writeNull();
64+
} else {
65+
codec.encode(writer, entry.getValue(), encoderContext);
66+
}
6367
}
6468
writer.writeEndDocument();
6569
}
@@ -69,7 +73,12 @@ public Map<String, T> decode(final BsonReader reader, final DecoderContext conte
6973
reader.readStartDocument();
7074
Map<String, T> map = getInstance();
7175
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
72-
map.put(reader.readName(), codec.decode(reader, context));
76+
if (reader.getCurrentBsonType() == BsonType.NULL) {
77+
map.put(reader.readName(), null);
78+
reader.readNull();
79+
} else {
80+
map.put(reader.readName(), codec.decode(reader, context));
81+
}
7382
}
7483
reader.readEndDocument();
7584
return map;

bson/src/test/unit/org/bson/codecs/pojo/PojoRoundTripTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ private static List<TestData> testCases() {
163163
"{'collection': [1, 2, 3], 'list': [4, 5, 6], 'linked': [7, 8, 9], 'map': {'A': 1.1, 'B': 2.2, 'C': 3.3},"
164164
+ "'concurrent': {'D': 4.4, 'E': 5.5, 'F': 6.6}}"));
165165

166+
data.add(new TestData("Handling of nulls inside collections", getConcreteCollectionsModelWithNulls(),
167+
getPojoCodecProviderBuilder(ConcreteCollectionsModel.class),
168+
"{'collection': [1, null, 3], 'list': [4, null, 6], 'linked': [null, 8, 9], 'map': {'A': 1.1, 'B': null, 'C': 3.3}}"));
169+
166170
data.add(new TestData("Concrete specific return collection type model through BsonCreator",
167171
new CollectionSpecificReturnTypeCreatorModel(Arrays.asList("foo", "bar")),
168172
getPojoCodecProviderBuilder(CollectionSpecificReturnTypeCreatorModel.class),

bson/src/test/unit/org/bson/codecs/pojo/PojoTestCase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ static ConcreteCollectionsModel getConcreteCollectionsModel() {
217217
return new ConcreteCollectionsModel(collection, list, linked, map, concurrent);
218218
}
219219

220+
221+
static ConcreteCollectionsModel getConcreteCollectionsModelWithNulls() {
222+
Collection<Integer> collection = asList(1, null, 3);
223+
List<Integer> list = asList(4, null, 6);
224+
LinkedList<Integer> linked = new LinkedList<Integer>(asList(null, 8, 9));
225+
Map<String, Double> map = new HashMap<String, Double>();
226+
map.put("A", 1.1);
227+
map.put("B", null);
228+
map.put("C", 3.3);
229+
230+
return new ConcreteCollectionsModel(collection, list, linked, map, null);
231+
}
232+
220233
static SimpleNestedPojoModel getSimpleNestedPojoModel() {
221234
SimpleModel simpleModel = getSimpleModel();
222235
return new SimpleNestedPojoModel(simpleModel);

0 commit comments

Comments
 (0)