Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -15,6 +15,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.exc.InputCoercionException;
import com.fasterxml.jackson.core.filter.FilteringParserDelegate;
import com.fasterxml.jackson.core.io.JsonEOFException;

import org.elasticsearch.core.IOUtils;
Expand Down Expand Up @@ -122,6 +123,10 @@ public XContentString optimizedText() throws IOException {
if (currentToken().isValue() == false) {
throwOnNoText();
}
var parser = this.parser;
if (parser instanceof FilteringParserDelegate delegate) {
parser = delegate.delegate();
}
if (parser instanceof ESUTF8StreamJsonParser esParser) {
var bytesRef = esParser.getValueAsText();
if (bytesRef != null) {
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,20 @@ 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();
assertTrue(text.hasBytes());
assertThat(text.string(), equalTo("foo"));
}
}
}