Skip to content

Commit 67b7fd4

Browse files
authored
[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
1 parent e124824 commit 67b7fd4

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import org.elasticsearch.client.node.NodeClient;
1616
import org.elasticsearch.common.ParsingException;
1717
import org.elasticsearch.common.Strings;
18+
import org.elasticsearch.common.logging.DeprecationLogger;
1819
import org.elasticsearch.common.xcontent.XContentBuilder;
20+
import org.elasticsearch.core.RestApiVersion;
1921
import org.elasticsearch.rest.BaseRestHandler;
2022
import org.elasticsearch.rest.BytesRestResponse;
2123
import org.elasticsearch.rest.RestChannel;
@@ -31,14 +33,22 @@
3133
import static org.elasticsearch.rest.RestStatus.OK;
3234

3335
public class RestValidateQueryAction extends BaseRestHandler {
34-
36+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestValidateQueryAction.class);
37+
static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
38+
" Specifying types in validate query requests is deprecated.";
3539
@Override
3640
public List<Route> routes() {
3741
return List.of(
3842
new Route(GET, "/_validate/query"),
3943
new Route(POST, "/_validate/query"),
4044
new Route(GET, "/{index}/_validate/query"),
41-
new Route(POST, "/{index}/_validate/query"));
45+
new Route(POST, "/{index}/_validate/query"),
46+
Route.builder(GET, "/{index}/{type}/_validate/query")
47+
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
48+
.build(),
49+
Route.builder(POST, "/{index}/{type}/_validate/query")
50+
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
51+
.build());
4252
}
4353

4454
@Override
@@ -48,6 +58,11 @@ public String getName() {
4858

4959
@Override
5060
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
61+
if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
62+
deprecationLogger.compatibleApiWarning("validate_query_with_types", TYPES_DEPRECATION_MESSAGE);
63+
request.param("type");
64+
}
65+
5166
ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
5267
validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
5368
validateQueryRequest.explain(request.paramAsBoolean("explain", false));

server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import org.elasticsearch.common.bytes.BytesArray;
1818
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1919
import org.elasticsearch.common.settings.Settings;
20+
import org.elasticsearch.common.util.concurrent.ThreadContext;
2021
import org.elasticsearch.common.xcontent.XContentType;
22+
import org.elasticsearch.core.RestApiVersion;
2123
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
24+
import org.elasticsearch.rest.RestChannel;
2225
import org.elasticsearch.rest.RestController;
2326
import org.elasticsearch.rest.RestRequest;
2427
import org.elasticsearch.search.AbstractSearchTestCase;
@@ -30,8 +33,8 @@
3033
import org.elasticsearch.threadpool.ThreadPool;
3134
import org.elasticsearch.transport.Transport;
3235
import org.elasticsearch.usage.UsageService;
33-
import org.junit.AfterClass;
34-
import org.junit.BeforeClass;
36+
import org.junit.After;
37+
import org.junit.Before;
3538

3639
import java.util.Collections;
3740
import java.util.HashMap;
@@ -46,21 +49,21 @@
4649

4750
public class RestValidateQueryActionTests extends AbstractSearchTestCase {
4851

49-
private static ThreadPool threadPool = new TestThreadPool(RestValidateQueryActionTests.class.getName());
50-
private static NodeClient client = new NodeClient(Settings.EMPTY, threadPool);
52+
private ThreadPool threadPool = new TestThreadPool(RestValidateQueryActionTests.class.getName());
53+
private NodeClient client = new NodeClient(Settings.EMPTY, threadPool);
5154

52-
private static UsageService usageService = new UsageService();
53-
private static RestController controller = new RestController(emptySet(), null, client,
55+
private UsageService usageService = new UsageService();
56+
private RestController controller = new RestController(emptySet(), null, client,
5457
new NoneCircuitBreakerService(), usageService);
55-
private static RestValidateQueryAction action = new RestValidateQueryAction();
58+
private RestValidateQueryAction action = new RestValidateQueryAction();
5659

5760
/**
5861
* Configures {@link NodeClient} to stub {@link ValidateQueryAction} transport action.
5962
* <p>
6063
* This lower level of validation is out of the scope of this test.
6164
*/
62-
@BeforeClass
63-
public static void stubValidateQueryAction() {
65+
@Before
66+
public void stubValidateQueryAction() {
6467
final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
6568

6669
final TransportAction transportAction = new TransportAction(ValidateQueryAction.NAME,
@@ -78,8 +81,8 @@ protected void doExecute(Task task, ActionRequest request, ActionListener listen
7881
controller.registerHandler(action);
7982
}
8083

81-
@AfterClass
82-
public static void terminateThreadPool() {
84+
@After
85+
public void terminateThreadPool() {
8386
terminate(threadPool);
8487

8588
threadPool = null;
@@ -146,4 +149,38 @@ private RestRequest createRestRequest(String content) {
146149
.build();
147150
}
148151

152+
public void testTypeInPath() {
153+
List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));
154+
155+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
156+
.withHeaders(Map.of("Accept", compatibleMediaType))
157+
.withMethod(RestRequest.Method.GET)
158+
.withPath("/some_index/some_type/_validate/query")
159+
.build();
160+
161+
performRequest(request);
162+
assertWarnings(RestValidateQueryAction.TYPES_DEPRECATION_MESSAGE);
163+
}
164+
165+
public void testTypeParameter() {
166+
List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));
167+
168+
Map<String, String> params = new HashMap<>();
169+
params.put("type", "some_type");
170+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
171+
.withHeaders(Map.of("Accept", compatibleMediaType))
172+
.withMethod(RestRequest.Method.GET)
173+
.withPath("_validate/query")
174+
.withParams(params)
175+
.build();
176+
177+
performRequest(request);
178+
assertWarnings(RestValidateQueryAction.TYPES_DEPRECATION_MESSAGE);
179+
}
180+
181+
private void performRequest(RestRequest request) {
182+
RestChannel channel = new FakeRestChannel(request, false, 1);
183+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
184+
controller.dispatchRequest(request, channel, threadContext);
185+
}
149186
}

0 commit comments

Comments
 (0)