Skip to content

Commit

Permalink
Fix #4200: use annotations for delegating @JsonCreator (#4228)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Nov 28, 2023
1 parent 8371ce1 commit 8fc618b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project: jackson-databind

2.17.0 (not yet released)

#4200: `JsonSetter(contentNulls = FAIL)` is ignored in delegating
`@JsonCreator` argument
#4205: Consider types in `sun.*` package(s) to be JDK (platform) types
for purposes of handling
#4209: Make `BeanDeserializerModifier`/`BeanSerializerModifier`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.JsonParser.NumberType;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.deser.impl.*;
import com.fasterxml.jackson.databind.deser.std.StdDelegatingDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
Expand Down Expand Up @@ -695,12 +696,29 @@ protected void _replaceProperty(BeanPropertyMap props, SettableBeanProperty[] cr

@SuppressWarnings("unchecked")
private JsonDeserializer<Object> _findDelegateDeserializer(DeserializationContext ctxt,
JavaType delegateType, AnnotatedWithParams delegateCreator) throws JsonMappingException
JavaType delegateType, AnnotatedWithParams delegateCreator)
throws JsonMappingException
{
// Need to create a temporary property to allow contextual deserializers:
BeanProperty.Std property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
delegateType, null, delegateCreator,
PropertyMetadata.STD_OPTIONAL);
// 27-Nov-2023, tatu: [databind#4200] Need to resolve PropertyMetadata.
// And all we have is the actual Creator method; but for annotations
// we actually need the one parameter -- if there is one
// (NOTE! This would not work for case of more than one parameter with
// delegation, others injected)
final BeanProperty property;

if ((delegateCreator != null) && (delegateCreator.getParameterCount() == 1)) {
AnnotatedMember delegator = delegateCreator.getParameter(0);
PropertyMetadata propMd = _getSetterInfo(ctxt, delegator, delegateType);
property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
delegateType, null, delegator, propMd);
} else {
// No creator indicated; or Zero, or more than 2 arguments (since we don't
// know which one is the "real" delegating parameter. Although could possibly
// figure it out if someone provides actual use case
property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
delegateType, null, delegateCreator,
PropertyMetadata.STD_OPTIONAL);
}
TypeDeserializer td = delegateType.getTypeHandler();
if (td == null) {
td = ctxt.getConfig().findTypeDeserializer(delegateType);
Expand All @@ -720,6 +738,62 @@ private JsonDeserializer<Object> _findDelegateDeserializer(DeserializationContex
return dd;
}

/**
* Method essentially copied from {@code BasicDeserializerFactory},
* needed to find {@link PropertyMetadata} for Delegating Creator,
* for access to annotation-derived info.
*
* @since 2.17
*/
protected PropertyMetadata _getSetterInfo(DeserializationContext ctxt,
AnnotatedMember accessor, JavaType type)
{
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
final DeserializationConfig config = ctxt.getConfig();

PropertyMetadata metadata = PropertyMetadata.STD_OPTIONAL;
boolean needMerge = true;
Nulls valueNulls = null;
Nulls contentNulls = null;

// NOTE: compared to `POJOPropertyBuilder`, we only have access to creator
// parameter, not other accessors, so code bit simpler
// Ok, first: does property itself have something to say?
if (intr != null) {
JsonSetter.Value setterInfo = intr.findSetterInfo(accessor);
if (setterInfo != null) {
valueNulls = setterInfo.nonDefaultValueNulls();
contentNulls = setterInfo.nonDefaultContentNulls();
}
}
// If not, config override?
if (needMerge || (valueNulls == null) || (contentNulls == null)) {
ConfigOverride co = config.getConfigOverride(type.getRawClass());
JsonSetter.Value setterInfo = co.getSetterInfo();
if (setterInfo != null) {
if (valueNulls == null) {
valueNulls = setterInfo.nonDefaultValueNulls();
}
if (contentNulls == null) {
contentNulls = setterInfo.nonDefaultContentNulls();
}
}
}
if (needMerge || (valueNulls == null) || (contentNulls == null)) {
JsonSetter.Value setterInfo = config.getDefaultSetterInfo();
if (valueNulls == null) {
valueNulls = setterInfo.nonDefaultValueNulls();
}
if (contentNulls == null) {
contentNulls = setterInfo.nonDefaultContentNulls();
}
}
if ((valueNulls != null) || (contentNulls != null)) {
metadata = metadata.withNulls(valueNulls, contentNulls);
}
return metadata;
}

/**
* Helper method that can be used to see if specified property is annotated
* to indicate use of a converter for property value (in case of container types,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.deser.filter;

import java.util.Map;

Expand Down

0 comments on commit 8fc618b

Please sign in to comment.