Skip to content

Commit b06a588

Browse files
committed
Improve error messaging wrt #5001
1 parent 82f0682 commit b06a588

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/main/java/tools/jackson/databind/DeserializationContext.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1851,10 +1851,17 @@ public <T> T reportTrailingTokens(Class<?> targetType,
18511851
JsonParser p, JsonToken trailingToken)
18521852
throws DatabindException
18531853
{
1854-
throw MismatchedInputException.from(p, targetType, String.format(
1855-
"Trailing token (of type %s) found after value (bound as %s): not allowed as per `DeserializationFeature.FAIL_ON_TRAILING_TOKENS`",
1856-
trailingToken, ClassUtil.nameOf(targetType)
1857-
));
1854+
// 05-Mar-2025, tatu: [databind#5001] Handle non-blocking case separately to give better message
1855+
if (trailingToken == JsonToken.NOT_AVAILABLE) {
1856+
throw MismatchedInputException.from(p, targetType,
1857+
"Incomplete content (`JsonToken.NOT_AVAILABLE`) after value (bound as %s):"
1858+
+"not allowed as per `DeserializationFeature.FAIL_ON_TRAILING_TOKENS`"
1859+
+" (missing call to `InputFeeder.endOfInput()`?)"
1860+
.formatted(ClassUtil.nameOf(targetType)));
1861+
}
1862+
throw MismatchedInputException.from(p, targetType,
1863+
"Trailing token (`JsonToken.%s`) found after value (bound as %s): not allowed as per `DeserializationFeature.FAIL_ON_TRAILING_TOKENS`"
1864+
.formatted(trailingToken, ClassUtil.nameOf(targetType)));
18581865
}
18591866

18601867
/*

src/test/java/tools/jackson/databind/deser/NonBlockingDeserTest.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package tools.jackson.databind.deser;
22

3+
import java.nio.ByteBuffer;
4+
35
import org.junit.jupiter.api.Test;
6+
47
import tools.jackson.core.JsonParser;
58
import tools.jackson.core.ObjectReadContext;
69
import tools.jackson.core.async.ByteBufferFeeder;
7-
import tools.jackson.databind.DeserializationFeature;
10+
811
import tools.jackson.databind.ObjectMapper;
912
import tools.jackson.databind.json.JsonMapper;
1013

11-
import java.nio.ByteBuffer;
12-
1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415

1516
/**
@@ -31,20 +32,18 @@ public void testNonBlockingParser()
3132
final ObjectMapper m = JsonMapper.builder()
3233
//.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
3334
.build();
34-
final JsonParser parser =
35-
m.tokenStreamFactory().createNonBlockingByteBufferParser(ObjectReadContext.empty());
3635
final int len = 10;
3736
Foo[] foos = new Foo[len];
3837
for (int i = 0; i < len; ++i) {
3938
foos[i] = new Foo("bar-" + i);
4039
}
41-
try {
42-
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(m.writeValueAsBytes(foos)));
43-
((ByteBufferFeeder) parser).endOfInput();
44-
final Foo[] result = m.readValue(parser, Foo[].class);
45-
assertEquals(len, result.length);
46-
} finally {
47-
parser.close();
40+
final Foo[] result;
41+
try (final JsonParser parser =
42+
m.tokenStreamFactory().createNonBlockingByteBufferParser(ObjectReadContext.empty())) {
43+
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(m.writeValueAsBytes(foos)));
44+
((ByteBufferFeeder) parser).endOfInput();
45+
result = m.readValue(parser, Foo[].class);
4846
}
47+
assertEquals(len, result.length);
4948
}
5049
}

src/test/java/tools/jackson/databind/deser/validate/FullStreamReadTest.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ public void testMapperFailOnTrailing()
8383
strict.readTree(JSON_FAIL_ARRAY);
8484
fail("Should not have passed");
8585
} catch (MismatchedInputException e) {
86-
verifyException(e, "Trailing token (of type START_ARRAY)");
86+
verifyException(e, "Trailing token (`JsonToken.START_ARRAY`)");
8787
verifyException(e, "value (bound as `tools.jackson.databind.JsonNode`)");
8888
}
8989

9090
try {
9191
strict.readValue(JSON_FAIL_ARRAY, List.class);
9292
fail("Should not have passed");
9393
} catch (MismatchedInputException e) {
94-
verifyException(e, "Trailing token (of type START_ARRAY)");
94+
verifyException(e, "Trailing token (`JsonToken.START_ARRAY`)");
9595
verifyException(e, "value (bound as `java.util.List`)");
9696
}
9797

@@ -135,15 +135,15 @@ public void testMapperFailOnTrailingWithNull()
135135
strict.readTree(JSON_FAIL_NULL);
136136
fail("Should not have passed");
137137
} catch (MismatchedInputException e) {
138-
verifyException(e, "Trailing token (of type VALUE_FALSE)");
138+
verifyException(e, "Trailing token (`JsonToken.VALUE_FALSE`)");
139139
verifyException(e, "value (bound as `tools.jackson.databind.JsonNode`)");
140140
}
141141

142142
try {
143143
strict.readValue(JSON_FAIL_NULL, List.class);
144144
fail("Should not have passed");
145145
} catch (MismatchedInputException e) {
146-
verifyException(e, "Trailing token (of type VALUE_FALSE)");
146+
verifyException(e, "Trailing token (`JsonToken.VALUE_FALSE`)");
147147
verifyException(e, "value (bound as `java.util.List`)");
148148
}
149149

@@ -203,14 +203,14 @@ public void testReaderFailOnTrailing()
203203
strictRForList.readValue(JSON_FAIL_ARRAY);
204204
fail("Should not have passed");
205205
} catch (MismatchedInputException e) {
206-
verifyException(e, "Trailing token (of type START_ARRAY)");
206+
verifyException(e, "Trailing token (`JsonToken.START_ARRAY`)");
207207
verifyException(e, "value (bound as `java.util.List`)");
208208
}
209209
try {
210210
strictR.readTree(JSON_FAIL_ARRAY);
211211
fail("Should not have passed");
212212
} catch (MismatchedInputException e) {
213-
verifyException(e, "Trailing token (of type START_ARRAY)");
213+
verifyException(e, "Trailing token (`JsonToken.START_ARRAY`)");
214214
verifyException(e, "value (bound as `tools.jackson.databind.JsonNode`)");
215215
}
216216

@@ -220,7 +220,7 @@ public void testReaderFailOnTrailing()
220220
.readValue(JSON_FAIL_ARRAY);
221221
fail("Should not have passed");
222222
} catch (MismatchedInputException e) {
223-
verifyException(e, "Trailing token (of type START_ARRAY)");
223+
verifyException(e, "Trailing token (`JsonToken.START_ARRAY`)");
224224
verifyException(e, "value (bound as `java.util.ArrayList`)");
225225
}
226226

@@ -262,15 +262,15 @@ public void testReaderFailOnTrailingWithNull()
262262
strictRForList.readValue(JSON_FAIL_NULL);
263263
fail("Should not have passed");
264264
} catch (MismatchedInputException e) {
265-
verifyException(e, "Trailing token (of type VALUE_FALSE)");
265+
verifyException(e, "Trailing token (`JsonToken.VALUE_FALSE`)");
266266
verifyException(e, "value (bound as `java.util.List`)");
267267
}
268268

269269
try {
270270
strictR.readTree(JSON_FAIL_NULL);
271271
fail("Should not have passed");
272272
} catch (MismatchedInputException e) {
273-
verifyException(e, "Trailing token (of type VALUE_FALSE)");
273+
verifyException(e, "Trailing token (`JsonToken.VALUE_FALSE`)");
274274
verifyException(e, "value (bound as `tools.jackson.databind.JsonNode`)");
275275
}
276276

0 commit comments

Comments
 (0)