Skip to content

Commit

Permalink
[Rest Api Compatibility] Validate Query typed api
Browse files Browse the repository at this point in the history
Adds back typed endpoints for validate query api.
Previously removed in elastic#46927

relates main meta issue elastic#51816
relates types removal issue elastic#54160
  • Loading branch information
pgomulka committed Jun 16, 2021
1 parent 51ec860 commit 3fdc8de
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
* <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,
Expand All @@ -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;
Expand Down Expand Up @@ -146,4 +149,38 @@ private RestRequest createRestRequest(String content) {
.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);
}
}

0 comments on commit 3fdc8de

Please sign in to comment.