Skip to content

[Backport 7.17] Limit the size of toString() to a configurable limit #273

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 1 commit into from
May 3, 2022
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
60 changes: 55 additions & 5 deletions java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import jakarta.json.stream.JsonParsingException;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.util.AbstractMap;
Expand Down Expand Up @@ -242,15 +241,51 @@ public static void serializeIntOrNull(JsonGenerator generator, int value, int de
}
}

/**
* Renders a <code>JsonpSerializable</code> as a string by serializing it to JSON. Any object of an application-specific
* class in the object graph is rendered using that object's <code>toString()</code> representation as a JSON string value.
* <p>
* The size of the string is limited to {@link #MAX_TO_STRING_LENGTH}.
*
* @see #MAX_TO_STRING_LENGTH
*/
public static String toString(JsonpSerializable value) {
return toString(value, ToStringMapper.INSTANCE, new StringBuilder()).toString();
}

/**
* Maximum length of the <code>toString</code> representation of a <code>JsonpSerializable</code>.
* <p>
* The default is 10k characters, and can be changed globally by changing the value of this field.
*/
public static int MAX_TO_STRING_LENGTH = 10000;

private static class ToStringTooLongException extends RuntimeException {
}

/**
* Renders a <code>JsonpSerializable</code> as a string in a destination <code>StringBuilder</code>by serializing it to JSON.
* <p>
* The size of the string is limited to {@link #MAX_TO_STRING_LENGTH}.
*
* @return the <code>dest</code> parameter, for chaining.
* @see #toString(JsonpSerializable)
* @see #MAX_TO_STRING_LENGTH
*/
public static StringBuilder toString(JsonpSerializable value, JsonpMapper mapper, StringBuilder dest) {
Writer writer = new Writer() {
int length = 0;
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
dest.append(cbuf, off, len);
public void write(char[] cbuf, int off, int len) {
int max = MAX_TO_STRING_LENGTH;
length += len;
if (length > max) {
dest.append(cbuf, off, len - (length - max));
dest.append("...");
throw new ToStringTooLongException();
} else {
dest.append(cbuf, off, len);
}
}

@Override
Expand All @@ -263,11 +298,26 @@ public void close() {
};

JsonGenerator generator = mapper.jsonProvider().createGenerator(writer);
value.serialize(generator, mapper);
generator.close();
try {
value.serialize(generator, mapper);
generator.close();
} catch (ToStringTooLongException e) {
// Ignore
}
return dest;
}

/**
* Renders a <code>JsonpSerializable</code> as a string in a destination <code>StringBuilder</code>by serializing it to JSON.
* Any object of an application-specific class in the object graph is rendered using that object's <code>toString()</code>
* representation as a JSON string value.
* <p>
* The size of the string is limited to {@link #MAX_TO_STRING_LENGTH}.
*
* @return the <code>dest</code> parameter, for chaining.
* @see #toString(JsonpSerializable)
* @see #MAX_TO_STRING_LENGTH
*/
public static StringBuilder toString(JsonpSerializable value, StringBuilder dest) {
return toString(value, ToStringMapper.INSTANCE, dest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ public String toString() {
}
}

@Test
public void testLargeObjectToString() {
// Build a large string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1001; i++) {
sb.append("0123456789");
}

String text = sb.toString();
assertEquals(10010, text.length());

Hit<String> hit = Hit.of(h -> h
.source(text)
.index("idx")
.id("id1")
);

String toString = hit.toString();

assertEquals(10003, toString.length());
assertTrue(toString.endsWith("..."));
}

@Test
public void testSerializeDoubleOrNull() {
// ---- Double values
Expand Down