9
9
10
10
package org .elasticsearch .common .xcontent .json ;
11
11
12
+ import org .elasticsearch .common .bytes .BytesReference ;
12
13
import org .elasticsearch .common .xcontent .BaseXContentTestCase ;
14
+ import org .elasticsearch .xcontent .Text ;
15
+ import org .elasticsearch .xcontent .XContentBuilder ;
13
16
import org .elasticsearch .xcontent .XContentGenerator ;
14
17
import org .elasticsearch .xcontent .XContentParseException ;
15
18
import org .elasticsearch .xcontent .XContentParser ;
18
21
import org .elasticsearch .xcontent .json .JsonXContent ;
19
22
20
23
import java .io .ByteArrayOutputStream ;
24
+ import java .util .Set ;
25
+
26
+ import static org .hamcrest .Matchers .equalTo ;
21
27
22
28
public class JsonXContentTests extends BaseXContentTestCase {
23
29
@@ -41,4 +47,20 @@ public void testMalformedJsonFieldThrowsXContentException() throws Exception {
41
47
assertThrows (XContentParseException .class , () -> parser .text ());
42
48
}
43
49
}
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
+ }
44
66
}
0 commit comments