[JAVA-5203] Record with nullable type throw BsonInvalidOperationException when value is explicitly null in the DB Created: 16/Oct/23  Updated: 04/Nov/23  Resolved: 23/Oct/23

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: None
Fix Version/s: 4.11.1

Type: Bug Priority: Major - P3
Reporter: Gordon Wang Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

This is the exact same issue that has been raised in JAVA-5134 for Kotlin driver, but affecting Java record codecs, which when decoding a nullable field when persisted field exists and contains null value (BSONType is NULL), the Java record codec could not handle the scenario correctly and will thrown a BsonInvalidOperationException.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Affected versions of Java driver: 4.11

How to Reproduce

Below test case reproduces the issue, included in https://github.com/mongodb/mongo-java-driver/pull/1223:

// Sample record
 
import javax.annotation.Nullable;
 
public record TestRecordWithNullableAnnotation (@BsonId ObjectId id, @Nullable String name) {
} 

// Sample test
@Test
public void testRecordWithStoredNulls() {
    var codec = createRecordCodec(TestRecordWithNullableAnnotation.class, Bson.DEFAULT_CODEC_REGISTRY);
    var identifier = new ObjectId();
    var testRecord = new TestRecordWithNullableAnnotation(identifier, null);
 
    var document = new BsonDocument("_id", new BsonObjectId(identifier))
            .append("name", new BsonNull());
 
    // when
    var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());
 
    // then
    assertEquals(testRecord, decoded);
} 

Additional Background

I have already made a patch to fix this at https://github.com/mongodb/mongo-java-driver/pull/1223, which contains a fix in Java record codec and also a test verifying the patch is working.



 Comments   
Comment by Jeffrey Yemin [ 26/Oct/23 ]

commit 81ad6a1c247bac3298ad4de60c564435db01ef06 (HEAD -> 4.11.x, upstream/4.11.x)
Author: Gordon Wang <gordonw@canva.com>
Date: Mon Oct 23 22:46:55 2023 +1100

Support decoding nulls for non-primitive fields in Java records (#1223)

JAVA-5203

Comment by Jeffrey Yemin [ 23/Oct/23 ]

Thanks gordonw@duck.com.

This will be included in the 4.11.1 patch release.

Comment by Githook User [ 23/Oct/23 ]

Author:

{'name': 'Gordon Wang', 'email': 'gordonw@canva.com', 'username': 'bulbfreeman'}

Message: Support decoding nulls for non-primitive fields in Java records (#1223)

JAVA-5203
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/2e12e71dd8e8c9d2505e3ff8afb4f3312e392291

Comment by PM Bot [ 16/Oct/23 ]

Hi gordonw@duck.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:04:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.