I discovered that Jackson under some circumstances would map private fields 
even though visibility is set to public only. See below very simple 
convertValue unit test example where I would expect r1 and r2 to have 2 
different values, since I assumed that Jackson wouldn't map public getters 
to their internal private fields when visibility is public only. I found a 
similar issue with private set methods where it would attempt to create 
internal deserializers for these setters and in turn cause exceptions when 
the parameter was not serializable. Sure, I can workaround this using 
JsonIgnore annotations, but is this really suppose to work like that or is 
this maybe a bug? I tested this with versions 2.9.8 and 2.10.4. 


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;

import java.util.UUID;

public class JacksonPrivatePropertyAccessTest {

    public static class RandomId {

        private String id = UUID.randomUUID().toString();

        public String getId() {
            return id;
        }

    }

    @Test
    public void noPrivateAccess() {
        RandomId r1 = new RandomId();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, 
JsonAutoDetect.Visibility.PUBLIC_ONLY);
        RandomId r2 = mapper.convertValue(r1, RandomId.class);
        Assert.assertNotEquals(r1.getId(), r2.getId());
    }
}



-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/10872401-56a2-4f86-9cc6-ba389f26d0e6%40googlegroups.com.

Reply via email to