Skip to content

Commit

Permalink
Fix #95 (oldest fixed issue so far for 2.6!)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 14, 2015
1 parent a341904 commit dbe68f6
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
<version>2.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Project: jackson-databind

2.6.0 (not yet released)

#95: Allow read-only properties with `@JsonIgnoreProperties(allowGetters=true)`
#312: Support Type Id mappings where two ids map to same Class
#348: ObjectMapper.valueToTree does not work with @JsonRawValue
(reported by Chris P, pimlottc@github)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,23 @@ public PropertyName findRootName(AnnotatedClass ac) {
* List of property names is applied
* after other detection mechanisms, to filter out these specific
* properties from being serialized and deserialized.
*
* @param forSerialization True if requesting properties to ignore for serialization;
* false if for deserialization
*/
public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
// !!! Change direction in 2.7 or later
return findPropertiesToIgnore(ac);
}

/**
* @deprecated Since 2.6, use variant that takes second argument.
*/
@Deprecated
public String[] findPropertiesToIgnore(Annotated ac) {
return null;
}

/**
* Method for checking whether an annotation indicates that all unknown properties
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,8 @@ public JsonDeserializer<?> createMapDeserializer(DeserializationContext ctxt,
if (deser == null) {
ValueInstantiator inst = findValueInstantiator(ctxt, beanDesc);
MapDeserializer md = new MapDeserializer(type, inst, keyDes, contentDeser, contentTypeDeser);
md.setIgnorableProperties(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo()));
AnnotationIntrospector ai = config.getAnnotationIntrospector();
md.setIgnorableProperties(ai.findPropertiesToIgnore(beanDesc.getClassInfo(), false));
deser = md;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
}
// And possibly add more properties to ignore
if (accessor != null) {
String[] ignorals = intr.findPropertiesToIgnore(accessor);
String[] ignorals = intr.findPropertiesToIgnore(accessor, false);
if (ignorals != null && ignorals.length != 0) {
HashSet<String> newIgnored = ArrayBuilders.setAndArray(contextual._ignorableProps, ignorals);
contextual = contextual.withIgnorableProperties(newIgnored);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ protected void addBeanProps(DeserializationContext ctxt,
}
}
// Or explicit/implicit definitions?
Set<String> ignored = ArrayBuilders.arrayToSet(intr.findPropertiesToIgnore(beanDesc.getClassInfo()));
Set<String> ignored = ArrayBuilders.arrayToSet(intr.findPropertiesToIgnore(beanDesc.getClassInfo(), false));
for (String propName : ignored) {
builder.addIgnorable(propName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
if (intr != null && property != null) {
AnnotatedMember member = property.getMember();
if (member != null) {
String[] moreToIgnore = intr.findPropertiesToIgnore(member);
String[] moreToIgnore = intr.findPropertiesToIgnore(member, false);
if (moreToIgnore != null) {
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
for (String str : moreToIgnore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,24 @@ public PropertyName findRootName(AnnotatedClass ac)
}

@Override
public String[] findPropertiesToIgnore(Annotated ac)
{
@Deprecated // since 2.6
public String[] findPropertiesToIgnore(Annotated ac) {
String[] result = _primary.findPropertiesToIgnore(ac);
if (result == null) {
result = _secondary.findPropertiesToIgnore(ac);
}
return result;
}

@Override
public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
String[] result = _primary.findPropertiesToIgnore(ac, forSerialization);
if (result == null) {
result = _secondary.findPropertiesToIgnore(ac, forSerialization);
}
return result;
}

@Override
public Boolean findIgnoreUnknownProperties(AnnotatedClass ac)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,31 @@ public PropertyName findRootName(AnnotatedClass ac)
}

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

@Override // since 2.6
public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
JsonIgnoreProperties ignore = _findAnnotation(ac, JsonIgnoreProperties.class);
if (ignore == null) {
return null;
}
// 13-May-2015, tatu: As per [databind#95], allow read-only/write-only props
if (forSerialization) {
if (ignore.allowGetters()) {
return null;
}
} else {
if (ignore.allowSetters()) {
return null;
}
}
return ignore.value();
}

@Override
public Boolean findIgnoreUnknownProperties(AnnotatedClass ac) {
JsonIgnoreProperties ignore = _findAnnotation(ac, JsonIgnoreProperties.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ protected JsonSerializer<?> buildMapSerializer(SerializationConfig config,
} else {
*/
Object filterId = findFilterId(config, beanDesc);
MapSerializer mapSer = MapSerializer.construct(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo()),
AnnotationIntrospector ai = config.getAnnotationIntrospector();
MapSerializer mapSer = MapSerializer.construct(ai.findPropertiesToIgnore(beanDesc.getClassInfo(), true),
type, staticTyping, elementTypeSerializer,
keySerializer, elementValueSerializer, filterId);
Object suppressableValue = findSuppressableContentValue(config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ protected List<BeanPropertyWriter> filterBeanProperties(SerializationConfig conf
{
AnnotationIntrospector intr = config.getAnnotationIntrospector();
AnnotatedClass ac = beanDesc.getClassInfo();
String[] ignored = intr.findPropertiesToIgnore(ac);
String[] ignored = intr.findPropertiesToIgnore(ac, true);
if (ignored != null && ignored.length > 0) {
HashSet<String> ignoredSet = ArrayBuilders.arrayToSet(ignored);
Iterator<BeanPropertyWriter> it = props.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,

// Then we may have an override for Object Id
if (accessor != null) {
ignorals = intr.findPropertiesToIgnore(accessor);
ignorals = intr.findPropertiesToIgnore(accessor, true);
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
if (objectIdInfo == null) {
// no ObjectId override, but maybe ObjectIdRef?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
HashSet<String> ignored = _ignoredEntries;
boolean sortKeys = false;
if (intr != null && propertyAcc != null) {
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc);
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc, true);
if (moreToIgnore != null) {
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
for (String str : moreToIgnore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.fasterxml.jackson.databind.*;

public class TestIgnorePropsForSerialization
public class IgnorePropsTest
extends BaseMapTest
{
@JsonIgnoreProperties({"b", "c"})
Expand Down Expand Up @@ -63,7 +63,7 @@ static class MapWrapper {
value.put("b", 2);
}
}

/*
/****************************************************************
/* Unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fasterxml.jackson.databind.filter;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.*;

/**
* Failing test related to [databind#95]
*/
public class ReadOnlyProperties95Test extends BaseMapTest
{
@JsonIgnoreProperties(value={ "computed" }, allowGetters=true)
static class ReadOnlyBean
{
public int value = 3;

public int getComputed() { return 32; }
}

public void testReadOnlyProp() throws Exception
{
ObjectMapper m = new ObjectMapper();
String json = m.writeValueAsString(new ReadOnlyBean());
if (json.indexOf("computed") < 0) {
fail("Should have property 'computed', didn't: "+json);
}
ReadOnlyBean bean = m.readValue(json, ReadOnlyBean.class);
assertNotNull(bean);
}
}

0 comments on commit dbe68f6

Please sign in to comment.