Skip to content

Commit

Permalink
Fix #3244
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 3, 2021
1 parent 810128e commit 765e2fe
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Project: jackson-databind
#3238: Add PropertyNamingStrategies.UpperSnakeCaseStrategy (and UPPER_SNAKE_CASE constant)
(requested by Kenneth J)
(contributed by Tanvesh)
#3244: StackOverflowError when serializing JsonProcessingException
(reported by saneksanek@github)
- Fix to avoid problem with `BigDecimalNode`, scale of `Integer.MIN_VALUE` (see
[dataformats-binary#264] for details)
- Extend handling of `FAIL_ON_NULL_FOR_PRIMITIVES` to cover coercion from (Empty) String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TokenStreamFactory;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;

import com.fasterxml.jackson.databind.*;
Expand All @@ -20,6 +23,7 @@
import com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer;
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
import com.fasterxml.jackson.databind.ser.std.ToEmptyObjectSerializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.fasterxml.jackson.databind.util.ClassUtil;
Expand Down Expand Up @@ -380,12 +384,15 @@ protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvid
return prov.getUnknownTypeSerializer(Object.class);
// throw new IllegalArgumentException("Cannot create bean serializer for Object.class");
}

JsonSerializer<?> ser = _findUnsupportedTypeSerializer(prov, type, beanDesc);
if (ser != null) {
return (JsonSerializer<Object>) ser;
}

// 02-Sep-2021, tatu: [databind#3244] Should not try "proper" serialization of
// things like ObjectMapper, JsonParser or JsonGenerator...
if (_isUnserializableJacksonType(prov, type)) {
return new ToEmptyObjectSerializer(type);
}
final SerializationConfig config = prov.getConfig();
BeanSerializerBuilder builder = constructBeanSerializerBuilder(beanDesc);
builder.setConfig(config);
Expand Down Expand Up @@ -854,4 +861,23 @@ protected JsonSerializer<?> _findUnsupportedTypeSerializer(SerializerProvider ct
}
return null;
}

/* Helper method used for preventing attempts to serialize various Jackson
* processor things which are not generally serializable.
*
* @since 2.13
*/
protected boolean _isUnserializableJacksonType(SerializerProvider ctxt,
JavaType type)
{
final Class<?> raw = type.getRawClass();
return ObjectMapper.class.isAssignableFrom(raw)
|| ObjectReader.class.isAssignableFrom(raw)
|| ObjectWriter.class.isAssignableFrom(raw)
|| DatabindContext.class.isAssignableFrom(raw)
|| TokenStreamFactory.class.isAssignableFrom(raw)
|| JsonParser.class.isAssignableFrom(raw)
|| JsonGenerator.class.isAssignableFrom(raw)
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.*;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.databind.*;

/**
Expand Down Expand Up @@ -37,12 +39,12 @@ public NoSerdeConstructor( String strVal ) {
}

/*
/**********************************************************
/* Tests
/**********************************************************
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final ObjectMapper MAPPER = new ObjectMapper();
private final ObjectMapper MAPPER = newJsonMapper();

public void testSimple() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fasterxml.jackson.databind.exc;

import java.util.Map;

import com.fasterxml.jackson.core.JacksonException;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonExceptionSerializationTest extends BaseMapTest
{
/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#3244]: StackOverflow for basic JsonProcessingException?
public void testIssue3244() throws Exception {
JacksonException e = null;
try {
MAPPER.readValue("{ foo ", Map.class);
fail("Should not pass");
} catch (JacksonException e0) {
e = e0;
}
String json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(e);
//System.err.println("JSON: "+json);

// Could try proper validation, but for now just ensure we won't crash
assertNotNull(json);
JsonNode n = MAPPER.readTree(json);
assertTrue(n.isObject());
}
}

0 comments on commit 765e2fe

Please sign in to comment.