Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add any-setter handling in PropertyValueBuffer post #562 #4720

Merged
merged 6 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Project: jackson-databind
=== Releases ===
------------------------------------------------------------------------

2.18.1 (WIP-2024)

#4508: Deserialized JsonAnySetter field in Kotlin data class is null #4508
(reported by @MaximValeev)
(fix by Joo-Hyuk K)

2.18.0 (26-Sep-2024)

#562: Allow `@JsonAnySetter` to flow through Creators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.SettableAnyProperty;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;

/**
Expand Down Expand Up @@ -135,6 +137,12 @@ public PropertyValueBuffer(JsonParser p, DeserializationContext ctxt, int paramC
*/
public final boolean hasParameter(SettableBeanProperty prop)
{
// 28-Sep-2024 : [databind#4508] Support any-setter flowing through creator
if (_anyParamSetter != null) {
if (prop.getCreatorIndex() == _anyParamSetter.getParameterIndex()) {
return true;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the most optimal way of checking, any idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me rewrite it slightly, considering that expectation is that match (true) is more likely than non-match.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite looks cleaner, thanks!

if (_paramsSeenBig == null) {
return ((_paramsSeen >> prop.getCreatorIndex()) & 1) == 1;
}
Expand All @@ -160,6 +168,16 @@ public Object getParameter(SettableBeanProperty prop)
} else {
value = _creatorParameters[prop.getCreatorIndex()] = _findMissing(prop);
}
// 28-Sep-2024 : [databind#4508] Support any-setter flowing through creator
if ((value == null) && (_anyParamSetter != null )
&& (prop.getCreatorIndex() == _anyParamSetter.getParameterIndex())) {
try {
value = _createAndSetAnySetterValue();
} catch (IOException e) {
// do-nothing here?
throw JsonMappingException.fromUnexpectedIOE(e);
}
}
if (value == null && _context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
return _context.reportInputMismatch(prop,
"Null value for creator property '%s' (index %d); `DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES` enabled",
Expand Down Expand Up @@ -198,11 +216,7 @@ public Object[] getParameters(SettableBeanProperty[] props)
}
// [databind#562] since 2.18 : Respect @JsonAnySetter in @JsonCreator
if (_anyParamSetter != null) {
Object anySetterParameterObject = _anyParamSetter.createParameterObject();
for (PropertyValue pv = _anyParamBuffered; pv != null; pv = pv.next) {
pv.setValue(anySetterParameterObject);
}
_creatorParameters[_anyParamSetter.getParameterIndex()] = anySetterParameterObject;
_creatorParameters[_anyParamSetter.getParameterIndex()] = _createAndSetAnySetterValue();
}
if (_context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
for (int ix = 0; ix < props.length; ++ix) {
Expand All @@ -217,6 +231,18 @@ public Object[] getParameters(SettableBeanProperty[] props)
return _creatorParameters;
}

/**
* Helper method called to create and set any values buffered for "any setter"
*/
private Object _createAndSetAnySetterValue() throws IOException
{
Object anySetterParameterObject = _anyParamSetter.createParameterObject();
for (PropertyValue pv = _anyParamBuffered; pv != null; pv = pv.next) {
pv.setValue(anySetterParameterObject);
}
return anySetterParameterObject;
}

protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingException
{
// 08-Jun-2024: [databind#562] AnySetters are bit special
Expand Down