-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: None
-
None
-
🔵 Done
-
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.
- is related to
-
JAVA-5971 @BsonIgnore throwing NPEs with 5.6.0
-
- Closed
-