Skip to content

Commit 765e2fe

Browse files
committed
Fix #3244
1 parent 810128e commit 765e2fe

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Project: jackson-databind
6969
#3238: Add PropertyNamingStrategies.UpperSnakeCaseStrategy (and UPPER_SNAKE_CASE constant)
7070
(requested by Kenneth J)
7171
(contributed by Tanvesh)
72+
#3244: StackOverflowError when serializing JsonProcessingException
73+
(reported by saneksanek@github)
7274
- Fix to avoid problem with `BigDecimalNode`, scale of `Integer.MIN_VALUE` (see
7375
[dataformats-binary#264] for details)
7476
- Extend handling of `FAIL_ON_NULL_FOR_PRIMITIVES` to cover coercion from (Empty) String

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
77
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
88
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
9+
import com.fasterxml.jackson.core.JsonGenerator;
10+
import com.fasterxml.jackson.core.JsonParser;
11+
import com.fasterxml.jackson.core.TokenStreamFactory;
912
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
1013

1114
import com.fasterxml.jackson.databind.*;
@@ -20,6 +23,7 @@
2023
import com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer;
2124
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
2225
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
26+
import com.fasterxml.jackson.databind.ser.std.ToEmptyObjectSerializer;
2327
import com.fasterxml.jackson.databind.type.ReferenceType;
2428
import com.fasterxml.jackson.databind.util.BeanUtil;
2529
import com.fasterxml.jackson.databind.util.ClassUtil;
@@ -380,12 +384,15 @@ protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvid
380384
return prov.getUnknownTypeSerializer(Object.class);
381385
// throw new IllegalArgumentException("Cannot create bean serializer for Object.class");
382386
}
383-
384387
JsonSerializer<?> ser = _findUnsupportedTypeSerializer(prov, type, beanDesc);
385388
if (ser != null) {
386389
return (JsonSerializer<Object>) ser;
387390
}
388-
391+
// 02-Sep-2021, tatu: [databind#3244] Should not try "proper" serialization of
392+
// things like ObjectMapper, JsonParser or JsonGenerator...
393+
if (_isUnserializableJacksonType(prov, type)) {
394+
return new ToEmptyObjectSerializer(type);
395+
}
389396
final SerializationConfig config = prov.getConfig();
390397
BeanSerializerBuilder builder = constructBeanSerializerBuilder(beanDesc);
391398
builder.setConfig(config);
@@ -854,4 +861,23 @@ protected JsonSerializer<?> _findUnsupportedTypeSerializer(SerializerProvider ct
854861
}
855862
return null;
856863
}
864+
865+
/* Helper method used for preventing attempts to serialize various Jackson
866+
* processor things which are not generally serializable.
867+
*
868+
* @since 2.13
869+
*/
870+
protected boolean _isUnserializableJacksonType(SerializerProvider ctxt,
871+
JavaType type)
872+
{
873+
final Class<?> raw = type.getRawClass();
874+
return ObjectMapper.class.isAssignableFrom(raw)
875+
|| ObjectReader.class.isAssignableFrom(raw)
876+
|| ObjectWriter.class.isAssignableFrom(raw)
877+
|| DatabindContext.class.isAssignableFrom(raw)
878+
|| TokenStreamFactory.class.isAssignableFrom(raw)
879+
|| JsonParser.class.isAssignableFrom(raw)
880+
|| JsonGenerator.class.isAssignableFrom(raw)
881+
;
882+
}
857883
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import java.util.*;
55

66
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7+
78
import com.fasterxml.jackson.core.JsonParser;
9+
810
import com.fasterxml.jackson.databind.*;
911

1012
/**
@@ -37,12 +39,12 @@ public NoSerdeConstructor( String strVal ) {
3739
}
3840

3941
/*
40-
/**********************************************************
41-
/* Tests
42-
/**********************************************************
42+
/**********************************************************************
43+
/* Test methods
44+
/**********************************************************************
4345
*/
4446

45-
private final ObjectMapper MAPPER = new ObjectMapper();
47+
private final ObjectMapper MAPPER = newJsonMapper();
4648

4749
public void testSimple() throws Exception
4850
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.databind.exc;
2+
3+
import java.util.Map;
4+
5+
import com.fasterxml.jackson.core.JacksonException;
6+
7+
import com.fasterxml.jackson.databind.BaseMapTest;
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
11+
public class JacksonExceptionSerializationTest extends BaseMapTest
12+
{
13+
/*
14+
/**********************************************************************
15+
/* Test methods
16+
/**********************************************************************
17+
*/
18+
19+
private final ObjectMapper MAPPER = newJsonMapper();
20+
21+
// [databind#3244]: StackOverflow for basic JsonProcessingException?
22+
public void testIssue3244() throws Exception {
23+
JacksonException e = null;
24+
try {
25+
MAPPER.readValue("{ foo ", Map.class);
26+
fail("Should not pass");
27+
} catch (JacksonException e0) {
28+
e = e0;
29+
}
30+
String json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(e);
31+
//System.err.println("JSON: "+json);
32+
33+
// Could try proper validation, but for now just ensure we won't crash
34+
assertNotNull(json);
35+
JsonNode n = MAPPER.readTree(json);
36+
assertTrue(n.isObject());
37+
}
38+
}

0 commit comments

Comments
 (0)