Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5203

Record with nullable type throw BsonInvalidOperationException when value is explicitly null in the DB

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.11.1
    • Affects Version/s: None
    • Component/s: Codecs
    • Labels:
      None
    • Minor Change
    • Not Needed
    • Hide

      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?

      Show
      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?

      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.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            gordonw@duck.com Gordon Wang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: