Improve handling of empty response (#125562)

Today `ActionResponse$Empty` implements `ToXContentObject`, but yields
no bytes of content when serialized which creates an invalid JSON
response. This commit removes the bogus interface and adjusts the
affected REST APIs to send a `text/plain` response instead.
This commit is contained in:
David Turner 2025-04-07 12:10:07 +01:00 committed by GitHub
parent a152b4e29b
commit 527d2a203b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 217 additions and 51 deletions

View File

@ -0,0 +1,6 @@
pr: 125562
summary: Improve handling of empty response
area: Infra/REST API
type: bug
issues:
- 57639

View File

@ -180,6 +180,11 @@ setup:
- requires: - requires:
cluster_features: ["gte_v8.8.0"] cluster_features: ["gte_v8.8.0"]
reason: "reset API added in in 8.8.0" reason: "reset API added in in 8.8.0"
test_runner_features: [ capabilities ]
capabilities:
- method: DELETE
path: /_internal/desired_balance
capabilities: [ plain_text_empty_response ]
- do: - do:
_internal.delete_desired_balance: { } _internal.delete_desired_balance: { }

View File

@ -3,6 +3,11 @@ setup:
- requires: - requires:
cluster_features: ["gte_v8.13.0"] cluster_features: ["gte_v8.13.0"]
reason: "API added in in 8.1.0 but modified in 8.13 (node_version field removed)" reason: "API added in in 8.1.0 but modified in 8.13 (node_version field removed)"
test_runner_features: [ capabilities ]
capabilities:
- method: DELETE
path: /_internal/desired_nodes
capabilities: [ plain_text_empty_response ]
--- ---
teardown: teardown:
- do: - do:

View File

@ -6,6 +6,11 @@ setup:
- requires: - requires:
cluster_features: ["gte_v8.3.0"] cluster_features: ["gte_v8.3.0"]
reason: "API added in in 8.1.0 but modified in 8.3" reason: "API added in in 8.1.0 but modified in 8.3"
test_runner_features: [ capabilities ]
capabilities:
- method: DELETE
path: /_internal/desired_nodes
capabilities: [ plain_text_empty_response ]
--- ---
teardown: teardown:
- do: - do:

View File

@ -3,6 +3,11 @@ setup:
- requires: - requires:
cluster_features: ["gte_v8.4.0"] cluster_features: ["gte_v8.4.0"]
reason: "Support for the dry run option was added in in 8.4.0" reason: "Support for the dry run option was added in in 8.4.0"
test_runner_features: [ capabilities ]
capabilities:
- method: DELETE
path: /_internal/desired_nodes
capabilities: [ plain_text_empty_response ]
--- ---
teardown: teardown:
- do: - do:

View File

@ -1,3 +1,16 @@
setup:
- requires:
test_runner_features: [ capabilities ]
capabilities:
- method: POST
path: /_cluster/voting_config_exclusions
capabilities: [ plain_text_empty_response ]
- method: DELETE
path: /_cluster/voting_config_exclusions
capabilities: [ plain_text_empty_response ]
reason: needs these capabilities
---
teardown: teardown:
- do: - do:
cluster.delete_voting_config_exclusions: {} cluster.delete_voting_config_exclusions: {}

View File

@ -9,22 +9,24 @@
package org.elasticsearch.cluster.coordination; package org.elasticsearch.cluster.coordination;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest; import org.elasticsearch.client.Request;
import org.elasticsearch.action.admin.cluster.configuration.TransportAddVotingConfigExclusionsAction; import org.elasticsearch.client.Response;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Priority; import org.elasticsearch.common.Priority;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.test.transport.MockTransportService;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutionException;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
@ -38,18 +40,39 @@ public class VotingConfigurationIT extends ESIntegTestCase {
return Collections.singletonList(MockTransportService.TestPlugin.class); return Collections.singletonList(MockTransportService.TestPlugin.class);
} }
public void testAbdicateAfterVotingConfigExclusionAdded() throws ExecutionException, InterruptedException { @Override
protected boolean addMockHttpTransport() {
return false; // enable HTTP
}
public void testAbdicateAfterVotingConfigExclusionAdded() throws IOException {
internalCluster().setBootstrapMasterNodeIndex(0); internalCluster().setBootstrapMasterNodeIndex(0);
internalCluster().startNodes(2); internalCluster().startNodes(2);
final String originalMaster = internalCluster().getMasterName(); final String originalMaster = internalCluster().getMasterName();
final var restClient = getRestClient();
logger.info("--> excluding master node {}", originalMaster); logger.info("--> excluding master node {}", originalMaster);
client().execute( final var excludeRequest = new Request("POST", "/_cluster/voting_config_exclusions");
TransportAddVotingConfigExclusionsAction.TYPE, excludeRequest.addParameter("node_names", originalMaster);
new AddVotingConfigExclusionsRequest(TEST_REQUEST_TIMEOUT, originalMaster) assertEmptyResponse(restClient.performRequest(excludeRequest));
).get();
clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT).setWaitForEvents(Priority.LANGUID).get(); clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT).setWaitForEvents(Priority.LANGUID).get();
assertNotEquals(originalMaster, internalCluster().getMasterName()); assertNotEquals(originalMaster, internalCluster().getMasterName());
final var clearRequest = new Request("DELETE", "/_cluster/voting_config_exclusions");
clearRequest.addParameter("wait_for_removal", "false");
assertEmptyResponse(restClient.performRequest(clearRequest));
assertThat(
internalCluster().getInstance(ClusterService.class).state().metadata().coordinationMetadata().getVotingConfigExclusions(),
empty()
);
}
private void assertEmptyResponse(Response response) throws IOException {
assertEquals("text/plain; charset=UTF-8", response.getHeader("content-type"));
assertEquals(0, response.getEntity().getContentLength());
assertEquals(0, response.getEntity().getContent().readAllBytes().length);
} }
public void testElectsNodeNotInVotingConfiguration() throws Exception { public void testElectsNodeNotInVotingConfiguration() throws Exception {

View File

@ -10,9 +10,10 @@
package org.elasticsearch.action; package org.elasticsearch.action;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.action.EmptyResponseListener;
import org.elasticsearch.transport.TransportResponse; import org.elasticsearch.transport.TransportResponse;
import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContent;
/** /**
* Base class for responses to action requests. * Base class for responses to action requests.
@ -21,20 +22,23 @@ public abstract class ActionResponse extends TransportResponse {
public ActionResponse() {} public ActionResponse() {}
public static final class Empty extends ActionResponse implements ToXContentObject { /**
* A response with no payload. This is deliberately not an implementation of {@link ToXContent} or similar because an empty response
* has no valid {@link XContent} representation. Use {@link EmptyResponseListener} to convert this to a valid (plain-text) REST
* response instead.
*/
public static final class Empty extends ActionResponse {
private Empty() { /* singleton */ }
public static final ActionResponse.Empty INSTANCE = new ActionResponse.Empty(); public static final ActionResponse.Empty INSTANCE = new ActionResponse.Empty();
@Override @Override
public String toString() { public String toString() {
return "EmptyActionResponse{}"; return "ActionResponse.Empty{}";
} }
@Override @Override
public void writeTo(StreamOutput out) {} public void writeTo(StreamOutput out) {}
@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) {
return builder;
}
} }
} }

View File

@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
package org.elasticsearch.rest.action;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
/**
* A listener which converts a successful {@link ActionResponse.Empty} action response into a {@code 200 OK} REST response with empty body.
*/
public final class EmptyResponseListener extends RestResponseListener<ActionResponse.Empty> {
public EmptyResponseListener(RestChannel channel) {
super(channel);
}
@Override
public RestResponse buildResponse(ActionResponse.Empty ignored) throws Exception {
// Content-type header is not required for an empty body but some clients may expect it; the empty body is a valid text/plain entity
// so we use that here.
return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
/**
* Capability name for APIs that previously would return an invalid zero-byte {@code application/json} response so that the YAML test
* runner can avoid those APIs.
*/
public static final String PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME = "plain_text_empty_response";
}

View File

@ -27,6 +27,7 @@ public abstract class RestBuilderListener<Response> extends RestResponseListener
try (XContentBuilder builder = channel.newBuilder()) { try (XContentBuilder builder = channel.newBuilder()) {
final RestResponse restResponse = buildResponse(response, builder); final RestResponse restResponse = buildResponse(response, builder);
assert assertBuilderClosed(builder); assert assertBuilderClosed(builder);
assert restResponse.content() != null && restResponse.content().length() > 0 : "Use EmptyResponseListener for empty responses";
return restResponse; return restResponse;
} }
} }

View File

@ -16,13 +16,15 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.TimeValue;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.rest.action.EmptyResponseListener;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.POST; import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout; import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout;
import static org.elasticsearch.rest.action.EmptyResponseListener.PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME;
public class RestAddVotingConfigExclusionAction extends BaseRestHandler { public class RestAddVotingConfigExclusionAction extends BaseRestHandler {
private static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(30L); private static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(30L);
@ -37,6 +39,11 @@ public class RestAddVotingConfigExclusionAction extends BaseRestHandler {
return List.of(new Route(POST, "/_cluster/voting_config_exclusions")); return List.of(new Route(POST, "/_cluster/voting_config_exclusions"));
} }
@Override
public Set<String> supportedCapabilities() {
return Set.of(PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME);
}
@Override @Override
public boolean canTripCircuitBreaker() { public boolean canTripCircuitBreaker() {
return false; return false;
@ -48,7 +55,7 @@ public class RestAddVotingConfigExclusionAction extends BaseRestHandler {
return channel -> client.execute( return channel -> client.execute(
TransportAddVotingConfigExclusionsAction.TYPE, TransportAddVotingConfigExclusionsAction.TYPE,
votingConfigExclusionsRequest, votingConfigExclusionsRequest,
new RestToXContentListener<>(channel) new EmptyResponseListener(channel)
); );
} }

View File

@ -14,13 +14,15 @@ import org.elasticsearch.action.admin.cluster.configuration.TransportClearVoting
import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.rest.action.EmptyResponseListener;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.DELETE; import static org.elasticsearch.rest.RestRequest.Method.DELETE;
import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout; import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout;
import static org.elasticsearch.rest.action.EmptyResponseListener.PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME;
public class RestClearVotingConfigExclusionsAction extends BaseRestHandler { public class RestClearVotingConfigExclusionsAction extends BaseRestHandler {
@ -39,10 +41,15 @@ public class RestClearVotingConfigExclusionsAction extends BaseRestHandler {
return "clear_voting_config_exclusions_action"; return "clear_voting_config_exclusions_action";
} }
@Override
public Set<String> supportedCapabilities() {
return Set.of(PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME);
}
@Override @Override
protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final var req = resolveVotingConfigExclusionsRequest(request); final var req = resolveVotingConfigExclusionsRequest(request);
return channel -> client.execute(TransportClearVotingConfigExclusionsAction.TYPE, req, new RestToXContentListener<>(channel)); return channel -> client.execute(TransportClearVotingConfigExclusionsAction.TYPE, req, new EmptyResponseListener(channel));
} }
static ClearVotingConfigExclusionsRequest resolveVotingConfigExclusionsRequest(final RestRequest request) { static ClearVotingConfigExclusionsRequest resolveVotingConfigExclusionsRequest(final RestRequest request) {

View File

@ -17,10 +17,13 @@ import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestUtils; import org.elasticsearch.rest.RestUtils;
import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.Scope;
import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.ServerlessScope;
import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.rest.action.EmptyResponseListener;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.action.EmptyResponseListener.PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME;
@ServerlessScope(Scope.INTERNAL) @ServerlessScope(Scope.INTERNAL)
public class RestDeleteDesiredBalanceAction extends BaseRestHandler { public class RestDeleteDesiredBalanceAction extends BaseRestHandler {
@ -35,9 +38,14 @@ public class RestDeleteDesiredBalanceAction extends BaseRestHandler {
return List.of(new Route(RestRequest.Method.DELETE, "_internal/desired_balance")); return List.of(new Route(RestRequest.Method.DELETE, "_internal/desired_balance"));
} }
@Override
public Set<String> supportedCapabilities() {
return Set.of(PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME);
}
@Override @Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final var req = new DesiredBalanceRequest(RestUtils.getMasterNodeTimeout(request)); final var req = new DesiredBalanceRequest(RestUtils.getMasterNodeTimeout(request));
return channel -> client.execute(TransportDeleteDesiredBalanceAction.TYPE, req, new RestToXContentListener<>(channel)); return channel -> client.execute(TransportDeleteDesiredBalanceAction.TYPE, req, new EmptyResponseListener(channel));
} }
} }

View File

@ -14,13 +14,15 @@ import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.rest.action.EmptyResponseListener;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestUtils.getAckTimeout; import static org.elasticsearch.rest.RestUtils.getAckTimeout;
import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout; import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout;
import static org.elasticsearch.rest.action.EmptyResponseListener.PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME;
public class RestDeleteDesiredNodesAction extends BaseRestHandler { public class RestDeleteDesiredNodesAction extends BaseRestHandler {
@Override @Override
@ -33,13 +35,18 @@ public class RestDeleteDesiredNodesAction extends BaseRestHandler {
return List.of(new Route(RestRequest.Method.DELETE, "_internal/desired_nodes")); return List.of(new Route(RestRequest.Method.DELETE, "_internal/desired_nodes"));
} }
@Override
public Set<String> supportedCapabilities() {
return Set.of(PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME);
}
@Override @Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final var deleteDesiredNodesRequest = new AcknowledgedRequest.Plain(getMasterNodeTimeout(request), getAckTimeout(request)); final var deleteDesiredNodesRequest = new AcknowledgedRequest.Plain(getMasterNodeTimeout(request), getAckTimeout(request));
return restChannel -> client.execute( return restChannel -> client.execute(
TransportDeleteDesiredNodesAction.TYPE, TransportDeleteDesiredNodesAction.TYPE,
deleteDesiredNodesRequest, deleteDesiredNodesRequest,
new RestToXContentListener<>(restChannel) new EmptyResponseListener(restChannel)
); );
} }
} }

View File

@ -23,16 +23,19 @@ import java.util.concurrent.atomic.AtomicReference;
public class RestBuilderListenerTests extends ESTestCase { public class RestBuilderListenerTests extends ESTestCase {
// bypass the check that XContent responses are never empty - we're ignoring the builder and sending a text/plain response anyway
private static final BytesArray NONEMPTY_BODY = new BytesArray(new byte[] { '\n' });
public void testXContentBuilderClosedInBuildResponse() throws Exception { public void testXContentBuilderClosedInBuildResponse() throws Exception {
AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>(); AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>( RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<>(
new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1) new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)
) { ) {
@Override @Override
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception { public RestResponse buildResponse(Empty empty, XContentBuilder builder) {
builderAtomicReference.set(builder); builderAtomicReference.set(builder);
builder.close(); builder.close();
return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, NONEMPTY_BODY);
} }
}; };
@ -43,13 +46,13 @@ public class RestBuilderListenerTests extends ESTestCase {
public void testXContentBuilderNotClosedInBuildResponseAssertionsDisabled() throws Exception { public void testXContentBuilderNotClosedInBuildResponseAssertionsDisabled() throws Exception {
AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>(); AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>( RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<>(
new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1) new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)
) { ) {
@Override @Override
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception { public RestResponse buildResponse(Empty empty, XContentBuilder builder) {
builderAtomicReference.set(builder); builderAtomicReference.set(builder);
return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, NONEMPTY_BODY);
} }
@Override @Override
@ -64,15 +67,15 @@ public class RestBuilderListenerTests extends ESTestCase {
assertTrue(builderAtomicReference.get().generator().isClosed()); assertTrue(builderAtomicReference.get().generator().isClosed());
} }
public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception { public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() {
assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus()); assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus());
RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>( RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<>(
new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1) new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)
) { ) {
@Override @Override
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception { public RestResponse buildResponse(Empty empty, XContentBuilder builder) {
return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); return new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, NONEMPTY_BODY);
} }
}; };

View File

@ -64,6 +64,6 @@ public class QueryVectorBuilderTests extends AbstractQueryVectorBuilderTestCase<
@Override @Override
protected ActionResponse createResponse(float[] array, TestQueryVectorBuilderPlugin.TestQueryVectorBuilder builder) { protected ActionResponse createResponse(float[] array, TestQueryVectorBuilderPlugin.TestQueryVectorBuilder builder) {
return new ActionResponse.Empty(); return ActionResponse.Empty.INSTANCE;
} }
} }

View File

@ -50,6 +50,9 @@ public class ClientYamlTestResponse {
byte[] bytes = EntityUtils.toByteArray(response.getEntity()); byte[] bytes = EntityUtils.toByteArray(response.getEntity());
// skip parsing if we got text back (e.g. if we called _cat apis) // skip parsing if we got text back (e.g. if we called _cat apis)
if (bodyContentType != null) { if (bodyContentType != null) {
if (bytes.length == 0) {
throw new IllegalArgumentException("Empty body is invalid for content-type [" + contentType + "]");
}
this.parsedResponse = ObjectPath.createFromXContent(bodyContentType.xContent(), new BytesArray(bytes)); this.parsedResponse = ObjectPath.createFromXContent(bodyContentType.xContent(), new BytesArray(bytes));
} }
this.body = bytes; this.body = bytes;

View File

@ -67,7 +67,7 @@ public class DeprecationCacheResetAction extends ActionType<DeprecationCacheRese
} }
} }
public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable, ToXContentObject { public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable {
public Response(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) { public Response(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) {
super(clusterName, nodes, failures); super(clusterName, nodes, failures);
} }
@ -82,11 +82,6 @@ public class DeprecationCacheResetAction extends ActionType<DeprecationCacheRese
TransportAction.localOnly(); TransportAction.localOnly();
} }
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) {
return builder;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -7,15 +7,17 @@
package org.elasticsearch.xpack.deprecation.logging; package org.elasticsearch.xpack.deprecation.logging;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.rest.action.EmptyResponseListener;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.DELETE; import static org.elasticsearch.rest.RestRequest.Method.DELETE;
import static org.elasticsearch.rest.action.EmptyResponseListener.PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME;
public class RestDeprecationCacheResetAction extends BaseRestHandler { public class RestDeprecationCacheResetAction extends BaseRestHandler {
@ -30,8 +32,17 @@ public class RestDeprecationCacheResetAction extends BaseRestHandler {
} }
@Override @Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { public Set<String> supportedCapabilities() {
return Set.of(PLAIN_TEXT_EMPTY_RESPONSE_CAPABILITY_NAME);
}
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
DeprecationCacheResetAction.Request resetRequest = new DeprecationCacheResetAction.Request(); DeprecationCacheResetAction.Request resetRequest = new DeprecationCacheResetAction.Request();
return channel -> client.execute(DeprecationCacheResetAction.INSTANCE, resetRequest, new RestToXContentListener<>(channel)); return channel -> client.execute(
DeprecationCacheResetAction.INSTANCE,
resetRequest,
new EmptyResponseListener(channel).map(ignored -> ActionResponse.Empty.INSTANCE)
);
} }
} }

View File

@ -12,15 +12,19 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase; import org.elasticsearch.test.rest.RestActionTestCase;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.core.ml.action.CreateTrainedModelAssignmentAction; import org.elasticsearch.xpack.core.ml.action.CreateTrainedModelAssignmentAction;
import org.elasticsearch.xpack.core.ml.action.UpdateTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.action.UpdateTrainedModelDeploymentAction;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class RestUpdateTrainedModelDeploymentActionTests extends RestActionTestCase { public class RestUpdateTrainedModelDeploymentActionTests extends RestActionTestCase {
public void testNumberOfAllocationInParam() { public void testNumberOfAllocationInParam() {
@ -33,7 +37,7 @@ public class RestUpdateTrainedModelDeploymentActionTests extends RestActionTestC
assertEquals(request.getNumberOfAllocations().intValue(), 5); assertEquals(request.getNumberOfAllocations().intValue(), 5);
executeCalled.set(true); executeCalled.set(true);
return mock(CreateTrainedModelAssignmentAction.Response.class); return newMockResponse();
})); }));
var params = new HashMap<String, String>(); var params = new HashMap<String, String>();
params.put("number_of_allocations", "5"); params.put("number_of_allocations", "5");
@ -56,7 +60,7 @@ public class RestUpdateTrainedModelDeploymentActionTests extends RestActionTestC
assertEquals(request.getNumberOfAllocations().intValue(), 6); assertEquals(request.getNumberOfAllocations().intValue(), 6);
executeCalled.set(true); executeCalled.set(true);
return mock(CreateTrainedModelAssignmentAction.Response.class); return newMockResponse();
})); }));
final String content = """ final String content = """
@ -69,4 +73,16 @@ public class RestUpdateTrainedModelDeploymentActionTests extends RestActionTestC
dispatchRequest(inferenceRequest); dispatchRequest(inferenceRequest);
assertThat(executeCalled.get(), equalTo(true)); assertThat(executeCalled.get(), equalTo(true));
} }
private static CreateTrainedModelAssignmentAction.Response newMockResponse() {
final var response = mock(CreateTrainedModelAssignmentAction.Response.class);
try {
when(response.toXContent(any(), any())).thenAnswer(
invocation -> asInstanceOf(XContentBuilder.class, invocation.getArgument(0)).startObject().endObject()
);
} catch (IOException e) {
fail(e);
}
return response;
}
} }

View File

@ -7,6 +7,7 @@
package org.elasticsearch.xpack.security.rest.action.profile; package org.elasticsearch.xpack.security.rest.action.profile;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.ElasticsearchSecurityException;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -33,7 +34,6 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -54,7 +54,7 @@ public class RestSuggestProfilesActionTests extends RestActionTestCase {
verifyingClient.setExecuteLocallyVerifier(((actionType, actionRequest) -> { verifyingClient.setExecuteLocallyVerifier(((actionType, actionRequest) -> {
assertThat(actionRequest, instanceOf(SuggestProfilesRequest.class)); assertThat(actionRequest, instanceOf(SuggestProfilesRequest.class));
requestHolder.set((SuggestProfilesRequest) actionRequest); requestHolder.set((SuggestProfilesRequest) actionRequest);
return mock(SuggestProfilesResponse.class); return new SuggestProfilesResponse(new SuggestProfilesResponse.ProfileHit[0], 0, new TotalHits(0, TotalHits.Relation.EQUAL_TO));
})); }));
} }

View File

@ -2,9 +2,13 @@
"Test operator privileges will work in the mixed cluster": "Test operator privileges will work in the mixed cluster":
- requires: - requires:
test_runner_features: headers test_runner_features: [headers, capabilities]
cluster_features: ["gte_v7.11.0"] cluster_features: ["gte_v7.11.0"]
reason: "operator privileges are available since 7.11" reason: "operator privileges are available since 7.11"
capabilities:
- method: DELETE
path: /_cluster/voting_config_exclusions
capabilities: [ plain_text_empty_response ]
# The default user ("test_user") is an operator, so this works # The default user ("test_user") is an operator, so this works
- do: - do: