Skip to content

test big strings #3704

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

Merged
merged 6 commits into from
Feb 15, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class TestBigStrings extends BaseMapTest
{
final static class StringWrapper
{
String string;

StringWrapper() { }

StringWrapper(String string) { this.string = string; }

void setString(String string) {
this.string = string;
}
}

/*
/**********************************************************
/* Tests
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

private ObjectMapper newJsonMapperWithUnlimitedStringSizeSupport() {
JsonFactory jsonFactory = JsonFactory.builder()
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build())
.build();
return JsonMapper.builder(jsonFactory).build();
}

public void testBigString() throws Exception
{
try {
MAPPER.readValue(generateJson("string", 1001000), StringWrapper.class);
fail("expected JsonMappingException");
} catch (JsonMappingException jsonMappingException) {
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(),
jsonMappingException.getMessage().startsWith("String length (1001000) exceeds the maximum length (1000000)"));
}
}

public void testBiggerString() throws Exception
{
try {
MAPPER.readValue(generateJson("string", 2000000), StringWrapper.class);
fail("expected JsonMappingException");
} catch (JsonMappingException jsonMappingException) {
final String message = jsonMappingException.getMessage();
// this test fails when the TextBuffer is being resized, so we don't yet know just how big the string is
// so best not to assert that the String length value in the message is the full 2000000 value
assertTrue("unexpected exception message: " + message, message.startsWith("String length"));
assertTrue("unexpected exception message: " + message, message.contains("exceeds the maximum length (1000000)"));
}
}

public void testUnlimitedString() throws Exception
{
final int len = 1001000;
StringWrapper sw = newJsonMapperWithUnlimitedStringSizeSupport()
.readValue(generateJson("string", len), StringWrapper.class);
assertEquals(len, sw.string.length());
}


private String generateJson(final String fieldName, final int len) {
final StringBuilder sb = new StringBuilder();
sb.append("{\"")
.append(fieldName)
.append("\": \"");
for (int i = 0; i < len; i++) {
sb.append('a');
}
sb.append("\"}");
return sb.toString();
}
}