Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a4410b

Browse files
committedJan 15, 2025·
More work pre #3406
1 parent b56139e commit 1a4410b

16 files changed

+109
-67
lines changed
 

‎src/test/java/com/fasterxml/jackson/databind/MapperViaParserTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ public void testPojoReading() throws IOException
106106
@Test
107107
public void testIncrementalPojoReading() throws IOException
108108
{
109-
JsonFactory jf = new MappingJsonFactory();
109+
ObjectMapper mapper = JsonMapper.builder()
110+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
111+
.build();
112+
JsonFactory jf = mapper.getFactory();
110113
final String JSON = "[ 1, true, null, \"abc\" ]";
111114
JsonParser p = jf.createParser(new StringReader(JSON));
112115

‎src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ public void testUnknownFields() throws Exception
551551
ObjectMapper mapper = JsonMapper.builder().addHandler(new DeserializationProblemHandler(){
552552
@Override
553553
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
554-
p.readValueAsTree();
554+
ctxt.readTree(p);
555555
return true;
556556
}
557557
}).build();

‎src/test/java/com/fasterxml/jackson/databind/convert/UpdateViaObjectReaderTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ public DataA deserialize(JsonParser p, DeserializationContext ctxt) throws IOExc
7878
+p.currentToken());
7979
// never gets here
8080
}
81-
/*JsonNode node =*/ p.readValueAsTree();
81+
/*JsonNode node =*/ ctxt.readTree(p);
8282

83-
p.skipChildren(); // important, must consume input
8483
DataA da = new DataA();
8584
da.i = 5;
8685
return da;

‎src/test/java/com/fasterxml/jackson/databind/deser/BeanDeserializerTest.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ public Issue1912SubBean(String a) {
167167
}
168168
}
169169

170-
public static class Issue1912CustomBeanDeserializer extends JsonDeserializer<Issue1912Bean> {
170+
public static class Issue1912CustomBeanDeserializer
171+
extends JsonDeserializer<Issue1912Bean>
172+
implements ContextualDeserializer
173+
{
171174
private BeanDeserializer defaultDeserializer;
172175

173176
public Issue1912CustomBeanDeserializer(BeanDeserializer defaultDeserializer) {
@@ -176,16 +179,29 @@ public Issue1912CustomBeanDeserializer(BeanDeserializer defaultDeserializer) {
176179

177180
@Override
178181
public Issue1912Bean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
179-
// this is need on some cases, this populate _propertyBasedCreator
180-
defaultDeserializer.resolve(ctxt);
181-
182182
p.nextFieldName(); // read subBean
183-
p.nextToken(); // read start object
183+
if (p.nextToken() != JsonToken.START_OBJECT) {
184+
throw new IllegalArgumentException("Unexpected token "+p.currentToken());
185+
}
184186

185187
Issue1912SubBean subBean = (Issue1912SubBean) defaultDeserializer.findProperty("subBean").deserialize(p, ctxt);
188+
// Must also read trailing END_OBJECT
189+
if (p.nextToken() != JsonToken.END_OBJECT) {
190+
throw new IllegalArgumentException("Unexpected token "+p.currentToken());
191+
}
186192

187193
return new Issue1912Bean(subBean);
188194
}
195+
196+
@Override
197+
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
198+
throws JsonMappingException {
199+
if (defaultDeserializer instanceof ContextualDeserializer) {
200+
ContextualDeserializer cd = (ContextualDeserializer) defaultDeserializer;
201+
return new Issue1912CustomBeanDeserializer((BeanDeserializer) cd.createContextual(ctxt, property));
202+
}
203+
return this;
204+
}
189205
}
190206

191207
public static class Issue1912CustomPropertyDeserializer extends JsonDeserializer<Issue1912SubBean> {
@@ -474,8 +490,9 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
474490

475491
@Test
476492
public void testAddOrReplacePropertyIsUsedOnDeserialization() throws Exception {
477-
ObjectMapper mapper = new ObjectMapper();
478-
mapper.registerModule(new Issue1912Module());
493+
ObjectMapper mapper = jsonMapperBuilder()
494+
.addModule(new Issue1912Module())
495+
.build();
479496

480497
Issue1912Bean result = mapper.readValue("{\"subBean\": {\"a\":\"foo\"}}", Issue1912Bean.class);
481498
assertEquals("foo_custom", result.subBean.a);

‎src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializersTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ public void testCustomDeserializerWithReadTreeAsValue() throws Exception
623623
assertEquals(new Point(13, -4), result.point);
624624

625625
// also, try some edge conditions
626-
result = MAPPER.readValue(a2q("{'name':4})"), NamedPoint.class);
626+
result = MAPPER.readValue(a2q("{'name':4}"), NamedPoint.class);
627627
assertNotNull(result);
628628
assertEquals("4", result.name);
629629
assertNull(result.point);

‎src/test/java/com/fasterxml/jackson/databind/deser/DeserConcurrencyTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ static class CustomBeanDeserializer
3838
protected volatile boolean resolved = false;
3939

4040
@Override
41-
public Bean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
41+
public Bean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
4242
{
4343
if (!resolved) {
4444
throw new IOException("Deserializer not yet completely resolved");
4545
}
46+
p.skipChildren(); // consume the value
4647
Bean b = new Bean();
4748
b.value = 13;
4849
return b;

‎src/test/java/com/fasterxml/jackson/databind/deser/JacksonTypesDeserTest.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,25 @@ public void testJavaType() throws Exception
7777
public void testTokenBufferWithSample() throws Exception
7878
{
7979
// First, try standard sample doc:
80-
TokenBuffer result = MAPPER.readValue(SAMPLE_DOC_JSON_SPEC, TokenBuffer.class);
81-
verifyJsonSpecSampleDoc(result.asParser(), true);
82-
result.close();
80+
try (TokenBuffer result = MAPPER.readValue(SAMPLE_DOC_JSON_SPEC, TokenBuffer.class)) {
81+
verifyJsonSpecSampleDoc(result.asParser(), true);
82+
}
8383
}
8484

8585
@SuppressWarnings("resource")
8686
@Test
8787
public void testTokenBufferWithSequence() throws Exception
8888
{
89+
final ObjectMapper mapper = jsonMapperBuilder()
90+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
91+
.build();
92+
8993
// and then sequence of other things
90-
JsonParser jp = MAPPER.createParser("[ 32, [ 1 ], \"abc\", { \"a\" : true } ]");
91-
assertToken(JsonToken.START_ARRAY, jp.nextToken());
94+
JsonParser p = mapper.createParser("[ 32, [ 1 ], \"abc\", { \"a\" : true } ]");
95+
assertToken(JsonToken.START_ARRAY, p.nextToken());
9296

93-
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
94-
TokenBuffer buf = MAPPER.readValue(jp, TokenBuffer.class);
97+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
98+
TokenBuffer buf = mapper.readValue(p, TokenBuffer.class);
9599

96100
// check manually...
97101
JsonParser bufParser = buf.asParser();
@@ -100,7 +104,7 @@ public void testTokenBufferWithSequence() throws Exception
100104
assertNull(bufParser.nextToken());
101105

102106
// then bind to another
103-
buf = MAPPER.readValue(jp, TokenBuffer.class);
107+
buf = mapper.readValue(p, TokenBuffer.class);
104108
bufParser = buf.asParser();
105109
assertToken(JsonToken.START_ARRAY, bufParser.nextToken());
106110
assertToken(JsonToken.VALUE_NUMBER_INT, bufParser.nextToken());
@@ -109,18 +113,18 @@ public void testTokenBufferWithSequence() throws Exception
109113
assertNull(bufParser.nextToken());
110114

111115
// third one, with automatic binding
112-
buf = MAPPER.readValue(jp, TokenBuffer.class);
113-
String str = MAPPER.readValue(buf.asParser(), String.class);
116+
buf = mapper.readValue(p, TokenBuffer.class);
117+
String str = mapper.readValue(buf.asParser(), String.class);
114118
assertEquals("abc", str);
115119

116120
// and ditto for last one
117-
buf = MAPPER.readValue(jp, TokenBuffer.class);
118-
Map<?,?> map = MAPPER.readValue(buf.asParser(), Map.class);
121+
buf = mapper.readValue(p, TokenBuffer.class);
122+
Map<?,?> map = mapper.readValue(buf.asParser(), Map.class);
119123
assertEquals(1, map.size());
120124
assertEquals(Boolean.TRUE, map.get("a"));
121125

122-
assertEquals(JsonToken.END_ARRAY, jp.nextToken());
123-
assertNull(jp.nextToken());
126+
assertEquals(JsonToken.END_ARRAY, p.nextToken());
127+
assertNull(p.nextToken());
124128
}
125129

126130
// 10k does it, 5k not, but use bit higher values just in case

‎src/test/java/com/fasterxml/jackson/databind/deser/SetterlessPropertiesDeserTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ public class SetterlessPropertiesDeserTest
2424
{
2525
static class CollectionBean
2626
{
27-
List<String> _values = new ArrayList<String>();
27+
List<String> _values = new ArrayList<>();
2828

2929
public List<String> getValues() { return _values; }
3030
}
3131

3232
static class MapBean
3333
{
34-
Map<String,Integer> _values = new HashMap<String,Integer>();
34+
Map<String,Integer> _values = new HashMap<>();
3535

3636
public Map<String,Integer> getValues() { return _values; }
3737
}
3838

3939
// testing to verify that field has precedence over getter, for lists
4040
static class Dual
4141
{
42-
@JsonProperty("list") protected List<Integer> values = new ArrayList<Integer>();
42+
@JsonProperty("list") protected List<Integer> values = new ArrayList<>();
4343

4444
public Dual() { }
4545

@@ -132,7 +132,7 @@ public void testSetterlessPrecedence() throws Exception
132132
ObjectMapper m = jsonMapperBuilder()
133133
.configure(MapperFeature.USE_GETTERS_AS_SETTERS, true)
134134
.build();
135-
Dual value = m.readValue("{\"list\":[1,2,3]}, valueType)", Dual.class);
135+
Dual value = m.readValue("{\"list\":[1,2,3]}", Dual.class);
136136
assertNotNull(value);
137137
assertEquals(3, value.values.size());
138138
}

‎src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderSimpleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public void testBuilderMethodReturnMoreSpecific() throws Exception
408408
@Test
409409
public void testSelfBuilder777() throws Exception
410410
{
411-
SelfBuilder777 result = MAPPER.readValue(a2q("{'x':3}'"),
411+
SelfBuilder777 result = MAPPER.readValue(a2q("{'x':3}"),
412412
SelfBuilder777.class);
413413
assertNotNull(result);
414414
assertEquals(3, result.x);

‎src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDeserializationTest.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -285,41 +285,46 @@ public static Operation3006 forValue(final String idStr) {
285285
/**********************************************************
286286
*/
287287

288-
protected final ObjectMapper MAPPER = new ObjectMapper();
288+
protected final ObjectMapper MAPPER = newJsonMapper();
289289

290290
@Test
291291
public void testSimple() throws Exception
292292
{
293293
// First "good" case with Strings
294294
String JSON = "\"OK\" \"RULES\" null";
295-
// multiple main-level mappings, need explicit parser:
296-
JsonParser jp = MAPPER.createParser(JSON);
295+
// multiple main-level mappings, need explicit parser
296+
// (and possibly prevent validation of trailing tokens)
297+
ObjectMapper mapper = jsonMapperBuilder()
298+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
299+
.build();
300+
301+
JsonParser p = mapper.createParser(JSON);
297302

298-
assertEquals(TestEnum.OK, MAPPER.readValue(jp, TestEnum.class));
299-
assertEquals(TestEnum.RULES, MAPPER.readValue(jp, TestEnum.class));
303+
assertEquals(TestEnum.OK, mapper.readValue(p, TestEnum.class));
304+
assertEquals(TestEnum.RULES, mapper.readValue(p, TestEnum.class));
300305

301306
// should be ok; nulls are typeless; handled by mapper, not by deserializer
302-
assertNull(MAPPER.readValue(jp, TestEnum.class));
307+
assertNull(MAPPER.readValue(p, TestEnum.class));
303308

304309
// and no more content beyond that...
305-
assertFalse(jp.hasCurrentToken());
310+
assertFalse(p.hasCurrentToken());
306311

307312
// Then alternative with index (0 means first entry)
308-
assertEquals(TestEnum.JACKSON, MAPPER.readValue(" 0 ", TestEnum.class));
313+
assertEquals(TestEnum.JACKSON, mapper.readValue(" 0 ", TestEnum.class));
309314

310315
// Then error case: unrecognized value
311316
try {
312-
/*Object result =*/ MAPPER.readValue("\"NO-SUCH-VALUE\"", TestEnum.class);
317+
/*Object result =*/ mapper.readValue("\"NO-SUCH-VALUE\"", TestEnum.class);
313318
fail("Expected an exception for bogus enum value...");
314319
} catch (MismatchedInputException jex) {
315320
verifyException(jex, "not one of the values accepted for Enum class");
316321
}
317-
jp.close();
322+
p.close();
318323
}
319324

320325
/**
321-
* Enums are considered complex if they have code (and hence sub-classes)... an
322-
* example is TimeUnit
326+
* Enums are considered complex if they have code (and hence sub-classes)...
327+
* an example is TimeUnit
323328
*/
324329
@Test
325330
public void testComplexEnum() throws Exception

‎src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ public void testInstantiationExceptionHandling() throws Exception
380380
public void testMissingInstantiatorHandling() throws Exception
381381
{
382382
ObjectMapper mapper = jsonMapperBuilder()
383+
// 14-Jan-2025, tatu: Need to disable trailing tokens (for 3.0)
384+
// for this to work (handler not consuming all tokens as it should
385+
// but no time to fully fix right now)
386+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
383387
.addHandler(new MissingInstantiationHandler(new NoDefaultCtor(13)))
384388
.build();
385389
NoDefaultCtor w = mapper.readValue("{ \"x\" : true }", NoDefaultCtor.class);

‎src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,14 @@ public void testUUIDAux() throws Exception
425425
byte[] data = bytes.toByteArray();
426426
assertEquals(16, data.length);
427427

428-
buf.writeObject(data);
429-
430-
UUID value2 = MAPPER.readValue(buf.asParser(), UUID.class);
431-
432-
assertEquals(value, value2);
428+
// Let's create fresh TokenBuffer, not reuse one
429+
try (TokenBuffer buf2 = new TokenBuffer(null, false)) {
430+
buf2.writeObject(data);
431+
432+
UUID value2 = MAPPER.readValue(buf2.asParser(), UUID.class);
433+
434+
assertEquals(value, value2);
435+
}
433436
}
434437
}
435438
}

‎src/test/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
import com.fasterxml.jackson.annotation.*;
1111

12-
import com.fasterxml.jackson.core.JsonGenerator;
13-
import com.fasterxml.jackson.core.JsonParser;
12+
import com.fasterxml.jackson.core.*;
1413
import com.fasterxml.jackson.databind.*;
1514
import com.fasterxml.jackson.databind.annotation.*;
1615
import com.fasterxml.jackson.databind.cfg.MapperConfig;
@@ -99,10 +98,10 @@ public void setEnumProperty(EnumExample enumProperty)
9998
public static class QNameSerializer extends JsonSerializer<QName> {
10099

101100
@Override
102-
public void serialize(QName value, JsonGenerator jgen, SerializerProvider provider)
101+
public void serialize(QName value, JsonGenerator g, SerializerProvider provider)
103102
throws IOException
104103
{
105-
jgen.writeString(value.toString());
104+
g.writeString(value.toString());
106105
}
107106
}
108107

@@ -111,10 +110,13 @@ public static class QNameDeserializer extends StdDeserializer<QName>
111110
{
112111
public QNameDeserializer() { super(QName.class); }
113112
@Override
114-
public QName deserialize(JsonParser jp, DeserializationContext ctxt)
113+
public QName deserialize(JsonParser p, DeserializationContext ctxt)
115114
throws IOException
116115
{
117-
return QName.valueOf(jp.readValueAs(String.class));
116+
if (!p.hasToken(JsonToken.VALUE_STRING)) {
117+
throw new IllegalArgumentException("Unexpected token "+p.currentToken());
118+
}
119+
return QName.valueOf(p.getText());
118120
}
119121
}
120122

‎src/test/java/com/fasterxml/jackson/databind/misc/TestBlocking.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@ public class TestBlocking
2626
@Test
2727
public void testEagerAdvance() throws IOException
2828
{
29-
ObjectMapper mapper = new ObjectMapper();
30-
JsonParser jp = createParserUsingReader("[ 1 ");
31-
assertToken(JsonToken.START_ARRAY, jp.nextToken());
32-
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
29+
ObjectMapper mapper = jsonMapperBuilder()
30+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
31+
.build();
32+
JsonParser p = createParserUsingReader("[ 1 ");
33+
assertToken(JsonToken.START_ARRAY, p.nextToken());
34+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
3335

3436
// And then try to map just a single entry: shouldn't fail:
35-
Integer I = mapper.readValue(jp, Integer.class);
37+
Integer I = mapper.readValue(p, Integer.class);
3638
assertEquals(Integer.valueOf(1), I);
3739

3840
// and should fail only now:
3941
try {
40-
jp.nextToken();
42+
p.nextToken();
43+
fail("Should not pass");
4144
} catch (IOException ioe) {
4245
verifyException(ioe, "Unexpected end-of-input: expected close marker for ARRAY");
4346
}
44-
jp.close();
47+
p.close();
4548
}
4649
}

‎src/test/java/com/fasterxml/jackson/databind/node/JsonNodeConversionsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ public void testAsBoolean() throws Exception
159159
public static class LeafDeserializer extends JsonDeserializer<Leaf>
160160
{
161161
@Override
162-
public Leaf deserialize(JsonParser jp, DeserializationContext ctxt)
162+
public Leaf deserialize(JsonParser p, DeserializationContext ctxt)
163163
throws IOException
164164
{
165-
JsonNode tree = (JsonNode) jp.readValueAsTree();
165+
JsonNode tree = ctxt.readTree(p);
166166
Leaf leaf = new Leaf();
167167
leaf.value = tree.get("value").intValue();
168168
return leaf;

‎src/test/java/com/fasterxml/jackson/databind/node/TreeReadViaMapperTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ public void testNullViaParser() throws Exception
148148
@Test
149149
public void testMultiple() throws Exception
150150
{
151+
ObjectMapper mapper = jsonMapperBuilder()
152+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
153+
.build();
151154
String JSON = "12 \"string\" [ 1, 2, 3 ]";
152-
JsonFactory jf = new JsonFactory();
153-
JsonParser p = jf.createParser(new StringReader(JSON));
154-
final ObjectMapper mapper = objectMapper();
155+
JsonParser p = mapper.createParser(JSON);
155156
JsonNode result = mapper.readTree(p);
156157

157158
assertTrue(result.isIntegralNumber());

0 commit comments

Comments
 (0)
Please sign in to comment.