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 all 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
3 changes: 3 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1834,3 +1834,6 @@ Rikkarth (rikkarth@github)
* Contributed #4709: Add `JacksonCollectors` with `toArrayNode()` implementation
(2.18.0)

Maxim Valeev (@MaximValeev)
* Reported #4508: Deserialized JsonAnySetter field in Kotlin data class is null
(2.18.1)
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
(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 @@ -135,10 +135,24 @@ public PropertyValueBuffer(JsonParser p, DeserializationContext ctxt, int paramC
*/
public final boolean hasParameter(SettableBeanProperty prop)
{
final int ix = prop.getCreatorIndex();

if (_paramsSeenBig == null) {
return ((_paramsSeen >> prop.getCreatorIndex()) & 1) == 1;
if (((_paramsSeen >> ix) & 1) == 1) {
return true;
}
} else {
if (_paramsSeenBig.get(ix)) {
return true;
}
}
return _paramsSeenBig.get(prop.getCreatorIndex());
// 28-Sep-2024 : [databind#4508] Support any-setter flowing through creator
if (_anyParamSetter != null) {
if (ix == _anyParamSetter.getParameterIndex()) {
return true;
}
}
return false;
}

/**
Expand All @@ -160,6 +174,11 @@ 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())) {
value = _createAndSetAnySetterValue();
}
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 +217,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 +232,27 @@ public Object[] getParameters(SettableBeanProperty[] props)
return _creatorParameters;
}

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

// Since one of callers only exposes JsonMappingException, but pv.setValue()
// nominally leaks IOException, need to do this unfortunate conversion
} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw JsonMappingException.fromUnexpectedIOE(e);
}
}
return anySetterParameterObject;
}

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