Skip to content

Commit 61a829a

Browse files
committed
Fix #1327
1 parent 84d6872 commit 61a829a

File tree

8 files changed

+68
-18
lines changed

8 files changed

+68
-18
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,20 @@ public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) {
796796
return EMPTY_INCLUDE;
797797
}
798798

799+
@Override
800+
public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
801+
JsonInclude.Value defaultIncl)
802+
{
803+
ConfigOverride overrides = findConfigOverride(baseType);
804+
if (overrides != null) {
805+
JsonInclude.Value v = overrides.getInclude();
806+
if (v != null) {
807+
return v;
808+
}
809+
}
810+
return defaultIncl;
811+
}
812+
799813
/*
800814
/**********************************************************
801815
/* MapperConfig implementation/overrides: other

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,20 @@ public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) {
899899
return _serializationInclusion;
900900
}
901901

902+
@Override
903+
public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
904+
JsonInclude.Value defaultIncl)
905+
{
906+
ConfigOverride overrides = findConfigOverride(baseType);
907+
if (overrides != null) {
908+
JsonInclude.Value v = overrides.getInclude();
909+
if (v != null) {
910+
return v;
911+
}
912+
}
913+
return defaultIncl;
914+
}
915+
902916
/*
903917
/**********************************************************
904918
/* Configuration: other

src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfig.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,26 @@ public BeanDescription introspectDirectClassAnnotations(Class<?> cls) {
357357

358358
/**
359359
* Accessor for default property inclusion to use for serialization,
360-
* considering possible per-type override for given base type.
360+
* considering possible per-type override for given base type.<br>
361+
* NOTE: if no override found, defaults to value returned by
362+
* {@link #getDefaultPropertyInclusion()}.
361363
*
362364
* @since 2.7
363365
*/
364366
public abstract JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType);
365367

368+
/**
369+
* Accessor for default property inclusion to use for serialization,
370+
* considering possible per-type override for given base type; but
371+
* if none found, returning given <code>defaultIncl</code>
372+
*
373+
* @param defaultIncl Inclusion setting to return if no overrides found.
374+
*
375+
* @since 2.8.2
376+
*/
377+
public abstract JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
378+
JsonInclude.Value defaultIncl);
379+
366380
/**
367381
* Accessor for default format settings to use for serialization (and, to a degree
368382
* deserialization), considering baseline settings and per-type defaults

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ public JsonInclude.Value findPropertyInclusion(JsonInclude.Value defValue) {
391391
if (_annotationIntrospector != null) {
392392
JsonInclude.Value incl = _annotationIntrospector.findPropertyInclusion(_classInfo);
393393
if (incl != null) {
394-
return defValue.withOverrides(incl);
394+
return (defValue == null) ? incl
395+
: defValue.withOverrides(incl);
395396
}
396397
}
397398
return defValue;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ public boolean isRequired() {
230230
/**
231231
* Method used to check if this property has specific inclusion override
232232
* associated with it or not.
233+
* It should NOT check for any default settings (global, per-type, or
234+
* containing POJO settings)
233235
*
234236
* @since 2.5
235237
*/

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,11 @@ public ObjectIdInfo withMember(AnnotatedMember member) {
553553
public JsonInclude.Value findInclusion() {
554554
AnnotatedMember a = getAccessor();
555555
// 16-Apr-2106, tatu: Let's include per-type default inclusion too
556-
JsonInclude.Value v = _config.getDefaultPropertyInclusion(a.getRawType());
557-
if (_annotationIntrospector != null) {
558-
JsonInclude.Value v2 = _annotationIntrospector.findPropertyInclusion(a);
559-
if (v2 != null) {
560-
if (v == null) {
561-
v = v2;
562-
} else {
563-
v = v.withOverrides(v2);
564-
}
565-
}
566-
}
556+
// 17-Aug-2016, tatu: Do NOT include global, or per-type defaults, because
557+
// not all of this information (specifically, enclosing type's settings)
558+
// is available here
559+
JsonInclude.Value v = (_annotationIntrospector == null) ?
560+
null : _annotationIntrospector.findPropertyInclusion(a);
567561
return (v == null) ? JsonInclude.Value.empty() : v;
568562
}
569563

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public PropertyBuilder(SerializationConfig config, BeanDescription beanDesc)
4444
{
4545
_config = config;
4646
_beanDesc = beanDesc;
47+
// NOTE: this includes global defaults and defaults of POJO that contains property,
48+
// but not defaults for types of properties referenced
4749
_defaultInclusion = beanDesc.findPropertyInclusion(
4850
config.getDefaultPropertyInclusion(beanDesc.getBeanClass()));
4951
_annotationIntrospector = _config.getAnnotationIntrospector();
@@ -97,14 +99,23 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
9799
Object valueToSuppress = null;
98100
boolean suppressNulls = false;
99101

100-
JsonInclude.Value inclV = _defaultInclusion.withOverrides(propDef.findInclusion());
102+
// 12-Jul-2016, tatu: [databind#1256] Need to make sure we consider type refinement
103+
JavaType actualType = (serializationType == null) ? declaredType : serializationType;
104+
105+
// 17-Aug-2016, tatu: Default inclusion covers global default (for all types), as well
106+
// as type-default for enclosing POJO. What we need, then, is per-type default (if any)
107+
// for declared property type... and finally property annotation overrides
108+
JsonInclude.Value inclV = _config.getDefaultPropertyInclusion(actualType.getRawClass(),
109+
_defaultInclusion);
110+
111+
// property annotation override
112+
113+
inclV = inclV.withOverrides(propDef.findInclusion());
101114
JsonInclude.Include inclusion = inclV.getValueInclusion();
115+
102116
if (inclusion == JsonInclude.Include.USE_DEFAULTS) { // should not occur but...
103117
inclusion = JsonInclude.Include.ALWAYS;
104118
}
105-
106-
// 12-Jul-2016, tatu: [databind#1256] Need to make sure we consider type refinement
107-
JavaType actualType = (serializationType == null) ? declaredType : serializationType;
108119

109120
switch (inclusion) {
110121
case NON_DEFAULT:

src/test/java/com/fasterxml/jackson/failing/JsonInclude1327Test.java renamed to src/test/java/com/fasterxml/jackson/databind/filter/JsonInclude1327Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.failing;
1+
package com.fasterxml.jackson.databind.filter;
22

33
import java.util.*;
44

0 commit comments

Comments
 (0)