Explorar o código

[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
lipsill %!s(int64=7) %!d(string=hai) anos
pai
achega
b48d5a8942

+ 1 - 9
test/framework/src/main/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParser.java

@@ -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--;
-                        }
                     }
                 }
 

+ 25 - 0
test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java

@@ -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" +