Skip to content

Commit

Permalink
Try to merge #4271 (for #1467) to master; problem with lack of acce…
Browse files Browse the repository at this point in the history
…ss to DeserializationContext
  • Loading branch information
cowtowncoder committed Nov 13, 2024
1 parent 181ddd2 commit 0c6797d
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 62 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,10 @@ Mike Minicki (@martel)
* Reported #4788: `EnumFeature.WRITE_ENUMS_TO_LOWERCASE` overrides `@JsonProperty` values
(2.18.2)

Liam Feid (@fxshlein)
* Contributed #1467: Support `@JsonUnwrapped` with `@JsonCreator`
(2.19.0)

@SandeepGaur2016
* Contributed fix for #2461: Nested `@JsonUnwrapped` property names not correctly handled
(2.19.0)
Expand Down
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.19.0 (not yet released)

#1467: Support `@JsonUnwrapped` with `@JsonCreator`
(implementation by Liam F)
#2461: Nested `@JsonUnwrapped` property names not correctly handled
(reported by @plovell)
(fix contributed by @SandeepGaur2016)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tools.jackson.databind.cfg.*;
import tools.jackson.databind.deser.bean.CreatorCandidate;
import tools.jackson.databind.deser.bean.CreatorCollector;
import tools.jackson.databind.deser.impl.UnwrappedPropertyHandler;
import tools.jackson.databind.deser.jackson.JsonNodeDeserializer;
import tools.jackson.databind.deser.jackson.TokenBufferDeserializer;
import tools.jackson.databind.deser.jdk.*;
Expand Down Expand Up @@ -48,12 +49,6 @@ public abstract class BasicDeserializerFactory
private final static Class<?> CLASS_MAP_ENTRY = Map.Entry.class;
private final static Class<?> CLASS_SERIALIZABLE = Serializable.class;

/**
* We need a placeholder for creator properties that don't have name
* but are marked with `@JsonWrapped` annotation.
*/
protected final static PropertyName UNWRAPPED_CREATOR_PARAM_NAME = new PropertyName("@JsonUnwrapped");

/*
/**********************************************************************
/* Config
Expand Down Expand Up @@ -363,11 +358,8 @@ private void _addImplicitDelegatingConstructors(DeserializationContext ctxt,
}
NameTransformer unwrapper = intr.findUnwrappingNameTransformer(config, param);
if (unwrapper != null) {
_reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
/*
properties[i] = constructCreatorProperty(ctxt, beanDesc, UNWRAPPED_CREATOR_PARAM_NAME, i, param, null);
++explicitNameCount;
*/
properties[i] = constructCreatorProperty(ctxt, beanDesc,
UnwrappedPropertyHandler.creatorParamName(i), i, param, null);
}
}

Expand Down Expand Up @@ -489,7 +481,8 @@ private void _addSelectedPropertiesBasedCreator(DeserializationContext ctxt,
// as that will not work with Creators well at all
NameTransformer unwrapper = intr.findUnwrappingNameTransformer(config, param);
if (unwrapper != null) {
_reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
properties[i] = constructCreatorProperty(ctxt, beanDesc,
UnwrappedPropertyHandler.creatorParamName(i), i, param, null);
}
// Must be injectable or have name; without either won't work
if ((name == null) && (injectId == null)) {
Expand Down Expand Up @@ -556,16 +549,6 @@ private boolean _handleSingleArgumentCreator(CreatorCollector creators,
return false;
}

// 01-Dec-2016, tatu: As per [databind#265] we cannot yet support passing
// of unwrapped values through creator properties, so fail fast
private void _reportUnwrappedCreatorProperty(DeserializationContext ctxt,
BeanDescription beanDesc, AnnotatedParameter param)
{
ctxt.reportBadTypeDefinition(beanDesc,
"Cannot define Creator parameter %d as `@JsonUnwrapped`: combination not yet supported",
param.getIndex());
}

/**
* Method that will construct a property object that represents
* a logical property passed via Creator (constructor or static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tools.jackson.databind.jsontype.TypeDeserializer;
import tools.jackson.databind.util.Annotations;
import tools.jackson.databind.util.ClassUtil;
import tools.jackson.databind.util.NameTransformer;
import tools.jackson.databind.util.ViewMatcher;

/**
Expand Down Expand Up @@ -577,6 +578,27 @@ public final Object deserializeWith(JsonParser p, DeserializationContext ctxt,
return value;
}

/**
* Returns a copy of this property, unwrapped using given {@link NameTransformer}.
*
* @since 2.19
*/
public SettableBeanProperty unwrapped(DeserializationContext ctxt, NameTransformer xf)
{
String newName = xf.transform(getName());
SettableBeanProperty renamed = withSimpleName(newName);
ValueDeserializer<?> deser = renamed.getValueDeserializer();
if (deser != null) {
@SuppressWarnings("unchecked")
ValueDeserializer<Object> newDeser = (ValueDeserializer<Object>)
deser.unwrappingDeserializer(ctxt, xf);
if (newDeser != deser) {
renamed = renamed.withValueDeserializer(newDeser);
}
}
return renamed;
}

/*
/**********************************************************************
/* Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,11 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, Deseri
}
}

// We could still have some not-yet-set creator properties that are unwrapped.
// These have to be processed last, because 'tokens' contains all properties
// that remain after regular deserialization.
buffer = _unwrappedPropertyHandler.processUnwrappedCreatorProperties(p, ctxt, buffer, tokens);

// We hit END_OBJECT, so:
Object bean;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ protected BeanDeserializerBase(BeanDeserializerBase src,
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_arrayDelegateDeserializer = src._arrayDelegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;

_backRefs = src._backRefs;
_ignorableProps = src._ignorableProps;
Expand All @@ -319,9 +318,24 @@ protected BeanDeserializerBase(BeanDeserializerBase src,
_objectIdReader = src._objectIdReader;

_nonStandardCreation = src._nonStandardCreation;
UnwrappedPropertyHandler uph = src._unwrappedPropertyHandler;
PropertyBasedCreator pbc = src._propertyBasedCreator;

_unwrappedPropertyHandler = unwrapHandler;
_beanProperties = renamedProperties;
if (unwrapHandler != null) {
// delegate further unwraps, if any
if (uph != null) { // got handler, delegate
uph = uph.renameAll(unwrapHandler);
}
// and handle direct unwrapping as well:
if (pbc != null) {
pbc = pbc.renameAll(unwrapHandler);
}
_beanProperties = src._beanProperties.renameAll(unwrapHandler);
} else {
_beanProperties = src._beanProperties;
}
_propertyBasedCreator = pbc;
_unwrappedPropertyHandler = uph;
_needViewProcesing = src._needViewProcesing;
_serializationShape = src._serializationShape;

Expand Down Expand Up @@ -550,7 +564,13 @@ public void resolve(DeserializationContext ctxt)
if (unwrapped == null) {
unwrapped = new UnwrappedPropertyHandler();
}
unwrapped.addProperty(prop);

if (prop instanceof CreatorProperty) {
unwrapped.addCreatorProperty(prop);
} else {
unwrapped.addProperty(prop);
}

// 12-Dec-2014, tatu: As per [databind#647], we will have problems if
// the original property is left in place. So let's remove it now.
// 25-Mar-2017, tatu: Wonder if this could be problematic wrt creators?
Expand Down Expand Up @@ -973,13 +993,6 @@ protected NameTransformer _findPropertyUnwrapper(DeserializationContext ctxt,
NameTransformer unwrapper = ctxt.getAnnotationIntrospector().findUnwrappingNameTransformer(
ctxt.getConfig(), am);
if (unwrapper != null) {
// 01-Dec-2016, tatu: As per [databind#265] we cannot yet support passing
// of unwrapped values through creator properties, so fail fast
if (prop instanceof CreatorProperty) {
ctxt.reportBadDefinition(getValueType(), String.format(
"Cannot define Creator property \"%s\" as `@JsonUnwrapped`: combination not yet supported",
prop.getName()));
}
return unwrapper;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tools.jackson.databind.deser.SettableBeanProperty;
import tools.jackson.databind.deser.ValueInstantiator;
import tools.jackson.databind.deser.impl.ObjectIdReader;
import tools.jackson.databind.util.NameTransformer;

/**
* Object that is used to collect arguments for non-default creator
Expand Down Expand Up @@ -92,6 +93,19 @@ protected PropertyBasedCreator(DeserializationContext ctxt,
}
}

/**
* @since 2.19
*/
protected PropertyBasedCreator(PropertyBasedCreator base,
HashMap<String, SettableBeanProperty> propertyLookup,
SettableBeanProperty[] allProperties)
{
_propertyCount = base._propertyCount;
_valueInstantiator = base._valueInstantiator;
_propertyLookup = propertyLookup;
_propertiesInOrder = allProperties;
}

/**
* Factory method used for building actual instances to be used with POJOS:
* resolves deserializers, checks for "null values".
Expand Down Expand Up @@ -143,6 +157,47 @@ public static PropertyBasedCreator construct(DeserializationContext ctxt,
caseInsensitive, false);
}

/**
* Mutant factory method for constructing a map where the names of all properties
* are transformed using the given {@link NameTransformer}.
*
* @since 2.19
*/
public PropertyBasedCreator renameAll(DeserializationContext ctxt,
NameTransformer transformer)
{
if (transformer == null || (transformer == NameTransformer.NOP)) {
return this;
}

final int len = _propertiesInOrder.length;
HashMap<String, SettableBeanProperty> newLookup = new HashMap<>(_propertyLookup);
List<SettableBeanProperty> newProps = new ArrayList<>(len);

for (SettableBeanProperty prop : _propertiesInOrder) {
if (prop == null) {
newProps.add(null);
continue;
}

SettableBeanProperty renamedProperty = prop.unwrapped(ctxt, transformer);
String oldName = prop.getName();
String newName = renamedProperty.getName();

newProps.add(renamedProperty);

if (!oldName.equals(newName) && newLookup.containsKey(oldName)) {
newLookup.remove(oldName);
newLookup.put(newName, renamedProperty);
}
}

return new PropertyBasedCreator(this,
newLookup,
newProps.toArray(new SettableBeanProperty[0])
);
}

/*
/**********************************************************************
/* Accessors
Expand Down
Loading

0 comments on commit 0c6797d

Please sign in to comment.