Skip to content

Commit

Permalink
Fix #3305 (CharSequence serialization in JDK 15 POJO, not String)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 11, 2021
1 parent c175996 commit 528c656
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
10 changes: 8 additions & 2 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1402,5 +1402,11 @@ Abishek Ravichandran (abrav9595@github)
(2.13.0)
Joel Berger (jberger@github)
* Reported #3299: Do not automatically trim trailing whitespace from `java.util.regex.Pattern` values
(2.13.0)
* Reported #3299: Do not automatically trim trailing whitespace from `java.util.regex.Pattern`
values
(2.13.1)
Sergey Chernov (seregamorph@github)
* Suggested the fix for #3305: ObjectMapper serializes `CharSequence` subtypes as POJO
instead of as String (JDK 15+)
(2.13.1)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Project: jackson-databind
(reported by GrozaAnton@github)
#3299: Do not automatically trim trailing whitespace from `java.util.regex.Pattern` values
(reported by Joel B)
#3305: ObjectMapper serializes `CharSequence` subtypes as POJO instead of
as String (JDK 15+)
(reported by stevenupton@github; fix suggested by Sergey C)
#3308: `ObjectMapper.valueToTree()` fails when
`DeserializationFeature.FAIL_ON_TRAILING_TOKENS` is enabled
(fix contributed by raphaelNguyen@github)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String findNameForIsGetter(AnnotatedMethod am, String name)
return null;
}

@Override
@Override
public String findNameForRegularGetter(AnnotatedMethod am, String name)
{
if ((_getterPrefix != null) && name.startsWith(_getterPrefix)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvid
}

// Any properties to suppress?

// 10-Dec-2021, tatu: [databind#3305] Some JDK types need special help
// (initially, `CharSequence` with its `isEmpty()` default impl)
props = filterUnwantedJDKProperties(config, beanDesc, props);
props = filterBeanProperties(config, beanDesc, props);

// Need to allow reordering of properties to serialize
Expand Down Expand Up @@ -636,7 +640,7 @@ protected List<BeanPropertyWriter> findBeanProperties(SerializerProvider prov,
/* Overridable non-public methods for manipulating bean properties
/**********************************************************
*/

/**
* Overridable method that can filter out properties. Default implementation
* checks annotations class may have.
Expand Down Expand Up @@ -672,6 +676,36 @@ protected List<BeanPropertyWriter> filterBeanProperties(SerializationConfig conf
return props;
}

/**
* Overridable method used to filter out specifically problematic JDK provided
* properties.
*<p>
* See issue <a href="https://github.com/FasterXML/jackson-databind/issues/3305">
* databind-3305</a> for details.
*
* @since 2.13.1
*/
protected List<BeanPropertyWriter> filterUnwantedJDKProperties(SerializationConfig config,
BeanDescription beanDesc, List<BeanPropertyWriter> props)
{
// First, only consider something that implement `CharSequence`
if (beanDesc.getType().isTypeOrSubTypeOf(CharSequence.class)) {
Iterator<BeanPropertyWriter> it = props.iterator();
while (it.hasNext()) {
BeanPropertyWriter prop = it.next();
// And only remove property induced by `isEmpty()` method declared
// in `CharSequence` (default implementation)
AnnotatedMember m = prop.getMember();
if ((m instanceof AnnotatedMethod)
&& "isEmpty".equals(m.getName())
&& m.getDeclaringClass() == CharSequence.class) {
it.remove();
}
}
}
return props;
}

/**
* Method called to handle view information for constructed serializer,
* based on bean property writers.
Expand Down

0 comments on commit 528c656

Please sign in to comment.