From 67b7fd43fb8975935f5be47ec7a45761a4d95d67 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 16 Jun 2021 15:59:22 +0200 Subject: [PATCH] [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 --- .../indices/RestValidateQueryAction.java | 19 +++++- .../indices/RestValidateQueryActionTests.java | 59 +++++++++++++++---- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java index fea94e15a9503..dde5f658d4888 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java @@ -15,7 +15,9 @@ 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.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 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 String getName() { @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)); diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java index 1160caa656871..c8e853c45be82 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java @@ -17,8 +17,11 @@ 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.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 @@ 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. *

* 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 @@ protected void doExecute(Task task, ActionRequest request, ActionListener listen controller.registerHandler(action); } - @AfterClass - public static void terminateThreadPool() { + @After + public void terminateThreadPool() { terminate(threadPool); threadPool = null; @@ -146,4 +149,38 @@ private RestRequest createRestRequest(String content) { .build(); } + public void testTypeInPath() { + List 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 compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7)); + + Map 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); + } }