Duplicate field name serialisation with @BsonDiscriminator and getter

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Minor - P4
    • 5.6.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • 🔵 Done
    • Java Drivers
    • 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?
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      When the driver serialise a POJO annotated with @BsonDiscriminator and a getter with the readName as the discriminator key it will serialise the field two times.

      I tried with the mongo-java-driver 5.2.1.

      How to Reproduce

      Run the following code:

      public class DuplicateKeySerialization {
          public static void main(final String[] args) {
              try (
                      var client = MongoClients.create(
                              MongoClientSettings.builder()
                                      .applyConnectionString(
                                              new ConnectionString("<REDACTED>")
                                      )
                                      .serverApi(ServerApi.builder().version(ServerApiVersion.V1).build())
                                      .codecRegistry(fromRegistries(
                                                      getDefaultCodecRegistry(),
                                                      fromProviders(PojoCodecProvider.builder().automatic(true).build())
                                              )
                                      )
                                      .build()
                      );
              ) {
                  client.getDatabase("test").getCollection("books", Book.class).insertOne(new Book());
              }
          }
      
          @BsonDiscriminator(key = "type", value = "BOOK")
          public static class Book {
      
              public String getType() {
                  return "BOOK";
              }
          }
      }
      

      Then, run the following aggregation pipeline:

      [
        {
          $project: {
            fields: {
              $objectToArray: "$$ROOT",
            },
          },
        },
      ]
      

      The output of the above pipeline is:

      {
        "_id": {
          "$oid": "67893f123f0ca25a374c252a"
        },
        "fields": [
          {
            "k": "_id",
            "v": {
              "$oid": "67893f123f0ca25a374c252a"
            }
          },
          {
            "k": "type",
            "v": "BOOK"
          },
          {
            "k": "type",
            "v": "BOOK"
          }
        ]
      }
      

      This will cause unexpected behaviour (https://www.mongodb.com/docs/manual/core/document/#field-names) like bad projections.

            Assignee:
            Ross Lawley
            Reporter:
            Raffaele Florio
            Ross Lawley
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: