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

Duplicate field name serialisation with @BsonDiscriminator and getter

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Java Drivers
    • Not Needed
    • 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:
            Unassigned Unassigned
            Reporter:
            raffaeleflorio@protonmail.com Raffaele Florio
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: