Skip to content

Commit

Permalink
Fix #2390
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 7, 2019
1 parent a22a18a commit 985ee58
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 32 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,7 @@ Victor Noël (victornoel@github)
(2.10.0)
* Reported #2339: Suboptimal return type for `ObjectNode.set()`
(2.10.0)

Chris Mercer (cmercer@github)
* Reported #2331: `JsonMappingException` through nested getter with generic wildcard return type
(2.10.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project: jackson-databind

#2331: `JsonMappingException` through nested getter with generic wildcard return type
(reported by sunchezz89@github)
#2390: `Iterable` serialization breaks when adding `@JsonFilter` annotation
(reported by Chris M)
#2392: `BeanDeserializerModifier.modifyDeserializer()` not applied to custom bean deserializers
(reported by andreasbaus@github)
#2393: `TreeTraversingParser.getLongValue()` incorrectly checks `canConvertToInt()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,12 @@ protected JsonSerializer<?> _createSerializer2(SerializerProvider prov,
// And this is where this class comes in: if type is not a
// known "primary JDK type", perhaps it's a bean? We can still
// get a null, if we can't find a single suitable bean property.
ser = findBeanSerializer(prov, type, beanDesc);
// Finally: maybe we can still deal with it as an implementation of some basic JDK interface?
ser = findBeanOrAddOnSerializer(prov, type, beanDesc, staticTyping);
// 18-Sep-2014, tatu: Actually, as per [jackson-databind#539], need to get
// 'unknown' serializer assigned earlier, here, so that it gets properly
// post-processed
if (ser == null) {
ser = findSerializerByAddonType(config, type, beanDesc, staticTyping);
// 18-Sep-2014, tatu: Actually, as per [jackson-databind#539], need to get
// 'unknown' serializer assigned earlier, here, so that it gets properly
// post-processed
if (ser == null) {
ser = prov.getUnknownTypeSerializer(beanDesc.getBeanClass());
}
ser = prov.getUnknownTypeSerializer(beanDesc.getBeanClass());
}
}
}
Expand All @@ -260,12 +256,23 @@ protected JsonSerializer<?> _createSerializer2(SerializerProvider prov,
/**********************************************************
*/

@Deprecated // since 2.10
public JsonSerializer<Object> findBeanSerializer(SerializerProvider prov, JavaType type,
BeanDescription beanDesc)
throws JsonMappingException
{
return findBeanOrAddOnSerializer(prov, type, beanDesc, prov.isEnabled(MapperFeature.USE_STATIC_TYPING));
}

/**
* Method that will try to construct a {@link BeanSerializer} for
* given class. Returns null if no properties are found.
* given class if at least one property is found, OR, if not,
* one of add-on types.
*<p>
* NOTE: behavior changed a bit
*/
public JsonSerializer<Object> findBeanSerializer(SerializerProvider prov, JavaType type,
BeanDescription beanDesc)
public JsonSerializer<Object> findBeanOrAddOnSerializer(SerializerProvider prov, JavaType type,
BeanDescription beanDesc, boolean staticTyping)
throws JsonMappingException
{
// First things first: we know some types are not beans...
Expand All @@ -276,7 +283,7 @@ public JsonSerializer<Object> findBeanSerializer(SerializerProvider prov, JavaTy
return null;
}
}
return constructBeanSerializer(prov, beanDesc);
return constructBeanOrAddOnSerializer(prov, type, beanDesc, staticTyping);
}

/**
Expand Down Expand Up @@ -344,14 +351,23 @@ public TypeSerializer findPropertyContentTypeSerializer(JavaType containerType,
/**********************************************************
*/

@Deprecated // since 2.10
protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov,
BeanDescription beanDesc)
throws JsonMappingException
{
return constructBeanOrAddOnSerializer(prov, beanDesc.getType(), beanDesc, prov.isEnabled(MapperFeature.USE_STATIC_TYPING));
}

/**
* Method called to construct serializer for serializing specified bean type.
* Method called to construct serializer for serializing specified bean type if
* (but only if, as of 2.10), at least one property is found.
*
* @since 2.1
* @since 2.10
*/
@SuppressWarnings("unchecked")
protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov,
BeanDescription beanDesc)
protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvider prov,
JavaType type, BeanDescription beanDesc, boolean staticTyping)
throws JsonMappingException
{
// 13-Oct-2010, tatu: quick sanity check: never try to create bean serializer for plain Object
Expand Down Expand Up @@ -402,18 +418,18 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov

AnnotatedMember anyGetter = beanDesc.findAnyGetter();
if (anyGetter != null) {
JavaType type = anyGetter.getType();
JavaType anyType = anyGetter.getType();
// copied from BasicSerializerFactory.buildMapSerializer():
boolean staticTyping = config.isEnabled(MapperFeature.USE_STATIC_TYPING);
JavaType valueType = type.getContentType();
JavaType valueType = anyType.getContentType();
TypeSerializer typeSer = createTypeSerializer(config, valueType);
// last 2 nulls; don't know key, value serializers (yet)
// 23-Feb-2015, tatu: As per [databind#705], need to support custom serializers
JsonSerializer<?> anySer = findSerializerFromAnnotation(prov, anyGetter);
if (anySer == null) {
// TODO: support '@JsonIgnoreProperties' with any setter?
anySer = MapSerializer.construct(/* ignored props*/ (Set<String>) null,
type, staticTyping, typeSer, null, null, /*filterId*/ null);
anyType, config.isEnabled(MapperFeature.USE_STATIC_TYPING),
typeSer, null, null, /*filterId*/ null);
}
// TODO: can we find full PropertyName?
PropertyName name = PropertyName.construct(anyGetter.getName());
Expand All @@ -435,15 +451,20 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
try {
ser = (JsonSerializer<Object>) builder.build();
} catch (RuntimeException e) {
prov.reportBadTypeDefinition(beanDesc, "Failed to construct BeanSerializer for %s: (%s) %s",
return prov.reportBadTypeDefinition(beanDesc, "Failed to construct BeanSerializer for %s: (%s) %s",
beanDesc.getType(), e.getClass().getName(), e.getMessage());
}
if (ser == null) {
// If we get this far, there were no properties found, so no regular BeanSerializer
// would be constructed. But, couple of exceptions.
// First: if there are known annotations, just create 'empty bean' serializer
if (beanDesc.hasKnownClassAnnotations()) {
return builder.createDummy();
// 06-Aug-2019, tatu: As per [databind#2390], we need to check for add-ons here,
// before considering fallbacks
ser = (JsonSerializer<Object>) findSerializerByAddonType(config, type, beanDesc, staticTyping);
if (ser == null) {
// If we get this far, there were no properties found, so no regular BeanSerializer
// would be constructed. But, couple of exceptions.
// First: if there are known annotations, just create 'empty bean' serializer
if (beanDesc.hasKnownClassAnnotations()) {
return builder.createDummy();
}
}
}
return ser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ protected static class CustomJsonSerializerFactory extends BeanSerializerFactory
public CustomJsonSerializerFactory() { super(null); }

@Override
protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov,
BeanDescription beanDesc)
protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvider prov,
JavaType type, BeanDescription beanDesc, boolean staticTyping)
throws JsonMappingException
{
return new CustomJsonSerializer(super.constructBeanSerializer(prov, beanDesc) );
return new CustomJsonSerializer(super.constructBeanOrAddOnSerializer(prov, type, beanDesc, staticTyping) );
}
}

// [Issue#543]
// [databind#543]
static class ContainerWithTwoAnimals<U extends Animal,V extends Animal> extends ContainerWithField<U> {
public V otherAnimal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.*;
Expand Down Expand Up @@ -45,7 +46,7 @@ static class BeanWithIterator {

public Iterator<String> getValues() { return values.iterator(); }
}

static class IntIterable implements Iterable<Integer>
{
@Override
Expand Down Expand Up @@ -99,6 +100,10 @@ public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provi
}
}

// [databind#2390]
@JsonFilter("default")
static class IntIterable2390 extends IntIterable { }

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -156,4 +161,11 @@ public void testIterable358() throws Exception {
String json = MAPPER.writeValueAsString(new B());
assertEquals("{\"list\":[[\"Hello world.\"]]}", json);
}

// [databind#2390]
public void testIterableWithAnnotation() throws Exception
{
assertEquals("[1,2,3]",
STATIC_MAPPER.writeValueAsString(new IntIterable2390()));
}
}

0 comments on commit 985ee58

Please sign in to comment.