|
22 | 22 | import org.bson.BsonDocumentWriter;
|
23 | 23 | import org.bson.BsonDouble;
|
24 | 24 | import org.bson.BsonInt32;
|
| 25 | +import org.bson.BsonInvalidOperationException; |
| 26 | +import org.bson.BsonNull; |
25 | 27 | import org.bson.BsonObjectId;
|
26 | 28 | import org.bson.BsonString;
|
27 | 29 | import org.bson.codecs.DecoderContext;
|
|
49 | 51 | import org.bson.codecs.record.samples.TestRecordWithMapOfRecords;
|
50 | 52 | import org.bson.codecs.record.samples.TestRecordWithNestedParameterized;
|
51 | 53 | import org.bson.codecs.record.samples.TestRecordWithNestedParameterizedRecord;
|
| 54 | +import org.bson.codecs.record.samples.TestRecordWithNullableField; |
52 | 55 | import org.bson.codecs.record.samples.TestRecordWithParameterizedRecord;
|
53 | 56 | import org.bson.codecs.record.samples.TestRecordWithPojoAnnotations;
|
54 | 57 | import org.bson.codecs.record.samples.TestSelfReferentialHolderRecord;
|
@@ -325,6 +328,35 @@ public void testRecordWithNulls() {
|
325 | 328 | assertEquals(testRecord, decoded);
|
326 | 329 | }
|
327 | 330 |
|
| 331 | + @Test |
| 332 | + public void testRecordWithStoredNulls() { |
| 333 | + var codec = createRecordCodec(TestRecordWithNullableField.class, Bson.DEFAULT_CODEC_REGISTRY); |
| 334 | + var identifier = new ObjectId(); |
| 335 | + var testRecord = new TestRecordWithNullableField(identifier, null, 42); |
| 336 | + |
| 337 | + var document = new BsonDocument("_id", new BsonObjectId(identifier)) |
| 338 | + .append("name", new BsonNull()) |
| 339 | + .append("age", new BsonInt32(42)); |
| 340 | + |
| 341 | + // when |
| 342 | + var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build()); |
| 343 | + |
| 344 | + // then |
| 345 | + assertEquals(testRecord, decoded); |
| 346 | + } |
| 347 | + |
| 348 | + @Test |
| 349 | + public void testExceptionsWithStoredNullsOnPrimitiveField() { |
| 350 | + var codec = createRecordCodec(TestRecordWithNullableField.class, Bson.DEFAULT_CODEC_REGISTRY); |
| 351 | + |
| 352 | + var document = new BsonDocument("_id", new BsonObjectId(new ObjectId())) |
| 353 | + .append("name", new BsonString("Felix")) |
| 354 | + .append("age", new BsonNull()); |
| 355 | + |
| 356 | + assertThrows(BsonInvalidOperationException.class, () -> |
| 357 | + codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build())); |
| 358 | + } |
| 359 | + |
328 | 360 | @Test
|
329 | 361 | public void testRecordWithExtraData() {
|
330 | 362 | var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
|
|
0 commit comments