Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 15, 2025
2 parents 20ad657 + b56139e commit 35143ed
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public DataA deserialize(JsonParser p, DeserializationContext ctxt)
}
/*JsonNode node =*/ p.readValueAsTree();

p.skipChildren(); // important, must consume input
DataA da = new DataA();
da.i = 5;
return da;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public BogusBeanDeserializer(String a, String b) {
}

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
public Object deserialize(JsonParser p, DeserializationContext ctxt)
{
p.skipChildren();
return new Bean(a, b);
}
}
Expand Down Expand Up @@ -247,8 +248,9 @@ static class ArrayDeserializerModifier extends ValueDeserializerModifier {
public ValueDeserializer<?> modifyArrayDeserializer(DeserializationConfig config, ArrayType valueType,
BeanDescription beanDesc, ValueDeserializer<?> deserializer) {
return (ValueDeserializer<?>) new StdDeserializer<Object>(Object.class) {
@Override public Object deserialize(JsonParser jp,
@Override public Object deserialize(JsonParser p,
DeserializationContext ctxt) {
p.skipChildren();
return new String[] { "foo" };
}
};
Expand All @@ -260,8 +262,9 @@ static class CollectionDeserializerModifier extends ValueDeserializerModifier {
public ValueDeserializer<?> modifyCollectionDeserializer(DeserializationConfig config, CollectionType valueType,
BeanDescription beanDesc, ValueDeserializer<?> deserializer) {
return (ValueDeserializer<?>) new StdDeserializer<Object>(Object.class) {
@Override public Object deserialize(JsonParser jp,
@Override public Object deserialize(JsonParser p,
DeserializationContext ctxt) {
p.skipChildren();
ArrayList<String> list = new ArrayList<String>();
list.add("foo");
return list;
Expand All @@ -275,8 +278,9 @@ static class MapDeserializerModifier extends ValueDeserializerModifier {
public ValueDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType valueType,
BeanDescription beanDesc, ValueDeserializer<?> deserializer) {
return (ValueDeserializer<?>) new StdDeserializer<Object>(Object.class) {
@Override public Object deserialize(JsonParser jp,
@Override public Object deserialize(JsonParser p,
DeserializationContext ctxt) {
p.skipChildren();
HashMap<String,String> map = new HashMap<String,String>();
map.put("a", "foo");
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public Object handleUnexpectedToken(DeserializationContext ctxt,
JavaType targetType, JsonToken t, JsonParser p,
String failureMsg)
{
p.skipChildren();
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import tools.jackson.databind.*;
import tools.jackson.databind.exc.MismatchedInputException;
import tools.jackson.databind.util.ClassUtil;
import tools.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.*;

import static tools.jackson.databind.testutil.DatabindTestUtil.*;

/**
* Unit tests for verifying handling of simple basic non-structured
* types; primitives (and/or their wrappers), Strings.
*/
public class JDKScalarsDeserTest
extends DatabindTestUtil
{
private final static String NAN_STRING = "NaN";

Expand Down Expand Up @@ -548,7 +548,7 @@ public void testBase64Variants() throws Exception

/**
* Then a unit test to verify that we can conveniently bind sequence of
* space-separate simple values
* space-separated simple values
*/
@Test
public void testSequenceOfInts() throws Exception
Expand All @@ -560,12 +560,15 @@ public void testSequenceOfInts() throws Exception
sb.append(" ");
sb.append(i);
}
JsonParser p = MAPPER.createParser(sb.toString());
for (int i = 0; i < NR_OF_INTS; ++i) {
Integer result = MAPPER.readValue(p, Integer.class);
assertEquals(Integer.valueOf(i), result);
ObjectMapper mapper = jsonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();
try (JsonParser p = mapper.createParser(sb.toString())) {
for (int i = 0; i < NR_OF_INTS; ++i) {
Integer result = mapper.readValue(p, Integer.class);
assertEquals(Integer.valueOf(i), result);
}
}
p.close();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.Test;

import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -299,8 +300,9 @@ private void assertLocale(Locale expected, Locale actual) {
@Test
public void testLocaleFuzz47034() throws Exception
{
Locale loc = MAPPER.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47034.json"),
Locale.class);
Locale loc = MAPPER.readerFor(Locale.class)
.without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47034.json"));
assertNotNull(loc);
}

Expand All @@ -309,8 +311,9 @@ public void testLocaleFuzz47034() throws Exception
@Test
public void testLocaleFuzz47036() throws Exception
{
Locale loc = MAPPER.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47036.json"),
Locale.class);
Locale loc = MAPPER.readerFor(Locale.class)
.without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47036.json"));
assertNotNull(loc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
import tools.jackson.databind.*;
import tools.jackson.databind.exc.MismatchedInputException;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.*;

import static tools.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
import static tools.jackson.databind.testutil.DatabindTestUtil.verifyException;

/**
* Test for validating {@link tools.jackson.databind.DeserializationFeature#FAIL_ON_TRAILING_TOKENS}.
*/
public class FullStreamReadTest
public class FullStreamReadTest extends DatabindTestUtil
{
private final static String JSON_OK_ARRAY = " [ 1, 2, 3] ";
private final static String JSON_OK_ARRAY_WITH_COMMENT = JSON_OK_ARRAY + " // stuff ";
Expand All @@ -40,27 +38,31 @@ public class FullStreamReadTest
@Test
public void testMapperAcceptTrailing()
{
assertFalse(MAPPER.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));
ObjectMapper mapper = jsonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();

assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));

// by default, should be ok to read, all
_verifyArray(MAPPER.readTree(JSON_OK_ARRAY));
_verifyArray(MAPPER.readTree(JSON_OK_ARRAY_WITH_COMMENT));
_verifyArray(MAPPER.readTree(JSON_FAIL_ARRAY));
_verifyArray(mapper.readTree(JSON_OK_ARRAY));
_verifyArray(mapper.readTree(JSON_OK_ARRAY_WITH_COMMENT));
_verifyArray(mapper.readTree(JSON_FAIL_ARRAY));

// and also via "untyped"
_verifyCollection(MAPPER.readValue(JSON_OK_ARRAY, List.class));
_verifyCollection(MAPPER.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class));
_verifyCollection(MAPPER.readValue(JSON_FAIL_ARRAY, List.class));
_verifyCollection(mapper.readValue(JSON_OK_ARRAY, List.class));
_verifyCollection(mapper.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class));
_verifyCollection(mapper.readValue(JSON_FAIL_ARRAY, List.class));

// ditto for getting `null` and some other token

assertTrue(MAPPER.readTree(JSON_OK_NULL).isNull());
assertTrue(MAPPER.readTree(JSON_OK_NULL_WITH_COMMENT).isNull());
assertTrue(MAPPER.readTree(JSON_FAIL_NULL).isNull());
assertTrue(mapper.readTree(JSON_OK_NULL).isNull());
assertTrue(mapper.readTree(JSON_OK_NULL_WITH_COMMENT).isNull());
assertTrue(mapper.readTree(JSON_FAIL_NULL).isNull());

assertNull(MAPPER.readValue(JSON_OK_NULL, Object.class));
assertNull(MAPPER.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class));
assertNull(MAPPER.readValue(JSON_FAIL_NULL, Object.class));
assertNull(mapper.readValue(JSON_OK_NULL, Object.class));
assertNull(mapper.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class));
assertNull(mapper.readValue(JSON_FAIL_NULL, Object.class));
}

@Test
Expand Down Expand Up @@ -176,8 +178,8 @@ public void testMapperFailOnTrailingWithNull()
@Test
public void testReaderAcceptTrailing()
{
ObjectReader R = MAPPER.reader();
assertFalse(R.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));
ObjectReader R = MAPPER.reader()
.without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);

_verifyArray(R.readTree(JSON_OK_ARRAY));
_verifyArray(R.readTree(JSON_OK_ARRAY_WITH_COMMENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ static class Test3787Bean {
static class Deserializer3787A extends ValueDeserializer<Test3787Bean> {
@Override
public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) {
p.skipChildren(); // important to consume value
Test3787Bean simpleTestBean = new Test3787Bean();
simpleTestBean.value = "I am A";
return simpleTestBean;
Expand All @@ -177,6 +178,7 @@ public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) {
static class Deserializer3787B extends ValueDeserializer<Test3787Bean> {
@Override
public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) {
p.skipChildren(); // important to consume value
Test3787Bean simpleTestBean = new Test3787Bean();
simpleTestBean.value = "I am B";
return simpleTestBean;
Expand Down

0 comments on commit 35143ed

Please sign in to comment.