@@ -74,6 +74,11 @@ public class StoredScriptSource extends AbstractDiffable<StoredScriptSource> imp
74
74
*/
75
75
public static final ParseField TEMPLATE_PARSE_FIELD = new ParseField ("template" );
76
76
77
+ /**
78
+ * Standard {@link ParseField} for query on the inner field.
79
+ */
80
+ public static final ParseField TEMPLATE_NO_WRAPPER_PARSE_FIELD = new ParseField ("query" );
81
+
77
82
/**
78
83
* Standard {@link ParseField} for lang on the inner level.
79
84
*/
@@ -189,6 +194,26 @@ private StoredScriptSource build(boolean ignoreEmpty) {
189
194
PARSER .declareField (Builder ::setOptions , XContentParser ::mapStrings , OPTIONS_PARSE_FIELD , ValueType .OBJECT );
190
195
}
191
196
197
+ private static StoredScriptSource parseRemaining (Token token , XContentParser parser ) throws IOException {
198
+ try (XContentBuilder builder = XContentFactory .jsonBuilder ()) {
199
+ if (token != Token .START_OBJECT ) {
200
+ builder .startObject ();
201
+ builder .copyCurrentStructure (parser );
202
+ builder .endObject ();
203
+ } else {
204
+ builder .copyCurrentStructure (parser );
205
+ }
206
+
207
+ String source = Strings .toString (builder );
208
+
209
+ if (source == null || source .isEmpty ()) {
210
+ DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
211
+ }
212
+
213
+ return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , source , Collections .emptyMap ());
214
+ }
215
+ }
216
+
192
217
/**
193
218
* This will parse XContent into a {@link StoredScriptSource}. The following formats can be parsed:
194
219
*
@@ -304,38 +329,28 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
304
329
} else {
305
330
throw new ParsingException (parser .getTokenLocation (), "unexpected token [" + token + "], expected [{, <source>]" );
306
331
}
307
- } else {
308
- if (TEMPLATE_PARSE_FIELD .getPreferredName ().equals (name )) {
309
- token = parser .nextToken ();
310
-
311
- if (token == Token .VALUE_STRING ) {
312
- String source = parser .text ();
313
-
314
- if (source == null || source .isEmpty ()) {
315
- DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
316
- }
317
-
318
- return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , source , Collections .emptyMap ());
319
- }
320
- }
332
+ } else if (TEMPLATE_PARSE_FIELD .getPreferredName ().equals (name )) {
321
333
322
- try (XContentBuilder builder = XContentFactory .jsonBuilder ()) {
323
- if (token != Token .START_OBJECT ) {
324
- builder .startObject ();
325
- builder .copyCurrentStructure (parser );
326
- builder .endObject ();
327
- } else {
328
- builder .copyCurrentStructure (parser );
329
- }
334
+ DEPRECATION_LOGGER .deprecated ("the template context is now deprecated. Specify templates in a \" script\" element." );
330
335
331
- String source = Strings .toString (builder );
336
+ token = parser .nextToken ();
337
+ if (token == Token .VALUE_STRING ) {
338
+ String source = parser .text ();
332
339
333
340
if (source == null || source .isEmpty ()) {
334
341
DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
335
342
}
336
343
337
344
return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , source , Collections .emptyMap ());
345
+ } else {
346
+ return parseRemaining (token , parser );
338
347
}
348
+ } else if (TEMPLATE_NO_WRAPPER_PARSE_FIELD .getPreferredName ().equals (name )) {
349
+ DEPRECATION_LOGGER .deprecated ("the template context is now deprecated. Specify templates in a \" script\" element." );
350
+ return parseRemaining (token , parser );
351
+ } else {
352
+ DEPRECATION_LOGGER .deprecated ("scripts should not be stored without a context. Specify them in a \" script\" element." );
353
+ return parseRemaining (token , parser );
339
354
}
340
355
} catch (IOException ioe ) {
341
356
throw new UncheckedIOException (ioe );
0 commit comments