Skip to content

Commit 54d3fd5

Browse files
authored
Make optimizedText wrapper aware (#132461)
1 parent 37b6f8f commit 54d3fd5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.fasterxml.jackson.core.JsonToken;
1717
import com.fasterxml.jackson.core.exc.InputCoercionException;
1818
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
19+
import com.fasterxml.jackson.core.filter.FilteringParserDelegate;
1920
import com.fasterxml.jackson.core.io.JsonEOFException;
2021

2122
import org.elasticsearch.core.IOUtils;
@@ -146,6 +147,10 @@ public XContentString optimizedText() throws IOException {
146147
if (currentToken().isValue() == false) {
147148
throwOnNoText();
148149
}
150+
var parser = this.parser;
151+
if (parser instanceof FilteringParserDelegate delegate) {
152+
parser = delegate.delegate();
153+
}
149154
if (parser instanceof ESUTF8StreamJsonParser esParser) {
150155
var bytesRef = esParser.getValueAsText();
151156
if (bytesRef != null) {

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

Lines changed: 22 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,20 @@ 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+
assertTrue(text.hasBytes());
63+
assertThat(text.string(), equalTo("foo"));
64+
}
65+
}
4466
}

0 commit comments

Comments
 (0)