Skip to content

Commit 500e960

Browse files
authored
Fixes #1207: apply "maxNameLength" change to CharsToNameCaonicalizer (#1208)
1 parent 9438a5e commit 500e960

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ a pure JSON library.
3838
#1195: Use `BufferRecycler` provided by output (`OutputStream`, `Writer`) object if available
3939
(contributed by Mario F)
4040
#1202: Add `RecyclerPool.clear()` method for dropping all pooled Objects
41+
#1205: JsonFactory.setStreamReadConstraints(StreamReadConstraints) fails to
42+
update "maxNameLength" for symbol tables
43+
(reported by @denizk)
4144

4245
2.16.2 (not yet released)
4346

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ public static int collectDefaults() {
221221
* Each factory comes equipped with a shared root symbol table.
222222
* It should not be linked back to the original blueprint, to
223223
* avoid contents from leaking between factories.
224+
*<p>
225+
* NOTE: non-final since 2.17 due to need to re-create if
226+
* {@link StreamReadConstraints} re-configured for factory.
224227
*/
225-
protected final transient CharsToNameCanonicalizer _rootCharSymbols;
228+
protected transient CharsToNameCanonicalizer _rootCharSymbols;
226229

227230
/**
228231
* Alternative to the basic symbol table, some stream-based
@@ -870,7 +873,13 @@ public StreamWriteConstraints streamWriteConstraints() {
870873
* @since 2.15
871874
*/
872875
public JsonFactory setStreamReadConstraints(StreamReadConstraints src) {
876+
final int maxNameLen = _streamReadConstraints.getMaxNameLength();
873877
_streamReadConstraints = Objects.requireNonNull(src);
878+
// 30-Jan-2024, tatu: [core#1207] Need to recreate if max-name-length
879+
// setting changes
880+
if (_streamReadConstraints.getMaxNameLength() != maxNameLen) {
881+
_rootCharSymbols = CharsToNameCanonicalizer.createRoot(this);
882+
}
874883
return this;
875884
}
876885

src/test/java/com/fasterxml/jackson/core/constraints/LargeNameReadTest.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class LargeNameReadTest extends BaseTest
1616
.streamReadConstraints(StreamReadConstraints.builder().maxNameLength(100).build())
1717
.build();
1818

19+
private final JsonFactory JSON_F_NAME_100_B = new JsonFactory();
20+
{
21+
JSON_F_NAME_100_B.setStreamReadConstraints(StreamReadConstraints.builder()
22+
.maxNameLength(100).build());
23+
}
24+
1925
// Test name that is below default max name
2026
public void testLargeNameBytes() throws Exception {
2127
final String doc = generateJSON(StreamReadConstraints.defaults().getMaxNameLength() - 100);
@@ -31,21 +37,31 @@ public void testLargeNameChars() throws Exception {
3137
}
3238
}
3339

34-
public void testLargeNameWithSmallLimitBytes() throws Exception
40+
public void testLargeNameWithSmallLimitBytes() throws Exception {
41+
_testLargeNameWithSmallLimitBytes(JSON_F_NAME_100);
42+
_testLargeNameWithSmallLimitBytes(JSON_F_NAME_100_B);
43+
}
44+
45+
private void _testLargeNameWithSmallLimitBytes(JsonFactory jf) throws Exception
3546
{
3647
final String doc = generateJSON(1000);
37-
try (JsonParser p = createParserUsingStream(JSON_F_NAME_100, doc, "UTF-8")) {
48+
try (JsonParser p = createParserUsingStream(jf, doc, "UTF-8")) {
3849
consumeTokens(p);
3950
fail("expected StreamConstraintsException");
4051
} catch (StreamConstraintsException e) {
4152
verifyException(e, "Name length");
4253
}
4354
}
4455

45-
public void testLargeNameWithSmallLimitChars() throws Exception
56+
public void testLargeNameWithSmallLimitChars() throws Exception {
57+
_testLargeNameWithSmallLimitChars(JSON_F_NAME_100);
58+
_testLargeNameWithSmallLimitChars(JSON_F_NAME_100_B);
59+
}
60+
61+
private void _testLargeNameWithSmallLimitChars(JsonFactory jf) throws Exception
4662
{
4763
final String doc = generateJSON(1000);
48-
try (JsonParser p = createParserUsingReader(JSON_F_NAME_100, doc)) {
64+
try (JsonParser p = createParserUsingReader(jf, doc)) {
4965
consumeTokens(p);
5066
fail("expected StreamConstraintsException");
5167
} catch (StreamConstraintsException e) {

0 commit comments

Comments
 (0)