Skip to content

Commit 6e5d99a

Browse files
authored
[9.1] Disable utf-8 parsing optimization (#135180)
* Disable utf-8 parsing optimization (#135172) A yet to be understood issue has emerged with the utf8 parsing optimization and the meantime we should disable the optimization. It looks like the wrong values are returned for fields. In the case of the test failures here, that surfaced as unable to the parse fields with IPs and causing index request to fail. However the impact could be larger. * fix test compile errors
1 parent b5ed181 commit 6e5d99a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentParser.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,7 @@ public String text() throws IOException {
143143

144144
@Override
145145
public XContentString optimizedText() throws IOException {
146-
if (currentToken().isValue() == false) {
147-
throwOnNoText();
148-
}
149-
if (parser instanceof ESUTF8StreamJsonParser esParser) {
150-
var bytesRef = esParser.getValueAsText();
151-
if (bytesRef != null) {
152-
return bytesRef;
153-
}
154-
}
146+
// TODO: enable utf-8 parsing optimization once verified it is completely safe
155147
return new Text(text());
156148
}
157149

server/src/test/java/org/elasticsearch/common/xcontent/json/JsonXContentTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
package org.elasticsearch.common.xcontent.json;
1111

12+
import org.elasticsearch.common.bytes.BytesReference;
1213
import org.elasticsearch.common.xcontent.BaseXContentTestCase;
14+
import org.elasticsearch.xcontent.Text;
15+
import org.elasticsearch.xcontent.XContentBuilder;
1316
import org.elasticsearch.xcontent.XContentGenerator;
1417
import org.elasticsearch.xcontent.XContentParseException;
1518
import org.elasticsearch.xcontent.XContentParser;
@@ -18,6 +21,9 @@
1821
import org.elasticsearch.xcontent.json.JsonXContent;
1922

2023
import java.io.ByteArrayOutputStream;
24+
import java.util.Set;
25+
26+
import static org.hamcrest.Matchers.equalTo;
2127

2228
public class JsonXContentTests extends BaseXContentTestCase {
2329

@@ -41,4 +47,21 @@ public void testMalformedJsonFieldThrowsXContentException() throws Exception {
4147
assertThrows(XContentParseException.class, () -> parser.text());
4248
}
4349
}
50+
51+
public void testOptimizedTextHasBytes() throws Exception {
52+
XContentBuilder builder = builder().startObject().field("text", new Text("foo")).endObject();
53+
XContentParserConfiguration parserConfig = parserConfig();
54+
if (randomBoolean()) {
55+
parserConfig = parserConfig.withFiltering(null, Set.of("*"), null, true);
56+
}
57+
try (XContentParser parser = createParser(parserConfig, xcontentType().xContent(), BytesReference.bytes(builder))) {
58+
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
59+
assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken());
60+
assertTrue(parser.nextToken().isValue());
61+
Text text = (Text) parser.optimizedText();
62+
// TODO: uncomment after utf8 optimized parsing has been enabled again:
63+
// assertTrue(text.hasBytes());
64+
assertThat(text.string(), equalTo("foo"));
65+
}
66+
}
4467
}

0 commit comments

Comments
 (0)