Skip to content

Commit 5c7f34f

Browse files
authored
Add StreamReadConstraints for TokenBuffer (#3691)
1 parent 6a7b516 commit 5c7f34f

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ protected Object _convertObjectId(JsonParser p, DeserializationContext ctxt,
13571357
// but that won't work for default impl (JSON and most dataformats)
13581358
buf.writeObject(rawId);
13591359
}
1360-
JsonParser bufParser = buf.asParser();
1360+
JsonParser bufParser = buf.asParser(p.streamReadConstraints());
13611361
bufParser.nextToken();
13621362
return idDeser.deserialize(bufParser, ctxt);
13631363
}
@@ -1746,7 +1746,7 @@ protected Object handlePolymorphic(JsonParser p, DeserializationContext ctxt,
17461746
if (unknownTokens != null) {
17471747
// need to add END_OBJECT marker first
17481748
unknownTokens.writeEndObject();
1749-
JsonParser p2 = unknownTokens.asParser();
1749+
JsonParser p2 = unknownTokens.asParser(p.streamReadConstraints());
17501750
p2.nextToken(); // to get to first data field
17511751
bean = subDeser.deserialize(p2, ctxt, bean);
17521752
}

src/main/java/com/fasterxml/jackson/databind/deser/impl/UnwrappedPropertyHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Object processUnwrapped(JsonParser originalParser, DeserializationContext
5757
{
5858
for (int i = 0, len = _properties.size(); i < len; ++i) {
5959
SettableBeanProperty prop = _properties.get(i);
60-
JsonParser p = buffered.asParser();
60+
JsonParser p = buffered.asParser(originalParser.streamReadConstraints());
6161
p.nextToken();
6262
prop.deserializeAndSet(p, ctxt, bean);
6363
}

src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public class TokenBuffer
5858
*/
5959
protected int _generatorFeatures;
6060

61+
/**
62+
* @since 2.15
63+
*/
64+
protected StreamReadConstraints _streamReadConstraints = StreamReadConstraints.defaults();
65+
6166
protected boolean _closed;
6267

6368
/**
@@ -170,6 +175,7 @@ public TokenBuffer(JsonParser p) {
170175
public TokenBuffer(JsonParser p, DeserializationContext ctxt)
171176
{
172177
_objectCodec = p.getCodec();
178+
_streamReadConstraints = p.streamReadConstraints();
173179
_parentContext = p.getParsingContext();
174180
_generatorFeatures = DEFAULT_GENERATOR_FEATURES;
175181
_writeContext = JsonWriteContext.createRootContext(null);
@@ -273,16 +279,26 @@ public JsonParser asParserOnFirstToken() throws IOException {
273279
*/
274280
public JsonParser asParser(ObjectCodec codec)
275281
{
276-
return new Parser(_first, codec, _hasNativeTypeIds, _hasNativeObjectIds, _parentContext);
282+
return new Parser(_first, codec, _hasNativeTypeIds, _hasNativeObjectIds, _parentContext, _streamReadConstraints);
283+
}
284+
285+
/**
286+
* @param streamReadConstraints constraints for streaming reads
287+
* @since v2.15
288+
*/
289+
public JsonParser asParser(StreamReadConstraints streamReadConstraints)
290+
{
291+
return new Parser(_first, _objectCodec, _hasNativeTypeIds, _hasNativeObjectIds, _parentContext, streamReadConstraints);
277292
}
278293

279294
/**
280295
* @param src Parser to use for accessing source information
281-
* like location, configured codec
296+
* like location, configured codec, streamReadConstraints
282297
*/
283298
public JsonParser asParser(JsonParser src)
284299
{
285-
Parser p = new Parser(_first, src.getCodec(), _hasNativeTypeIds, _hasNativeObjectIds, _parentContext);
300+
Parser p = new Parser(_first, src.getCodec(), _hasNativeTypeIds, _hasNativeObjectIds,
301+
_parentContext, src.streamReadConstraints());
286302
p.setLocation(src.getTokenLocation());
287303
return p;
288304
}
@@ -1441,6 +1457,11 @@ protected final static class Parser
14411457

14421458
protected ObjectCodec _codec;
14431459

1460+
/**
1461+
* @since 2.15
1462+
*/
1463+
protected StreamReadConstraints _streamReadConstraints;
1464+
14441465
/**
14451466
* @since 2.3
14461467
*/
@@ -1493,10 +1514,18 @@ public Parser(Segment firstSeg, ObjectCodec codec,
14931514
{
14941515
this(firstSeg, codec, hasNativeTypeIds, hasNativeObjectIds, null);
14951516
}
1496-
1517+
1518+
@Deprecated // since 2.15
1519+
public Parser(Segment firstSeg, ObjectCodec codec,
1520+
boolean hasNativeTypeIds, boolean hasNativeObjectIds,
1521+
JsonStreamContext parentContext)
1522+
{
1523+
this(firstSeg, codec, hasNativeTypeIds, hasNativeObjectIds, parentContext, StreamReadConstraints.defaults());
1524+
}
1525+
14971526
public Parser(Segment firstSeg, ObjectCodec codec,
14981527
boolean hasNativeTypeIds, boolean hasNativeObjectIds,
1499-
JsonStreamContext parentContext)
1528+
JsonStreamContext parentContext, StreamReadConstraints streamReadConstraints)
15001529
{
15011530
// 25-Jun-2022, tatu: Ideally would pass parser flags along (as
15021531
// per [databund#3528]) but for now make sure not to clear the flags
@@ -1505,6 +1534,7 @@ public Parser(Segment firstSeg, ObjectCodec codec,
15051534
_segment = firstSeg;
15061535
_segmentPtr = -1; // not yet read
15071536
_codec = codec;
1537+
_streamReadConstraints = streamReadConstraints;
15081538
_parsingContext = TokenBufferReadContext.createRootContext(parentContext);
15091539
_hasNativeTypeIds = hasNativeTypeIds;
15101540
_hasNativeObjectIds = hasNativeObjectIds;
@@ -1541,11 +1571,9 @@ public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
15411571
return DEFAULT_READ_CAPABILITIES;
15421572
}
15431573

1544-
// 03-Dec-2022, tatu: Not 100% sure what to do here; could probably instead
1545-
// pass from somewhere?
15461574
@Override
15471575
public StreamReadConstraints streamReadConstraints() {
1548-
return StreamReadConstraints.defaults();
1576+
return _streamReadConstraints;
15491577
}
15501578

15511579
/*

src/test/java/com/fasterxml/jackson/databind/deser/TestBigNumbers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void setNumber(BigInteger number) {
4848

4949
private final ObjectMapper MAPPER = newJsonMapper();
5050

51-
private final ObjectMapper newJsonMapperWithUnlimitedNumberSizeSupport() {
51+
private ObjectMapper newJsonMapperWithUnlimitedNumberSizeSupport() {
5252
JsonFactory jsonFactory = JsonFactory.builder()
5353
.streamReadConstraints(StreamReadConstraints.builder().maxNumberLength(Integer.MAX_VALUE).build())
5454
.build();
@@ -59,6 +59,7 @@ public void testDouble() throws Exception
5959
{
6060
try {
6161
MAPPER.readValue(generateJson("d"), DoubleWrapper.class);
62+
fail("expected JsonMappingException");
6263
} catch (JsonMappingException jsonMappingException) {
6364
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(),
6465
jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])"));
@@ -76,6 +77,7 @@ public void testBigDecimal() throws Exception
7677
{
7778
try {
7879
MAPPER.readValue(generateJson("number"), BigDecimalWrapper.class);
80+
fail("expected JsonMappingException");
7981
} catch (JsonMappingException jsonMappingException) {
8082
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(),
8183
jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])"));
@@ -94,6 +96,7 @@ public void testBigInteger() throws Exception
9496
{
9597
try {
9698
MAPPER.readValue(generateJson("number"), BigIntegerWrapper.class);
99+
fail("expected JsonMappingException");
97100
} catch (JsonMappingException jsonMappingException) {
98101
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(),
99102
jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])"));

0 commit comments

Comments
 (0)