[TEST] ClientYamlSuiteRestApiParser to parse spec without path parts (#33720)

Previously ClientYamlSuiteRestApiParser threw an exception when an api
spec contained neither path parts nor url parameter sections.

Closes #31649
This commit is contained in:
lipsill 2018-09-21 17:26:55 +02:00 committed by Luca Cavanna
parent bf0a0f74da
commit b48d5a8942
2 changed files with 26 additions and 9 deletions

View File

@ -64,8 +64,7 @@ public class ClientYamlSuiteRestApiParser {
if ("url".equals(parser.currentName())) {
String currentFieldName = "url";
int innerLevel = -1;
while(parser.nextToken() != XContentParser.Token.END_OBJECT || innerLevel >= 0) {
while(parser.nextToken() != XContentParser.Token.END_OBJECT) {
if (parser.currentToken() == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
}
@ -108,13 +107,6 @@ public class ClientYamlSuiteRestApiParser {
restApi.addParam(param, PARAMETER_PARSER.parse(parser, null).isRequired());
}
}
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
innerLevel++;
}
if (parser.currentToken() == XContentParser.Token.END_OBJECT) {
innerLevel--;
}
}
}

View File

@ -91,6 +91,31 @@ public class ClientYamlSuiteRestApiParserTests extends AbstractClientYamlTestFra
assertThat(restApi.isBodyRequired(), equalTo(false));
}
public void testRequiredBodyWithoutUrlParts() throws Exception {
String spec = "{\n" +
" \"count\": {\n" +
" \"documentation\": \"whatever\",\n" +
" \"methods\": [ \"GET\", \"POST\" ],\n" +
" \"url\": {\n" +
" \"path\": \"/whatever\",\n" +
" \"paths\": [ \"/whatever\" ]\n" +
" },\n" +
" \"body\": {\n" +
" \"description\" : \"whatever\",\n" +
" \"required\" : true\n" +
" }\n" +
" }\n" +
"}";
parser = createParser(YamlXContent.yamlXContent, spec);
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("count.json", parser);
assertThat(restApi, notNullValue());
assertThat(restApi.getPathParts().isEmpty(), equalTo(true));
assertThat(restApi.getParams().isEmpty(), equalTo(true));
assertThat(restApi.isBodyRequired(), equalTo(true));
}
private static final String REST_SPEC_COUNT_API = "{\n" +
" \"count\": {\n" +
" \"documentation\": \"http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-count.html\",\n" +