[JAVA-4667] Support generic List and Map with POJO/record elements for records components Created: 05/Jul/22  Updated: 28/Oct/23  Resolved: 27/Sep/22

Status: Closed
Project: Java Driver
Component/s: POJO
Affects Version/s: 4.6.0
Fix Version/s: 4.8.0

Type: Improvement Priority: Major - P3
Reporter: Valentin Kavalenka Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
Related
is related to JAVA-4740 Support deserialization of records wi... Closed
Backwards Compatibility: Minor Change
Documentation Changes: Needed

 Description   

RecordCodec was introduced in 4.6.0 (see JAVA-3567). Currently, it decodes List or Map of POJO/record elements as if they contain Document values. This behavior differs from that of the PojoCodec.



 Comments   
Comment by Githook User [ 27/Sep/22 ]

Author:

{'name': 'Jeff Yemin', 'email': 'jeff.yemin@mongodb.com', 'username': 'jyemin'}

Message: Support List<T> and Map<String, T> components in Java records

This enhances the support for any record component that is a
generic List or Map where the generic type of the List or Map
is a record or POJO. It used to incorrectly decode to
org.bson.Document. Now it respects the generic type of the
List or Map when decoding.

JAVA-4667
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/fc342ff69b70a0947ae10cc7074f833d7e7c0618

Comment by Jeffrey Yemin [ 23/Sep/22 ]

SImple repro:

public class JAVA4667 {     
 
     public record Employee(@BsonId ObjectId id, List<Address> addresses) {}     
     public record Address(String addressId, String city, String state) {}         
 
     public static void main(String[] args) {
        var mongoClient = MongoClients.create();
 
        var database = mongoClient.getDatabase("test");
        var collection = database.getCollection("employee", Employee.class);
 
        collection.drop();
 
        collection.insertOne(new Employee(new ObjectId("62ad33d33fde2c00f75e0bc2"),
                List.of(new Address("1", "NYC", "NY"))));
 
        var employee = collection.find().first();
        System.out.println(employee);
        System.out.println(employee.addresses.get(0).getClass());
    }
}

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