Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -140,94 +140,103 @@ public static FetchSourceContext parseFromRestRequest(RestRequest request) {

public static FetchSourceContext fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken();
boolean fetchSource = true;
switch (token) {
case XContentParser.Token.VALUE_BOOLEAN -> {
return parser.booleanValue() ? FETCH_SOURCE : DO_NOT_FETCH_SOURCE;
}
case XContentParser.Token.VALUE_STRING -> {
String[] includes = new String[] { parser.text() };
return new FetchSourceContext(true, includes, null);
}
case XContentParser.Token.START_ARRAY -> {
ArrayList<String> list = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
list.add(parser.text());
}
String[] includes = list.toArray(new String[0]);
return new FetchSourceContext(true, includes, null);
Comment thread
sandeshkr419 marked this conversation as resolved.
}
case XContentParser.Token.START_OBJECT -> {
return parseSourceObject(parser);
}
default -> {
throw new ParsingException(
parser.getTokenLocation(),
"Expected one of ["
+ XContentParser.Token.VALUE_BOOLEAN
+ ", "
+ XContentParser.Token.START_OBJECT
+ "] but found ["
+ token
+ "]"
);
}
}
}

private static FetchSourceContext parseSourceObject(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken();
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = Strings.EMPTY_ARRAY;
if (token == XContentParser.Token.VALUE_BOOLEAN) {
fetchSource = parser.booleanValue();
} else if (token == XContentParser.Token.VALUE_STRING) {
includes = new String[] { parser.text() };
} else if (token == XContentParser.Token.START_ARRAY) {
ArrayList<String> list = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
list.add(parser.text());
String currentFieldName = null;
if (token != XContentParser.Token.START_OBJECT) {
throw new ParsingException(
parser.getTokenLocation(),
"Expected a " + XContentParser.Token.START_OBJECT + " but got a " + token + " in [" + parser.currentName() + "]."
);
}
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
continue; // only field name is required in this iteration
}
includes = list.toArray(new String[0]);
} else if (token == XContentParser.Token.START_OBJECT) {
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
// process field value
switch (token) {
case XContentParser.Token.START_ARRAY -> {
if (INCLUDES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
List<String> includesList = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.VALUE_STRING) {
includesList.add(parser.text());
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + currentFieldName + "].",
parser.getTokenLocation()
);
}
}
includes = includesList.toArray(new String[0]);
includes = parseSourceArray(parser).toArray(new String[0]);
} else if (EXCLUDES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
List<String> excludesList = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.VALUE_STRING) {
excludesList.add(parser.text());
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + currentFieldName + "].",
parser.getTokenLocation()
);
}
}
excludes = excludesList.toArray(new String[0]);
excludes = parseSourceArray(parser).toArray(new String[0]);
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + currentFieldName + "].",
parser.getTokenLocation()
"Unknown key for a " + token + " in [" + currentFieldName + "]."
);
}
} else if (token == XContentParser.Token.VALUE_STRING) {
}
case XContentParser.Token.VALUE_STRING -> {
if (INCLUDES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
includes = new String[] { parser.text() };
} else if (EXCLUDES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
excludes = new String[] { parser.text() };
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + currentFieldName + "].",
parser.getTokenLocation()
"Unknown key for a " + token + " in [" + currentFieldName + "]."
);
}
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + currentFieldName + "].",
parser.getTokenLocation()
);
}
default -> {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].");
}
}
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Expected one of ["
+ XContentParser.Token.VALUE_BOOLEAN
+ ", "
+ XContentParser.Token.START_OBJECT
+ "] but found ["
+ token
+ "]",
parser.getTokenLocation()
);
}
return new FetchSourceContext(fetchSource, includes, excludes);
return new FetchSourceContext(true, includes, excludes);
}

private static List<String> parseSourceArray(XContentParser parser) throws IOException {
List<String> sourceArr = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
sourceArr.add(parser.text());
} else {
throw new ParsingException(
parser.getTokenLocation(),
"Unknown key for a " + parser.currentToken() + " in [" + parser.currentName() + "]."
);
}
}
return sourceArr;
}

@Override
Expand Down
Loading