Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1207: apply "maxNameLength" change to CharsToNameCanonicalizer #1208

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ a pure JSON library.
#1195: Use `BufferRecycler` provided by output (`OutputStream`, `Writer`) object if available
(contributed by Mario F)
#1202: Add `RecyclerPool.clear()` method for dropping all pooled Objects
#1205: JsonFactory.setStreamReadConstraints(StreamReadConstraints) fails to
update "maxNameLength" for symbol tables
(reported by @denizk)

2.16.2 (not yet released)

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@ public static int collectDefaults() {
* Each factory comes equipped with a shared root symbol table.
* It should not be linked back to the original blueprint, to
* avoid contents from leaking between factories.
*<p>
* NOTE: non-final since 2.17 due to need to re-create if
* {@link StreamReadConstraints} re-configured for factory.
*/
protected final transient CharsToNameCanonicalizer _rootCharSymbols;
protected transient CharsToNameCanonicalizer _rootCharSymbols;

/**
* Alternative to the basic symbol table, some stream-based
Expand Down Expand Up @@ -870,7 +873,13 @@ public StreamWriteConstraints streamWriteConstraints() {
* @since 2.15
*/
public JsonFactory setStreamReadConstraints(StreamReadConstraints src) {
final int maxNameLen = _streamReadConstraints.getMaxNameLength();
_streamReadConstraints = Objects.requireNonNull(src);
// 30-Jan-2024, tatu: [core#1207] Need to recreate if max-name-length
// setting changes
if (_streamReadConstraints.getMaxNameLength() != maxNameLen) {
_rootCharSymbols = CharsToNameCanonicalizer.createRoot(this);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class LargeNameReadTest extends BaseTest
.streamReadConstraints(StreamReadConstraints.builder().maxNameLength(100).build())
.build();

private final JsonFactory JSON_F_NAME_100_B = new JsonFactory();
{
JSON_F_NAME_100_B.setStreamReadConstraints(StreamReadConstraints.builder()
.maxNameLength(100).build());
}

// Test name that is below default max name
public void testLargeNameBytes() throws Exception {
final String doc = generateJSON(StreamReadConstraints.defaults().getMaxNameLength() - 100);
Expand All @@ -31,21 +37,31 @@ public void testLargeNameChars() throws Exception {
}
}

public void testLargeNameWithSmallLimitBytes() throws Exception
public void testLargeNameWithSmallLimitBytes() throws Exception {
_testLargeNameWithSmallLimitBytes(JSON_F_NAME_100);
_testLargeNameWithSmallLimitBytes(JSON_F_NAME_100_B);
}

private void _testLargeNameWithSmallLimitBytes(JsonFactory jf) throws Exception
{
final String doc = generateJSON(1000);
try (JsonParser p = createParserUsingStream(JSON_F_NAME_100, doc, "UTF-8")) {
try (JsonParser p = createParserUsingStream(jf, doc, "UTF-8")) {
consumeTokens(p);
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
verifyException(e, "Name length");
}
}

public void testLargeNameWithSmallLimitChars() throws Exception
public void testLargeNameWithSmallLimitChars() throws Exception {
_testLargeNameWithSmallLimitChars(JSON_F_NAME_100);
_testLargeNameWithSmallLimitChars(JSON_F_NAME_100_B);
}

private void _testLargeNameWithSmallLimitChars(JsonFactory jf) throws Exception
{
final String doc = generateJSON(1000);
try (JsonParser p = createParserUsingReader(JSON_F_NAME_100, doc)) {
try (JsonParser p = createParserUsingReader(jf, doc)) {
consumeTokens(p);
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
Expand Down