Skip to content

Commit b614d67

Browse files
committed
Fix #3305 (CharSequence serialization in JDK 15 POJO, not String)
1 parent 3ccde7d commit b614d67

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

release-notes/VERSION-2.x

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7-
(not yet released)
7+
2.12.6 (not yet released)
88

99
#3280: Can not deserialize json to enum value with Object-/Array-valued input,
1010
`@JsonCreator`
1111
(reported by peteryuanpan@github)
12+
#3305: ObjectMapper serializes `CharSequence` subtypes as POJO instead of
13+
as String (JDK 15+)
14+
(reported by stevenupton@github; fix suggested by Sergey C)
1215
#3328: Possible DoS issue
1316

1417
2.12.5 (27-Aug-2021)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public String findNameForIsGetter(AnnotatedMethod am, String name)
7979
return null;
8080
}
8181

82-
@Override
82+
@Override
8383
public String findNameForRegularGetter(AnnotatedMethod am, String name)
8484
{
8585
if ((_getterPrefix != null) && name.startsWith(_getterPrefix)) {

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvid
409409
}
410410

411411
// Any properties to suppress?
412+
413+
// 10-Dec-2021, tatu: [databind#3305] Some JDK types need special help
414+
// (initially, `CharSequence` with its `isEmpty()` default impl)
415+
props = filterUnwantedJDKProperties(config, beanDesc, props);
412416
props = filterBeanProperties(config, beanDesc, props);
413417

414418
// Need to allow reordering of properties to serialize
@@ -629,7 +633,7 @@ protected List<BeanPropertyWriter> findBeanProperties(SerializerProvider prov,
629633
/* Overridable non-public methods for manipulating bean properties
630634
/**********************************************************
631635
*/
632-
636+
633637
/**
634638
* Overridable method that can filter out properties. Default implementation
635639
* checks annotations class may have.
@@ -665,6 +669,36 @@ protected List<BeanPropertyWriter> filterBeanProperties(SerializationConfig conf
665669
return props;
666670
}
667671

672+
/**
673+
* Overridable method used to filter out specifically problematic JDK provided
674+
* properties.
675+
*<p>
676+
* See issue <a href="https://github.com/FasterXML/jackson-databind/issues/3305">
677+
* databind-3305</a> for details.
678+
*
679+
* @since 2.13.1
680+
*/
681+
protected List<BeanPropertyWriter> filterUnwantedJDKProperties(SerializationConfig config,
682+
BeanDescription beanDesc, List<BeanPropertyWriter> props)
683+
{
684+
// First, only consider something that implement `CharSequence`
685+
if (beanDesc.getType().isTypeOrSubTypeOf(CharSequence.class)) {
686+
Iterator<BeanPropertyWriter> it = props.iterator();
687+
while (it.hasNext()) {
688+
BeanPropertyWriter prop = it.next();
689+
// And only remove property induced by `isEmpty()` method declared
690+
// in `CharSequence` (default implementation)
691+
AnnotatedMember m = prop.getMember();
692+
if ((m instanceof AnnotatedMethod)
693+
&& "isEmpty".equals(m.getName())
694+
&& m.getDeclaringClass() == CharSequence.class) {
695+
it.remove();
696+
}
697+
}
698+
}
699+
return props;
700+
}
701+
668702
/**
669703
* Method called to handle view information for constructed serializer,
670704
* based on bean property writers.

0 commit comments

Comments
 (0)