[JAVA-3567] Support Java record types Created: 20/Dec/19  Updated: 28/Oct/23  Resolved: 12/Apr/22

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

Type: New Feature Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Ross Lawley
Resolution: Fixed Votes: 9
Labels: QP-priority, size-medium
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to JAVA-4440 Add codec and codec provider for enum... Closed
is related to JAVA-4622 Support existing pojo annotations for... Closed
Quarter: FY22Q4, FY23Q1

 Description   

Java is adding support for records. See https://openjdk.java.net/jeps/359 for a description. The feature will be in preview in Java 14 and if all goes well will be added to the language.

Update: a second preview will be included in Java 15 and is described in https://openjdk.java.net/jeps/384.

The PojoCodecProvider does not support records because records don't follow Java Beans conventions. Rather than generating getter methods that are prefixed with "get", the getter names are identical to the property names, e.g. a record like

public record Animal(@BsonId ObjectId id, @BsonProperty("name") String name, @BsonProperty("age") int age) {
}

will generate a JVM class like:

public class Animal extends java.lang.Record {
    @BsonId 
    private final ObjectId id
    @BsonProperty("name")
    private final String name;
    @BsonProperty("age") i
    private final int age;
 
    public Animal(@BsonId ObjectId id, @BsonProperty("name") String name, @BsonProperty("age") int age) {
        this.objectId = objectId;
        this.name = name;
        this.age = age;
    }
 
    @BsonId 
    public ObjectId id() {
        return id;
    }
 
    @BsonProperty("name")
    public String name() {
        return name;
    }
 
    @BsonProperty("age")
    public int age() {
       return age;
    }
 
    // hashCode, equals, toString



 Comments   
Comment by Githook User [ 12/Apr/22 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Add Java record support to the default registry

Add dependency in core to bson-record-codec

Wrap the RecordCodecProvider with a canary check so support is
only added if the JDK version supports it.

JAVA-3567
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/ba1cf0ba2fbabcf3b4eb46643860e863a83fe822

Comment by Githook User [ 08/Apr/22 ]

Author:

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

Message: Fix up @since and remove TODO

JAVA-3567
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/49491e53c273dbf1293fe9c9ddf4260269f1e607

Comment by Githook User [ 08/Apr/22 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Support codec for Java records

JAVA-3567
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/346a5e0e66b2f20aa6e9bc97dcb60331e91eea10

Comment by Jeffrey Yemin [ 20/Apr/21 ]

Hi chen@spotible.com, not yet.  We still have some issues to work out with our build and release process in order to be able to use Java 16.

Comment by Chen Levy [ 20/Apr/21 ]

Is this included in 4.3.0-SNAPSHOT ?

Comment by Jeffrey Yemin [ 16/Mar/21 ]

Java 16 was released today! Moving this from Blocked back to Scheduled.

Comment by Brian DeLeonardis (Inactive) [ 08/Oct/20 ]

An implementation for this is on the 3567 branch. We will merge it once Java 16 is released.

Generated at Thu Feb 08 08:59:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.