Skip to content

Commit 73d3bbd

Browse files
committed
Fix #4200: use annotations for delegating @JsonCreator (#4228)
1 parent 3bc9a0a commit 73d3bbd

File tree

3 files changed

+144
-7
lines changed

3 files changed

+144
-7
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Project: jackson-databind
66

77
2.17.0 (not yet released)
88

9-
-
10-
119
2.16.1 (not yet released)
1210

11+
#4200: `JsonSetter(contentNulls = FAIL)` is ignored in delegating
12+
`@JsonCreator` argument
1313
#4216: Primitive array deserializer cannot being captured by `DeserializerModifier`
1414
(reported by @SakuraKoi)
1515
(fix contributed by Joo-Hyuk K)

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.core.JsonParser.NumberType;
1212

1313
import com.fasterxml.jackson.databind.*;
14+
import com.fasterxml.jackson.databind.cfg.ConfigOverride;
1415
import com.fasterxml.jackson.databind.deser.impl.*;
1516
import com.fasterxml.jackson.databind.deser.std.StdDelegatingDeserializer;
1617
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
@@ -695,12 +696,29 @@ protected void _replaceProperty(BeanPropertyMap props, SettableBeanProperty[] cr
695696

696697
@SuppressWarnings("unchecked")
697698
private JsonDeserializer<Object> _findDelegateDeserializer(DeserializationContext ctxt,
698-
JavaType delegateType, AnnotatedWithParams delegateCreator) throws JsonMappingException
699+
JavaType delegateType, AnnotatedWithParams delegateCreator)
700+
throws JsonMappingException
699701
{
700-
// Need to create a temporary property to allow contextual deserializers:
701-
BeanProperty.Std property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
702-
delegateType, null, delegateCreator,
703-
PropertyMetadata.STD_OPTIONAL);
702+
// 27-Nov-2023, tatu: [databind#4200] Need to resolve PropertyMetadata.
703+
// And all we have is the actual Creator method; but for annotations
704+
// we actually need the one parameter -- if there is one
705+
// (NOTE! This would not work for case of more than one parameter with
706+
// delegation, others injected)
707+
final BeanProperty property;
708+
709+
if ((delegateCreator != null) && (delegateCreator.getParameterCount() == 1)) {
710+
AnnotatedMember delegator = delegateCreator.getParameter(0);
711+
PropertyMetadata propMd = _getSetterInfo(ctxt, delegator, delegateType);
712+
property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
713+
delegateType, null, delegator, propMd);
714+
} else {
715+
// No creator indicated; or Zero, or more than 2 arguments (since we don't
716+
// know which one is the "real" delegating parameter. Although could possibly
717+
// figure it out if someone provides actual use case
718+
property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
719+
delegateType, null, delegateCreator,
720+
PropertyMetadata.STD_OPTIONAL);
721+
}
704722
TypeDeserializer td = delegateType.getTypeHandler();
705723
if (td == null) {
706724
td = ctxt.getConfig().findTypeDeserializer(delegateType);
@@ -720,6 +738,62 @@ private JsonDeserializer<Object> _findDelegateDeserializer(DeserializationContex
720738
return dd;
721739
}
722740

741+
/**
742+
* Method essentially copied from {@code BasicDeserializerFactory},
743+
* needed to find {@link PropertyMetadata} for Delegating Creator,
744+
* for access to annotation-derived info.
745+
*
746+
* @since 2.16.1
747+
*/
748+
protected PropertyMetadata _getSetterInfo(DeserializationContext ctxt,
749+
AnnotatedMember accessor, JavaType type)
750+
{
751+
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
752+
final DeserializationConfig config = ctxt.getConfig();
753+
754+
PropertyMetadata metadata = PropertyMetadata.STD_OPTIONAL;
755+
boolean needMerge = true;
756+
Nulls valueNulls = null;
757+
Nulls contentNulls = null;
758+
759+
// NOTE: compared to `POJOPropertyBuilder`, we only have access to creator
760+
// parameter, not other accessors, so code bit simpler
761+
// Ok, first: does property itself have something to say?
762+
if (intr != null) {
763+
JsonSetter.Value setterInfo = intr.findSetterInfo(accessor);
764+
if (setterInfo != null) {
765+
valueNulls = setterInfo.nonDefaultValueNulls();
766+
contentNulls = setterInfo.nonDefaultContentNulls();
767+
}
768+
}
769+
// If not, config override?
770+
if (needMerge || (valueNulls == null) || (contentNulls == null)) {
771+
ConfigOverride co = config.getConfigOverride(type.getRawClass());
772+
JsonSetter.Value setterInfo = co.getSetterInfo();
773+
if (setterInfo != null) {
774+
if (valueNulls == null) {
775+
valueNulls = setterInfo.nonDefaultValueNulls();
776+
}
777+
if (contentNulls == null) {
778+
contentNulls = setterInfo.nonDefaultContentNulls();
779+
}
780+
}
781+
}
782+
if (needMerge || (valueNulls == null) || (contentNulls == null)) {
783+
JsonSetter.Value setterInfo = config.getDefaultSetterInfo();
784+
if (valueNulls == null) {
785+
valueNulls = setterInfo.nonDefaultValueNulls();
786+
}
787+
if (contentNulls == null) {
788+
contentNulls = setterInfo.nonDefaultContentNulls();
789+
}
790+
}
791+
if ((valueNulls != null) || (contentNulls != null)) {
792+
metadata = metadata.withNulls(valueNulls, contentNulls);
793+
}
794+
return metadata;
795+
}
796+
723797
/**
724798
* Helper method that can be used to see if specified property is annotated
725799
* to indicate use of a converter for property value (in case of container types,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fasterxml.jackson.databind.deser.filter;
2+
3+
import java.util.Map;
4+
5+
import com.fasterxml.jackson.annotation.*;
6+
7+
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.InvalidNullException;
9+
10+
// [databind#4200]: Nulls.FAIL not taken into account with DELEGATING creator
11+
public class NullConversionsForContent4200Test extends BaseMapTest
12+
{
13+
static class DelegatingWrapper4200 {
14+
private final Map<String, String> value;
15+
16+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
17+
DelegatingWrapper4200(@JsonSetter(contentNulls = Nulls.FAIL)
18+
Map<String, String> value)
19+
{
20+
this.value = value;
21+
}
22+
23+
public Map<String, String> getValue() {
24+
return value;
25+
}
26+
}
27+
28+
static class SetterWrapper4200 {
29+
private Map<String, String> value;
30+
31+
public Map<String, String> getValue() {
32+
return value;
33+
}
34+
35+
@JsonSetter(contentNulls = Nulls.FAIL)
36+
public void setValue(Map<String, String> value) {
37+
this.value = value;
38+
}
39+
}
40+
41+
private final ObjectMapper MAPPER = newJsonMapper();
42+
43+
public void testDelegatingCreatorNulls4200() throws Exception
44+
{
45+
try {
46+
MAPPER.readValue(a2q("{'foo': null}"), DelegatingWrapper4200.class);
47+
fail("Should not pass");
48+
} catch (InvalidNullException e) {
49+
verifyException(e, "Invalid `null` value");
50+
}
51+
}
52+
53+
public void testSetterNulls4200() throws Exception
54+
{
55+
try {
56+
MAPPER.readValue(a2q("{'value':{'foo': null}}"),
57+
SetterWrapper4200.class);
58+
fail("Should not pass");
59+
} catch (InvalidNullException e) {
60+
verifyException(e, "Invalid `null` value");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)