Skip to content

Commit 2f60d39

Browse files
committed
Fix #3275
1 parent b447e39 commit 2f60d39

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

release-notes/CREDITS-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -1436,3 +1436,9 @@ Taylor S Marks (TaylorSMarks@github)
14361436
Spence Nace (snace98@github)
14371437
* Contributed fix for #2816: Optimize UntypedObjectDeserializer wrt recursion
14381438
(2.13.3)
1439+
1440+
Jason Harper (jsharper@github)
1441+
* Reported #3275: JDK 16 Illegal reflective access for `Throwable.setCause()` with
1442+
`PropertyNamingStrategy.UPPER_CAMEL_CASE`
1443+
(2.13.4)
1444+

release-notes/VERSION-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.13.4 (not yet released)
8+
9+
#3275: JDK 16 Illegal reflective access for `Throwable.setCause()` with
10+
`PropertyNamingStrategy.UPPER_CAMEL_CASE`
11+
(reported by Jason H)
12+
713
2.13.3 (14-May-2022)
814

915
#3412: Version 2.13.2 uses `Method.getParameterCount()` which is not supported on

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ public JsonDeserializer<Object> buildThrowableDeserializer(DeserializationContex
424424

425425
// But then let's decorate things a bit
426426
// Need to add "initCause" as setter for exceptions (sub-classes of Throwable).
427+
// 26-May-2022, tatu: [databind#3275] Looks like JDK 12 added "setCause()"
428+
// which can wreak havoc, at least with NamingStrategy
429+
Iterator<SettableBeanProperty> it = builder.getProperties();
430+
while (it.hasNext()) {
431+
SettableBeanProperty prop = it.next();
432+
if ("setCause".equals(prop.getMember().getName())) {
433+
// For now this is allowed as we are returned "live" Iterator...
434+
it.remove();
435+
break;
436+
}
437+
}
427438
AnnotatedMethod am = beanDesc.findMethod("initCause", INIT_CAUSE_PARAMS);
428439
if (am != null) { // should never be null
429440
SimpleBeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(ctxt.getConfig(), am,
@@ -453,10 +464,9 @@ public JsonDeserializer<Object> buildThrowableDeserializer(DeserializationContex
453464
}
454465
}
455466
JsonDeserializer<?> deserializer = builder.build();
456-
457-
/* At this point it ought to be a BeanDeserializer; if not, must assume
458-
* it's some other thing that can handle deserialization ok...
459-
*/
467+
468+
// At this point it ought to be a BeanDeserializer; if not, must assume
469+
// it's some other thing that can handle deserialization ok...
460470
if (deserializer instanceof BeanDeserializer) {
461471
deserializer = new ThrowableDeserializer((BeanDeserializer) deserializer);
462472
}

src/test/java/com/fasterxml/jackson/databind/exc/ExceptionDeserializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void testLineNumberAsString() throws IOException
235235
assertNotNull(exc);
236236
}
237237

238-
// [databind#1842]:
238+
// [databind#1842]
239239
public void testNullAsMessage() throws IOException
240240
{
241241
Exception exc = MAPPER.readValue(a2q(

src/test/java/com/fasterxml/jackson/databind/exc/ExceptionSerializationTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.core.JsonParser;
99

1010
import com.fasterxml.jackson.databind.*;
11+
import com.fasterxml.jackson.databind.json.JsonMapper;
1112

1213
/**
1314
* Unit tests for verifying that simple exceptions can be serialized.
@@ -134,4 +135,15 @@ public void testDatabindExceptionSerialization() throws IOException {
134135
fail("Exception should contain '"+MATCH+"', does not: '"+msg+"'");
135136
}
136137
}
138+
139+
// [databind#3275]
140+
public void testSerializeWithNamingStrategy() throws IOException {
141+
final ObjectMapper mapper = JsonMapper.builder()
142+
.propertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE)
143+
.build();
144+
String json = mapper.writeValueAsString(new Exception("message!"));
145+
Map<?,?> map = mapper.readValue(json, Map.class);
146+
assertEquals(new HashSet<>(Arrays.asList("Cause", "StackTrace", "Message", "Suppressed", "LocalizedMessage")),
147+
map.keySet());
148+
}
137149
}

0 commit comments

Comments
 (0)