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

Protected and package private fields are not set by the SET_PRIVATE_FIELDS_CONVENTION

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • 3.10.1
    • POJO
    • None
    • Minor Change

    Description

      Using the PojoCodecProvider with convention Conventions.SET_PRIVATE_FIELDS_CONVENTION does not set the protected or package private fields directly like it does with private fields. If this is the intended behavior, I think it would be useful to have another convention that sets the fields directly regardless of the access modifier. Below I have a small test to demonstrate the problem.

       

      1
      public class PrivateSetConventionTest {
      2
          
      3
          public static class Pojo {
      4
              protected String protectedFoo;
      5
              private String   privateFoo;
      6
              String ppFoo;
      7
              public String getProtectedFoo() {
      8
                  return protectedFoo;
      9
              }
      10
              public String getPrivateFoo() {
      11
                  return privateFoo;
      12
              }        @Override
      13
              public boolean equals( Object o ) {
      14
                  if ( this == o ) return true;
      15
                  if ( !( o instanceof Pojo ) ) return false;
      16
                  Pojo pojo = ( Pojo ) o;
      17
                  return Objects.equals( protectedFoo, pojo.protectedFoo ) &&
      18
                         Objects.equals( privateFoo, pojo.privateFoo ) &&
      19
                         Objects.equals( ppFoo, pojo.ppFoo );
      20
              }        @Override
      21
              public int hashCode() {
      22
                  return Objects.hash( protectedFoo, privateFoo, ppFoo );
      23
              }        @Override
      24
              public String toString() {
      25
                  return "Pojo{" + "protectedFoo='" + protectedFoo + '\'' + ", privateFoo='" + privateFoo + '\'' +
      26
                         ", ppFoo='" + ppFoo + '\'' + '}';
      27
              }
      28
          }    private CodecRegistry codecRegistry;    @BeforeClass
      29
          public void setup() {
      30
              List<Convention> conventions = Arrays.asList( Conventions.SET_PRIVATE_FIELDS_CONVENTION );
      31
              CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).conventions( conventions ).build();
      32
              codecRegistry = CodecRegistries.fromRegistries(
      33
                      MongoClientSettings.getDefaultCodecRegistry(),
      34
                      CodecRegistries.fromProviders( pojoCodecProvider )
      35
              );
      36
          }    @Test
      37
          public void testDecode() {
      38
              Pojo expected = new Pojo();
      39
              expected.protectedFoo = "Test 1";
      40
              expected.privateFoo = "Test 2";
      41
              expected.ppFoo = "Test 3";
      42
              BsonDocument rawBson = new BsonDocument().append( "protectedFoo", new BsonString( expected.protectedFoo ) )
      43
                                                       .append( "privateFoo", new BsonString( expected.privateFoo ) )
      44
                                                       .append( "ppFoo", new BsonString( expected.ppFoo ) );
      45
              Codec<Pojo> pojoCodec = codecRegistry.get( Pojo.class );
      46
              BsonDocumentReader reader = new BsonDocumentReader( rawBson );
      47
              Pojo actual = pojoCodec.decode( reader, DecoderContext.builder().build() );
      48
              Assert.assertEquals( actual, expected );
      49
          }
      50
      }
      

      The output of running above test is:

      Expected :Pojo{protectedFoo='Test 1', privateFoo='Test 2', ppFoo='Test 3'}
      Actual   :Pojo{protectedFoo='null', privateFoo='Test 2', ppFoo='null'}
      

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            aarjav Aarjav Patel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: