Skip to content

Commit d8d0714

Browse files
committed
Merge branch '2.18' into 2.19
2 parents fa1ce1f + 7d187c9 commit d8d0714

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

release-notes/VERSION-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Project: jackson-databind
3232
#4773: `SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS` should not apply to Maps
3333
with uncomparable keys
3434
(requested by @nathanukey)
35+
36+
2.18.3 (not yet released)
37+
38+
#4827: Subclassed Throwable deserialization fails since v2.18.0 - no creator
39+
index for property 'cause'
3540
(fix by Joo-Hyuk K)
3641

3742
2.18.2 (27-Nov-2024)

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

+21-14
Original file line numberDiff line numberDiff line change
@@ -439,20 +439,27 @@ public JsonDeserializer<Object> buildThrowableDeserializer(DeserializationContex
439439
}
440440
AnnotatedMethod am = beanDesc.findMethod("initCause", INIT_CAUSE_PARAMS);
441441
if (am != null) { // should never be null
442-
// [databind#3497]: must consider possible PropertyNamingStrategy
443-
String name = "cause";
444-
PropertyNamingStrategy pts = config.getPropertyNamingStrategy();
445-
if (pts != null) {
446-
name = pts.nameForSetterMethod(config, am, "cause");
447-
}
448-
SimpleBeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(ctxt.getConfig(), am,
449-
new PropertyName(name));
450-
SettableBeanProperty prop = constructSettableProperty(ctxt, beanDesc, propDef,
451-
am.getParameterType(0));
452-
if (prop != null) {
453-
// 21-Aug-2011, tatus: We may actually have found 'cause' property
454-
// to set... but let's replace it just in case, otherwise can end up with odd errors.
455-
builder.addOrReplaceProperty(prop, true);
442+
SettableBeanProperty causeCreatorProp = builder.findProperty(PropertyName.construct("cause"));
443+
// [databind#4827] : Consider case where sub-classed `Exception` has `JsonCreator` with `cause` parameter
444+
if (causeCreatorProp instanceof CreatorProperty) {
445+
// Set fallback-setter as null, so `fixAccess()` does not happen during build
446+
((CreatorProperty) causeCreatorProp).setFallbackSetter(null);
447+
} else {
448+
// [databind#3497]: must consider possible PropertyNamingStrategy
449+
String name = "cause";
450+
PropertyNamingStrategy pts = config.getPropertyNamingStrategy();
451+
if (pts != null) {
452+
name = pts.nameForSetterMethod(config, am, "cause");
453+
}
454+
SimpleBeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(ctxt.getConfig(), am,
455+
new PropertyName(name));
456+
SettableBeanProperty prop = constructSettableProperty(ctxt, beanDesc, propDef,
457+
am.getParameterType(0));
458+
if (prop != null) {
459+
// 21-Aug-2011, tatus: We may actually have found 'cause' property
460+
// to set... but let's replace it just in case, otherwise can end up with odd errors.
461+
builder.addOrReplaceProperty(prop, true);
462+
}
456463
}
457464
}
458465
// update builder now that all information is in?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fasterxml.jackson.databind.exc;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.fasterxml.jackson.databind.DeserializationFeature;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
// [databind#4827] Subclassed Throwable deserialization fails since v2.18.0
14+
// no creator index for property 'cause'
15+
public class SubclassedThrowableDeserialization4827Test
16+
extends DatabindTestUtil
17+
{
18+
19+
public static class SubclassedExceptionJava extends Exception {
20+
@JsonCreator
21+
public SubclassedExceptionJava(
22+
@JsonProperty("message") String message,
23+
@JsonProperty("cause") Throwable cause
24+
) {
25+
super(message, cause);
26+
}
27+
}
28+
29+
private final ObjectMapper MAPPER = jsonMapperBuilder()
30+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
31+
.build();
32+
33+
@Test
34+
public void testDeserialization()
35+
throws Exception
36+
{
37+
// Given input
38+
SubclassedExceptionJava input = new SubclassedExceptionJava(
39+
"Test Message", new RuntimeException("test runtime cause"));
40+
41+
// When serialize, then deserialize, round-trip
42+
String serialized = MAPPER.writeValueAsString(input);
43+
SubclassedExceptionJava deserialized = MAPPER.readValue(serialized, SubclassedExceptionJava.class);
44+
45+
// Contents are same
46+
assertEquals(input.getMessage(), deserialized.getMessage());
47+
assertEquals(input.getCause().getMessage(), deserialized.getCause().getMessage());
48+
}
49+
}

0 commit comments

Comments
 (0)