Check for for null collection elements in POJO CollectionCodec before deferring to codec.encode.
This fixes an NPE when a POJO has a nested collection element and one of the nested collection values is null.
@@ -53,8 +53,12 @@ final class CollectionPropertyCodecProvider implements PropertyCodecProvider {
public void encode(final BsonWriter writer, final Collection<T> collection, final EncoderContext encoderContext) {
writer.writeStartArray();
for (final T value : collection) {
+ if (value == null) {
+ writer.writeNull();
+ } else {
codec.encode(writer, value, encoderContext);
}
+ }
writer.writeEndArray();
}
