| 
12 | 12 | import com.carrotsearch.randomizedtesting.RandomizedContext;  | 
13 | 13 | 
 
  | 
14 | 14 | import org.elasticsearch.common.bytes.BytesReference;  | 
 | 15 | +import org.elasticsearch.xcontent.ToXContentFragment;  | 
15 | 16 | import org.elasticsearch.xcontent.XContentBuilder;  | 
16 | 17 | import org.elasticsearch.xcontent.XContentFactory;  | 
17 | 18 | import org.elasticsearch.xcontent.XContentParser;  | 
18 | 19 | import org.elasticsearch.xcontent.XContentType;  | 
19 | 20 | 
 
  | 
 | 21 | +import java.io.IOException;  | 
20 | 22 | import java.util.Map;  | 
21 | 23 | 
 
  | 
22 | 24 | import static org.hamcrest.Matchers.equalTo;  | 
@@ -49,4 +51,42 @@ public void testInsertRandomFieldsAndShuffle() throws Exception {  | 
49 | 51 |             assertThat(mapOrdered.keySet().iterator().next(), not(equalTo("field")));  | 
50 | 52 |         }  | 
51 | 53 |     }  | 
 | 54 | + | 
 | 55 | +    private record TestToXContent(String field, String value) implements ToXContentFragment {  | 
 | 56 | + | 
 | 57 | +        @Override  | 
 | 58 | +        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {  | 
 | 59 | +            return builder.field(field, value);  | 
 | 60 | +        }  | 
 | 61 | +    }  | 
 | 62 | + | 
 | 63 | +    public void testYamlXContentRoundtripSanitization() throws Exception {  | 
 | 64 | +        var test = new AbstractXContentTestCase<TestToXContent>() {  | 
 | 65 | + | 
 | 66 | +            @Override  | 
 | 67 | +            protected TestToXContent createTestInstance() {  | 
 | 68 | +                // we need to randomly create both a "problematic" and an okay version in order to ensure that the sanitization code  | 
 | 69 | +                // can draw at least one okay version if polled often enough  | 
 | 70 | +                return randomBoolean() ? new TestToXContent("a\u0085b", "def") : new TestToXContent("a b", "def");  | 
 | 71 | +            }  | 
 | 72 | + | 
 | 73 | +            @Override  | 
 | 74 | +            protected TestToXContent doParseInstance(XContentParser parser) throws IOException {  | 
 | 75 | +                assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());  | 
 | 76 | +                assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());  | 
 | 77 | +                String name = parser.currentName();  | 
 | 78 | +                assertEquals(XContentParser.Token.VALUE_STRING, parser.nextToken());  | 
 | 79 | +                String value = parser.text();  | 
 | 80 | +                assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());  | 
 | 81 | +                return new TestToXContent(name, value);  | 
 | 82 | +            };  | 
 | 83 | + | 
 | 84 | +            @Override  | 
 | 85 | +            protected boolean supportsUnknownFields() {  | 
 | 86 | +                return false;  | 
 | 87 | +            }  | 
 | 88 | +        };  | 
 | 89 | +        // testFromXContent runs 20 repetitions, enough to hit a YAML xcontent version very likely  | 
 | 90 | +        test.testFromXContent();  | 
 | 91 | +    }  | 
52 | 92 | }  | 
0 commit comments