[JAVA-3914] Event SET_PRIVATE_FIELDS_CONVENTION enabled, the field need an public getter to be set Created: 18/Dec/20  Updated: 27/Oct/23  Resolved: 04/Jan/21

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

Type: Task Priority: Major - P3
Reporter: dorian rodriguez Assignee: Jeffrey Yemin
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

Hello,

I try to play with pojo and "SET_PRIVATE_FIELDS_CONVENTION" but that convention doesn't work if an public getter is not set.

 

My constructor of client :

final CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().conventions(asList(
  Conventions.ANNOTATION_CONVENTION, Conventions.SET_PRIVATE_FIELDS_CONVENTION, Conventions.OBJECT_ID_GENERATORS)).automatic(true).build()));
 
// Create settings
final MongoClientSettings.Builder settings = MongoClientSettings.builder()
        .applyToClusterSettings(builder -> builder.hosts(singletonList(serverAddress)))
        .uuidRepresentation(UuidRepresentation.STANDARD)
        .codecRegistry(pojoCodecRegistry)
        .credential(MongoCredential.createCredential(username, database, password.toCharArray()));
 
this.mongoClient = MongoClients.create(settings.build());

 

My pojo class :

public class Account {
    private final UUID id;
    private String username;
    private Date createdAt;
 
    public Account(@Nonnull UUID id, @Nonnull String username, @Nonnull Date createdAt) {
        this.id = id;
        this.username = username;
        this.createdAt = createdAt;
    }
 
    @BsonCreator
    public Account(@BsonProperty(value = "_id") UUID id) {
        this.id = id;
    }
 
    /**
     * This is unique id of the player (Mojang UUID)
     *
     * @return The player unique id
     */
    public UUID getId() {
        return id;
    }
 
    /**
     * This is a name of the player (Minecraft account name)
     *
     * @return The player name
     */
    public String getName() {
        return username;
    }
 
    /**
     * This is a created date
     *
     * @return The created date
     */
    public Date getCreatedAt() {
        return createdAt;
    }
 
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Account) {
            return ((Account) obj).getId().equals(id);
        }
 
        return false;
    }
 
    @Override
    public String toString() {
        return "Account{"
                + "id='" + id + "'"
                + ",username='" + username + "'"
                + ",createdAt=" + createdAt
                + "}";
    }
}

 

The result :

Actual : Account{id='2df03368-a4c6-4b05-8807-fca12ab50134',username='null',createdAt=null}
Excepted : Account{id='2df03368-a4c6-4b05-8807-fca12ab50134',username='orblazer',createdAt=null}

 

Currently for fix that issue i have need to make an public getter "getUsername".

I have also try to put "@BsonProperty("username")" to "getName" method.

 

Thanks.

 



 Comments   
Comment by Backlog - Core Eng Program Management Team [ 04/Jan/21 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by Jeffrey Yemin [ 18/Dec/20 ]

Hi orblazer.minecraft@gmail.com

Thanks for reaching out. I think this is the intended behavior, based on the documentation for the convention, which says:

     * A convention that enables private fields to be set using reflection.
     *
     * <p>This convention mimics how some other JSON libraries directly set a private field when there is no setter.</p>
     * <p>Note: This convention is not part of the {@code DEFAULT_CONVENTIONS} list and must explicitly be set.</p>

The driver still requires the getter method during the encoding process. Are you asking for an additional convention where the driver also uses the private field during encoding?

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