20
20
package org .elasticsearch .action .admin .indices .mapping .get ;
21
21
22
22
import org .elasticsearch .action .admin .indices .mapping .get .GetFieldMappingsResponse .FieldMappingMetaData ;
23
+ import org .elasticsearch .common .Strings ;
23
24
import org .elasticsearch .common .bytes .BytesArray ;
24
25
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
25
26
import org .elasticsearch .common .io .stream .StreamInput ;
31
32
import java .util .HashMap ;
32
33
import java .util .Map ;
33
34
35
+ import static org .hamcrest .CoreMatchers .containsString ;
36
+
34
37
public class GetFieldMappingsResponseTests extends AbstractWireSerializingTestCase <GetFieldMappingsResponse > {
35
38
36
39
public void testManualSerialization () throws IOException {
@@ -50,6 +53,28 @@ public void testManualSerialization() throws IOException {
50
53
}
51
54
}
52
55
56
+ public void testNullFieldMappingToXContent () {
57
+ Map <String , Map <String , FieldMappingMetaData >> mappings = new HashMap <>();
58
+ mappings .put ("index" , Collections .singletonMap ("field" , FieldMappingMetaData .NULL ));
59
+ GetFieldMappingsResponse response = new GetFieldMappingsResponse (mappings );
60
+ assertEquals ("{\" index\" :{\" mappings\" :{}}}" , Strings .toString (response ));
61
+ }
62
+
63
+ public void testMixedNullAndPresentFieldMappingToXContent () {
64
+ Map <String , Map <String , FieldMappingMetaData >> mappings = new HashMap <>();
65
+ mappings .put ("index" , Map .of ("field" , FieldMappingMetaData .NULL ,
66
+ "field1" , new FieldMappingMetaData ("field1" , new BytesArray ("{\" type\" :\" string\" }" ))));
67
+ mappings .put ("index2" , Map .of ("field" , new FieldMappingMetaData ("field" , new BytesArray ("{}" ))));
68
+ mappings .put ("index3" , Map .of ("field" , FieldMappingMetaData .NULL ));
69
+ GetFieldMappingsResponse response = new GetFieldMappingsResponse (mappings );
70
+ String respAsString = Strings .toString (response );
71
+ assertThat (respAsString , containsString ("\" index3\" :{\" mappings\" :{}}" ));
72
+ assertThat (respAsString ,
73
+ containsString ("\" index\" :{\" mappings\" :{\" field1\" :{\" full_name\" :\" field1\" ,\" mapping\" :{\" type\" :\" string\" }}}}" ));
74
+ assertThat (respAsString ,
75
+ containsString ("\" index2\" :{\" mappings\" :{\" field\" :{\" full_name\" :\" field\" ,\" mapping\" :{}}}}" ));
76
+ }
77
+
53
78
@ Override
54
79
protected GetFieldMappingsResponse createTestInstance () {
55
80
return new GetFieldMappingsResponse (randomMapping ());
0 commit comments