Skip to content

Commit dbe68f6

Browse files
committed
Fix #95 (oldest fixed issue so far for 2.6!)
1 parent a341904 commit dbe68f6

15 files changed

+87
-14
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
6969
<dependency>
7070
<groupId>com.fasterxml.jackson.core</groupId>
7171
<artifactId>jackson-annotations</artifactId>
72-
<version>2.5.0</version>
72+
<version>2.6.0-SNAPSHOT</version>
7373
</dependency>
7474
<dependency>
7575
<groupId>com.fasterxml.jackson.core</groupId>

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Project: jackson-databind
66

77
2.6.0 (not yet released)
88

9+
#95: Allow read-only properties with `@JsonIgnoreProperties(allowGetters=true)`
910
#312: Support Type Id mappings where two ids map to same Class
1011
#348: ObjectMapper.valueToTree does not work with @JsonRawValue
1112
(reported by Chris P, pimlottc@github)

src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,23 @@ public PropertyName findRootName(AnnotatedClass ac) {
223223
* List of property names is applied
224224
* after other detection mechanisms, to filter out these specific
225225
* properties from being serialized and deserialized.
226+
*
227+
* @param forSerialization True if requesting properties to ignore for serialization;
228+
* false if for deserialization
229+
*/
230+
public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
231+
// !!! Change direction in 2.7 or later
232+
return findPropertiesToIgnore(ac);
233+
}
234+
235+
/**
236+
* @deprecated Since 2.6, use variant that takes second argument.
226237
*/
238+
@Deprecated
227239
public String[] findPropertiesToIgnore(Annotated ac) {
228240
return null;
229241
}
230-
242+
231243
/**
232244
* Method for checking whether an annotation indicates that all unknown properties
233245
*/

src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,8 @@ public JsonDeserializer<?> createMapDeserializer(DeserializationContext ctxt,
11641164
if (deser == null) {
11651165
ValueInstantiator inst = findValueInstantiator(ctxt, beanDesc);
11661166
MapDeserializer md = new MapDeserializer(type, inst, keyDes, contentDeser, contentTypeDeser);
1167-
md.setIgnorableProperties(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo()));
1167+
AnnotationIntrospector ai = config.getAnnotationIntrospector();
1168+
md.setIgnorableProperties(ai.findPropertiesToIgnore(beanDesc.getClassInfo(), false));
11681169
deser = md;
11691170
}
11701171
}

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
618618
}
619619
// And possibly add more properties to ignore
620620
if (accessor != null) {
621-
String[] ignorals = intr.findPropertiesToIgnore(accessor);
621+
String[] ignorals = intr.findPropertiesToIgnore(accessor, false);
622622
if (ignorals != null && ignorals.length != 0) {
623623
HashSet<String> newIgnored = ArrayBuilders.setAndArray(contextual._ignorableProps, ignorals);
624624
contextual = contextual.withIgnorableProperties(newIgnored);

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ protected void addBeanProps(DeserializationContext ctxt,
451451
}
452452
}
453453
// Or explicit/implicit definitions?
454-
Set<String> ignored = ArrayBuilders.arrayToSet(intr.findPropertiesToIgnore(beanDesc.getClassInfo()));
454+
Set<String> ignored = ArrayBuilders.arrayToSet(intr.findPropertiesToIgnore(beanDesc.getClassInfo(), false));
455455
for (String propName : ignored) {
456456
builder.addIgnorable(propName);
457457
}

src/main/java/com/fasterxml/jackson/databind/deser/std/MapDeserializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
254254
if (intr != null && property != null) {
255255
AnnotatedMember member = property.getMember();
256256
if (member != null) {
257-
String[] moreToIgnore = intr.findPropertiesToIgnore(member);
257+
String[] moreToIgnore = intr.findPropertiesToIgnore(member, false);
258258
if (moreToIgnore != null) {
259259
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
260260
for (String str : moreToIgnore) {

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationIntrospectorPair.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,24 @@ public PropertyName findRootName(AnnotatedClass ac)
113113
}
114114

115115
@Override
116-
public String[] findPropertiesToIgnore(Annotated ac)
117-
{
116+
@Deprecated // since 2.6
117+
public String[] findPropertiesToIgnore(Annotated ac) {
118118
String[] result = _primary.findPropertiesToIgnore(ac);
119119
if (result == null) {
120120
result = _secondary.findPropertiesToIgnore(ac);
121121
}
122122
return result;
123123
}
124124

125+
@Override
126+
public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
127+
String[] result = _primary.findPropertiesToIgnore(ac, forSerialization);
128+
if (result == null) {
129+
result = _secondary.findPropertiesToIgnore(ac, forSerialization);
130+
}
131+
return result;
132+
}
133+
125134
@Override
126135
public Boolean findIgnoreUnknownProperties(AnnotatedClass ac)
127136
{

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

+20
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,31 @@ public PropertyName findRootName(AnnotatedClass ac)
8181
}
8282

8383
@Override
84+
@Deprecated // since 2.6, remove from 2.7 or later
8485
public String[] findPropertiesToIgnore(Annotated ac) {
8586
JsonIgnoreProperties ignore = _findAnnotation(ac, JsonIgnoreProperties.class);
8687
return (ignore == null) ? null : ignore.value();
8788
}
8889

90+
@Override // since 2.6
91+
public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
92+
JsonIgnoreProperties ignore = _findAnnotation(ac, JsonIgnoreProperties.class);
93+
if (ignore == null) {
94+
return null;
95+
}
96+
// 13-May-2015, tatu: As per [databind#95], allow read-only/write-only props
97+
if (forSerialization) {
98+
if (ignore.allowGetters()) {
99+
return null;
100+
}
101+
} else {
102+
if (ignore.allowSetters()) {
103+
return null;
104+
}
105+
}
106+
return ignore.value();
107+
}
108+
89109
@Override
90110
public Boolean findIgnoreUnknownProperties(AnnotatedClass ac) {
91111
JsonIgnoreProperties ignore = _findAnnotation(ac, JsonIgnoreProperties.class);

src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,8 @@ protected JsonSerializer<?> buildMapSerializer(SerializationConfig config,
761761
} else {
762762
*/
763763
Object filterId = findFilterId(config, beanDesc);
764-
MapSerializer mapSer = MapSerializer.construct(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo()),
764+
AnnotationIntrospector ai = config.getAnnotationIntrospector();
765+
MapSerializer mapSer = MapSerializer.construct(ai.findPropertiesToIgnore(beanDesc.getClassInfo(), true),
765766
type, staticTyping, elementTypeSerializer,
766767
keySerializer, elementValueSerializer, filterId);
767768
Object suppressableValue = findSuppressableContentValue(config,

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ protected List<BeanPropertyWriter> filterBeanProperties(SerializationConfig conf
587587
{
588588
AnnotationIntrospector intr = config.getAnnotationIntrospector();
589589
AnnotatedClass ac = beanDesc.getClassInfo();
590-
String[] ignored = intr.findPropertiesToIgnore(ac);
590+
String[] ignored = intr.findPropertiesToIgnore(ac, true);
591591
if (ignored != null && ignored.length > 0) {
592592
HashSet<String> ignoredSet = ArrayBuilders.arrayToSet(ignored);
593593
Iterator<BeanPropertyWriter> it = props.iterator();

src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
420420

421421
// Then we may have an override for Object Id
422422
if (accessor != null) {
423-
ignorals = intr.findPropertiesToIgnore(accessor);
423+
ignorals = intr.findPropertiesToIgnore(accessor, true);
424424
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
425425
if (objectIdInfo == null) {
426426
// no ObjectId override, but maybe ObjectIdRef?

src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
356356
HashSet<String> ignored = _ignoredEntries;
357357
boolean sortKeys = false;
358358
if (intr != null && propertyAcc != null) {
359-
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc);
359+
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc, true);
360360
if (moreToIgnore != null) {
361361
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
362362
for (String str : moreToIgnore) {
+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import com.fasterxml.jackson.databind.*;
88

9-
public class TestIgnorePropsForSerialization
9+
public class IgnorePropsTest
1010
extends BaseMapTest
1111
{
1212
@JsonIgnoreProperties({"b", "c"})
@@ -63,7 +63,7 @@ static class MapWrapper {
6363
value.put("b", 2);
6464
}
6565
}
66-
66+
6767
/*
6868
/****************************************************************
6969
/* Unit tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fasterxml.jackson.databind.filter;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.databind.*;
5+
6+
/**
7+
* Failing test related to [databind#95]
8+
*/
9+
public class ReadOnlyProperties95Test extends BaseMapTest
10+
{
11+
@JsonIgnoreProperties(value={ "computed" }, allowGetters=true)
12+
static class ReadOnlyBean
13+
{
14+
public int value = 3;
15+
16+
public int getComputed() { return 32; }
17+
}
18+
19+
public void testReadOnlyProp() throws Exception
20+
{
21+
ObjectMapper m = new ObjectMapper();
22+
String json = m.writeValueAsString(new ReadOnlyBean());
23+
if (json.indexOf("computed") < 0) {
24+
fail("Should have property 'computed', didn't: "+json);
25+
}
26+
ReadOnlyBean bean = m.readValue(json, ReadOnlyBean.class);
27+
assertNotNull(bean);
28+
}
29+
}

0 commit comments

Comments
 (0)