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

Event SET_PRIVATE_FIELDS_CONVENTION enabled, the field need an public getter to be set

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Gone away
    • Icon: Major - P3 Major - P3
    • None
    • 4.1.1
    • POJO
    • None
    • 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.

       

      Attachments

        Activity

          People

            jeff.yemin@mongodb.com Jeffrey Yemin
            orblazer.minecraft@gmail.com dorian rodriguez
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: