-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.10.1
-
Component/s: POJO
-
None
-
Minor Change
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.
public class PrivateSetConventionTest { public static class Pojo { protected String protectedFoo; private String privateFoo; String ppFoo; public String getProtectedFoo() { return protectedFoo; } public String getPrivateFoo() { return privateFoo; } @Override public boolean equals( Object o ) { if ( this == o ) return true; if ( !( o instanceof Pojo ) ) return false; Pojo pojo = ( Pojo ) o; return Objects.equals( protectedFoo, pojo.protectedFoo ) && Objects.equals( privateFoo, pojo.privateFoo ) && Objects.equals( ppFoo, pojo.ppFoo ); } @Override public int hashCode() { return Objects.hash( protectedFoo, privateFoo, ppFoo ); } @Override public String toString() { return "Pojo{" + "protectedFoo='" + protectedFoo + '\'' + ", privateFoo='" + privateFoo + '\'' + ", ppFoo='" + ppFoo + '\'' + '}'; } } private CodecRegistry codecRegistry; @BeforeClass public void setup() { List<Convention> conventions = Arrays.asList( Conventions.SET_PRIVATE_FIELDS_CONVENTION ); CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).conventions( conventions ).build(); codecRegistry = CodecRegistries.fromRegistries( MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders( pojoCodecProvider ) ); } @Test public void testDecode() { Pojo expected = new Pojo(); expected.protectedFoo = "Test 1"; expected.privateFoo = "Test 2"; expected.ppFoo = "Test 3"; BsonDocument rawBson = new BsonDocument().append( "protectedFoo", new BsonString( expected.protectedFoo ) ) .append( "privateFoo", new BsonString( expected.privateFoo ) ) .append( "ppFoo", new BsonString( expected.ppFoo ) ); Codec<Pojo> pojoCodec = codecRegistry.get( Pojo.class ); BsonDocumentReader reader = new BsonDocumentReader( rawBson ); Pojo actual = pojoCodec.decode( reader, DecoderContext.builder().build() ); Assert.assertEquals( actual, expected ); } }
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'}
- is duplicated by
-
JAVA-4545 Introduce convention to disable getters/setters during POJO serialization
- Backlog
- links to