26
26
import org .elasticsearch .common .io .stream .StreamInput ;
27
27
import org .elasticsearch .common .xcontent .ToXContent ;
28
28
import org .elasticsearch .common .xcontent .XContentBuilder ;
29
+ import org .elasticsearch .common .xcontent .XContentFactory ;
29
30
import org .elasticsearch .common .xcontent .XContentParser ;
30
31
import org .elasticsearch .common .xcontent .XContentType ;
31
32
import org .elasticsearch .test .ESTestCase ;
@@ -65,7 +66,7 @@ public void testXContentRoundTrip() throws IOException {
65
66
try (XContentBuilder builder = XContentBuilder .builder (randomFrom (XContentType .values ()).xContent ())) {
66
67
result .toXContent (builder , ToXContent .EMPTY_PARAMS );
67
68
try (XContentBuilder shuffled = shuffleXContent (builder );
68
- XContentParser parser = createParser (shuffled )) {
69
+ XContentParser parser = createParser (shuffled )) {
69
70
read = TaskResult .PARSER .apply (parser , null );
70
71
}
71
72
} catch (IOException e ) {
@@ -74,16 +75,52 @@ public void testXContentRoundTrip() throws IOException {
74
75
assertEquals (result , read );
75
76
}
76
77
78
+ public void testTaskInfoIsForwardCompatible () throws IOException {
79
+ TaskInfo taskInfo = randomTaskInfo ();
80
+ TaskInfo read ;
81
+ try (XContentBuilder builder = XContentBuilder .builder (randomFrom (XContentType .values ()).xContent ())) {
82
+ builder .startObject ();
83
+ taskInfo .toXContent (builder , ToXContent .EMPTY_PARAMS );
84
+ builder .endObject ();
85
+ try (XContentBuilder withExtraFields = addRandomUnknownFields (builder )) {
86
+ try (XContentBuilder shuffled = shuffleXContent (withExtraFields )) {
87
+ try (XContentParser parser = createParser (shuffled )) {
88
+ read = TaskInfo .PARSER .apply (parser , null );
89
+ }
90
+ }
91
+ }
92
+ } catch (IOException e ) {
93
+ throw new IOException ("Error processing [" + taskInfo + "]" , e );
94
+ }
95
+ assertEquals (taskInfo , read );
96
+ }
97
+
98
+ private XContentBuilder addRandomUnknownFields (XContentBuilder builder ) throws IOException {
99
+ try (XContentParser parser = createParser (builder )) {
100
+ Map <String , Object > map = parser .mapOrdered ();
101
+ int numberOfNewFields = randomIntBetween (2 , 10 );
102
+ for (int i = 0 ; i < numberOfNewFields ; i ++) {
103
+ if (randomBoolean ()) {
104
+ map .put ("unknown_field" + i , randomAlphaOfLength (20 ));
105
+ } else {
106
+ map .put ("unknown_field" + i , Collections .singletonMap ("inner" , randomAlphaOfLength (20 )));
107
+ }
108
+ }
109
+ XContentBuilder xContentBuilder = XContentFactory .contentBuilder (parser .contentType ());
110
+ return xContentBuilder .map (map );
111
+ }
112
+ }
113
+
77
114
private static TaskResult randomTaskResult () throws IOException {
78
115
switch (between (0 , 2 )) {
79
- case 0 :
80
- return new TaskResult (randomBoolean (), randomTaskInfo ());
81
- case 1 :
82
- return new TaskResult (randomTaskInfo (), new RuntimeException ("error" ));
83
- case 2 :
84
- return new TaskResult (randomTaskInfo (), randomTaskResponse ());
85
- default :
86
- throw new UnsupportedOperationException ("Unsupported random TaskResult constructor" );
116
+ case 0 :
117
+ return new TaskResult (randomBoolean (), randomTaskInfo ());
118
+ case 1 :
119
+ return new TaskResult (randomTaskInfo (), new RuntimeException ("error" ));
120
+ case 2 :
121
+ return new TaskResult (randomTaskInfo (), randomTaskResponse ());
122
+ default :
123
+ throw new UnsupportedOperationException ("Unsupported random TaskResult constructor" );
87
124
}
88
125
}
89
126
0 commit comments