Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,7 @@ public String text() throws IOException {

@Override
public XContentString optimizedText() throws IOException {
if (currentToken().isValue() == false) {
throwOnNoText();
}
if (parser instanceof ESUTF8StreamJsonParser esParser) {
var bytesRef = esParser.getValueAsText();
if (bytesRef != null) {
return bytesRef;
}
}
// TODO: enable utf-8 parsing optimization once verified it is completely safe
return new Text(text());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

package org.elasticsearch.common.xcontent.json;

import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.BaseXContentTestCase;
import org.elasticsearch.xcontent.Text;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentGenerator;
import org.elasticsearch.xcontent.XContentParseException;
import org.elasticsearch.xcontent.XContentParser;
Expand All @@ -18,6 +21,9 @@
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.ByteArrayOutputStream;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class JsonXContentTests extends BaseXContentTestCase {

Expand All @@ -41,4 +47,21 @@ public void testMalformedJsonFieldThrowsXContentException() throws Exception {
assertThrows(XContentParseException.class, () -> parser.text());
}
}

public void testOptimizedTextHasBytes() throws Exception {
XContentBuilder builder = builder().startObject().field("text", new Text("foo")).endObject();
XContentParserConfiguration parserConfig = parserConfig();
if (randomBoolean()) {
parserConfig = parserConfig.withFiltering(null, Set.of("*"), null, true);
}
try (XContentParser parser = createParser(parserConfig, xcontentType().xContent(), BytesReference.bytes(builder))) {
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertTrue(parser.nextToken().isValue());
Text text = (Text) parser.optimizedText();
// TODO: uncomment after utf8 optimized parsing has been enabled again:
// assertTrue(text.hasBytes());
assertThat(text.string(), equalTo("foo"));
}
}
}