[JAVA-3220] Protected and package private fields are not set by the SET_PRIVATE_FIELDS_CONVENTION Created: 28/Feb/19  Updated: 10/Mar/23

Status: Backlog
Project: Java Driver
Component/s: POJO
Affects Version/s: 3.10.1
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Aarjav Patel Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by JAVA-4545 Introduce convention to disable gette... Backlog
Epic Link: Investigate our POJO implementation
Quarter: FY23Q4, FY24Q2
Backwards Compatibility: 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'}

 

 



 Comments   
Comment by Aarjav Patel [ 13/Apr/19 ]

Can this be added in the next release? or do we need a different implementation of this (or these) convention(s)?

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