Browse Source

[Rest Api Compatibility] Validate Query typed api (#74171)

Adds back typed endpoints for validate query api.
Previously removed in #46927

relates main meta issue #51816
relates types removal issue #54160
Przemyslaw Gomulka 4 years ago
parent
commit
67b7fd43fb

+ 17 - 2
server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java

@@ -15,7 +15,9 @@ import org.elasticsearch.action.support.IndicesOptions;
 import org.elasticsearch.client.node.NodeClient;
 import org.elasticsearch.common.ParsingException;
 import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.logging.DeprecationLogger;
 import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.core.RestApiVersion;
 import org.elasticsearch.rest.BaseRestHandler;
 import org.elasticsearch.rest.BytesRestResponse;
 import org.elasticsearch.rest.RestChannel;
@@ -31,14 +33,22 @@ import static org.elasticsearch.rest.RestRequest.Method.POST;
 import static org.elasticsearch.rest.RestStatus.OK;
 
 public class RestValidateQueryAction extends BaseRestHandler {
-
+    private static final DeprecationLogger deprecationLogger =  DeprecationLogger.getLogger(RestValidateQueryAction.class);
+    static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
+        " Specifying types in validate query requests is deprecated.";
     @Override
     public List<Route> routes() {
         return List.of(
             new Route(GET, "/_validate/query"),
             new Route(POST, "/_validate/query"),
             new Route(GET, "/{index}/_validate/query"),
-            new Route(POST, "/{index}/_validate/query"));
+            new Route(POST, "/{index}/_validate/query"),
+            Route.builder(GET, "/{index}/{type}/_validate/query")
+                .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
+                .build(),
+            Route.builder(POST, "/{index}/{type}/_validate/query")
+                .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
+                .build());
     }
 
     @Override
@@ -48,6 +58,11 @@ public class RestValidateQueryAction extends BaseRestHandler {
 
     @Override
     public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
+        if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
+            deprecationLogger.compatibleApiWarning("validate_query_with_types", TYPES_DEPRECATION_MESSAGE);
+            request.param("type");
+        }
+
         ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
         validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
         validateQueryRequest.explain(request.paramAsBoolean("explain", false));

+ 48 - 11
server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java

@@ -17,8 +17,11 @@ import org.elasticsearch.client.node.NodeClient;
 import org.elasticsearch.common.bytes.BytesArray;
 import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
 import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.util.concurrent.ThreadContext;
 import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.core.RestApiVersion;
 import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
+import org.elasticsearch.rest.RestChannel;
 import org.elasticsearch.rest.RestController;
 import org.elasticsearch.rest.RestRequest;
 import org.elasticsearch.search.AbstractSearchTestCase;
@@ -30,8 +33,8 @@ import org.elasticsearch.threadpool.TestThreadPool;
 import org.elasticsearch.threadpool.ThreadPool;
 import org.elasticsearch.transport.Transport;
 import org.elasticsearch.usage.UsageService;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.After;
+import org.junit.Before;
 
 import java.util.Collections;
 import java.util.HashMap;
@@ -46,21 +49,21 @@ import static org.mockito.Mockito.mock;
 
 public class RestValidateQueryActionTests extends AbstractSearchTestCase {
 
-    private static ThreadPool threadPool = new TestThreadPool(RestValidateQueryActionTests.class.getName());
-    private static NodeClient client = new NodeClient(Settings.EMPTY, threadPool);
+    private ThreadPool threadPool = new TestThreadPool(RestValidateQueryActionTests.class.getName());
+    private NodeClient client = new NodeClient(Settings.EMPTY, threadPool);
 
-    private static UsageService usageService = new UsageService();
-    private static RestController controller = new RestController(emptySet(), null, client,
+    private UsageService usageService = new UsageService();
+    private RestController controller = new RestController(emptySet(), null, client,
         new NoneCircuitBreakerService(), usageService);
-    private static RestValidateQueryAction action = new RestValidateQueryAction();
+    private RestValidateQueryAction action = new RestValidateQueryAction();
 
     /**
      * Configures {@link NodeClient} to stub {@link ValidateQueryAction} transport action.
      * <p>
      * This lower level of validation is out of the scope of this test.
      */
-    @BeforeClass
-    public static void stubValidateQueryAction() {
+    @Before
+    public void stubValidateQueryAction() {
         final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
 
         final TransportAction transportAction = new TransportAction(ValidateQueryAction.NAME,
@@ -78,8 +81,8 @@ public class RestValidateQueryActionTests extends AbstractSearchTestCase {
         controller.registerHandler(action);
     }
 
-    @AfterClass
-    public static void terminateThreadPool() {
+    @After
+    public void terminateThreadPool() {
         terminate(threadPool);
 
         threadPool = null;
@@ -146,4 +149,38 @@ public class RestValidateQueryActionTests extends AbstractSearchTestCase {
             .build();
     }
 
+    public void testTypeInPath() {
+        List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));
+
+        RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
+            .withHeaders(Map.of("Accept", compatibleMediaType))
+            .withMethod(RestRequest.Method.GET)
+            .withPath("/some_index/some_type/_validate/query")
+            .build();
+
+        performRequest(request);
+        assertWarnings(RestValidateQueryAction.TYPES_DEPRECATION_MESSAGE);
+    }
+
+    public void testTypeParameter() {
+        List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));
+
+        Map<String, String> params = new HashMap<>();
+        params.put("type", "some_type");
+        RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
+            .withHeaders(Map.of("Accept", compatibleMediaType))
+            .withMethod(RestRequest.Method.GET)
+            .withPath("_validate/query")
+            .withParams(params)
+            .build();
+
+        performRequest(request);
+        assertWarnings(RestValidateQueryAction.TYPES_DEPRECATION_MESSAGE);
+    }
+
+    private void performRequest(RestRequest request) {
+        RestChannel channel = new FakeRestChannel(request, false, 1);
+        ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
+        controller.dispatchRequest(request, channel, threadContext);
+    }
 }