Skip to content

Commit 74e3110

Browse files
authored
[Rest Api Compatibility] Typed endpoints for get_source api (#73957)
retrofits typed get_source api removed in #46931 and #46587 relates #51816
1 parent 13e884b commit 74e3110

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

rest-api-spec/build.gradle

-9
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,6 @@ tasks.named("yamlRestCompatTest").configure {
7777
'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion and specifying both node_ids and node_names',
7878
'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion without specifying nodes',
7979
'field_caps/30_filter/Field caps with index filter',
80-
'get_source/11_basic_with_types/Basic with types',
81-
'get_source/16_default_values_with_types/Default values',
82-
'get_source/41_routing_with_types/Routing',
83-
'get_source/61_realtime_refresh_with_types/Realtime',
84-
'get_source/71_source_filtering_with_types/Source filtering',
85-
'get_source/81_missing_with_types/Missing document with catch',
86-
'get_source/81_missing_with_types/Missing document with ignore',
87-
'get_source/86_source_missing_with_types/Missing document source with catch',
88-
'get_source/86_source_missing_with_types/Missing document source with ignore',
8980
'indices.create/10_basic/Create index without soft deletes',
9081
'indices.flush/10_basic/Index synced flush rest test',
9182
'indices.forcemerge/10_basic/Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set',

server/src/main/java/org/elasticsearch/rest/action/document/RestGetSourceAction.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import org.elasticsearch.action.get.GetResponse;
1515
import org.elasticsearch.client.node.NodeClient;
1616
import org.elasticsearch.common.bytes.BytesReference;
17+
import org.elasticsearch.common.logging.DeprecationLogger;
1718
import org.elasticsearch.common.xcontent.XContentBuilder;
1819
import org.elasticsearch.common.xcontent.XContentHelper;
20+
import org.elasticsearch.core.RestApiVersion;
1921
import org.elasticsearch.rest.BaseRestHandler;
2022
import org.elasticsearch.rest.BytesRestResponse;
2123
import org.elasticsearch.rest.RestChannel;
@@ -36,12 +38,21 @@
3638
* The REST handler for get source and head source APIs.
3739
*/
3840
public class RestGetSourceAction extends BaseRestHandler {
41+
private final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestGetSourceAction.class);
42+
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in get_source and exist_source "
43+
+ "requests is deprecated.";
3944

4045
@Override
4146
public List<Route> routes() {
4247
return List.of(
4348
new Route(GET, "/{index}/_source/{id}"),
44-
new Route(HEAD, "/{index}/_source/{id}"));
49+
new Route(HEAD, "/{index}/_source/{id}"),
50+
Route.builder(GET, "/{index}/{type}/{id}/_source")
51+
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
52+
.build(),
53+
Route.builder(HEAD, "/{index}/{type}/{id}/_source")
54+
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
55+
.build());
4556
}
4657

4758
@Override
@@ -51,6 +62,11 @@ public String getName() {
5162

5263
@Override
5364
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
65+
if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
66+
request.param("type"); // consume and ignore the type
67+
deprecationLogger.compatibleApiWarning("get_source_with_types", TYPES_DEPRECATION_MESSAGE);
68+
}
69+
5470
final GetRequest getRequest = new GetRequest(request.param("index"), request.param("id"));
5571
getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
5672
getRequest.routing(request.param("routing"));

server/src/test/java/org/elasticsearch/rest/action/document/RestGetSourceActionTests.java

+68
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
package org.elasticsearch.rest.action.document;
1010

1111
import org.elasticsearch.ResourceNotFoundException;
12+
import org.elasticsearch.action.get.GetRequest;
1213
import org.elasticsearch.action.get.GetResponse;
1314
import org.elasticsearch.common.bytes.BytesArray;
1415
import org.elasticsearch.common.bytes.BytesReference;
16+
import org.elasticsearch.core.RestApiVersion;
1517
import org.elasticsearch.index.get.GetResult;
1618
import org.elasticsearch.rest.RestRequest;
1719
import org.elasticsearch.rest.RestResponse;
@@ -21,21 +23,33 @@
2123
import org.elasticsearch.test.rest.RestActionTestCase;
2224
import org.junit.AfterClass;
2325
import org.junit.Before;
26+
import org.mockito.Mockito;
27+
28+
import java.util.Collections;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
2432

2533
import static java.util.Collections.emptyMap;
2634
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
2735
import static org.elasticsearch.rest.RestStatus.OK;
2836
import static org.hamcrest.Matchers.equalTo;
37+
import static org.hamcrest.Matchers.instanceOf;
2938

3039
public class RestGetSourceActionTests extends RestActionTestCase {
3140

3241
private static RestRequest request = new FakeRestRequest();
3342
private static FakeRestChannel channel = new FakeRestChannel(request, true, 0);
3443
private static RestGetSourceResponseListener listener = new RestGetSourceResponseListener(channel, request);
44+
private final List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));
3545

3646
@Before
3747
public void setUpAction() {
3848
controller().registerHandler(new RestGetSourceAction());
49+
verifyingClient.setExecuteVerifier((actionType, request) -> {
50+
assertThat(request, instanceOf(GetRequest.class));
51+
return Mockito.mock(GetResponse.class);
52+
});
3953
}
4054

4155
@AfterClass
@@ -44,6 +58,7 @@ public static void cleanupReferences() {
4458
channel = null;
4559
listener = null;
4660
}
61+
4762
public void testRestGetSourceAction() throws Exception {
4863
final BytesReference source = new BytesArray("{\"foo\": \"bar\"}");
4964
final GetResponse response =
@@ -73,4 +88,57 @@ public void testRestGetSourceActionWithMissingDocumentSource() {
7388

7489
assertThat(exception.getMessage(), equalTo("Source not found [index1]/[1]"));
7590
}
91+
92+
/**
93+
* test deprecation is logged if type is used in path
94+
*/
95+
public void testTypeInGetPath() {
96+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
97+
.withHeaders(Map.of("Accept", compatibleMediaType))
98+
.withMethod(RestRequest.Method.HEAD)
99+
.withPath("/some_index/some_type/id/_source")
100+
.build();
101+
dispatchRequest(request);
102+
assertWarnings(RestGetSourceAction.TYPES_DEPRECATION_MESSAGE);
103+
}
104+
105+
public void testTypeInHeadPath() {
106+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
107+
.withHeaders(Map.of("Accept", compatibleMediaType))
108+
.withMethod(RestRequest.Method.GET)
109+
.withPath("/some_index/some_type/id/_source")
110+
.build();
111+
dispatchRequest(request);
112+
assertWarnings(RestGetSourceAction.TYPES_DEPRECATION_MESSAGE);
113+
}
114+
115+
/**
116+
* test deprecation is logged if type is used as parameter
117+
*/
118+
public void testTypeParameterAndGet() {
119+
Map<String, String> params = new HashMap<>();
120+
params.put("type", "some_type");
121+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
122+
.withHeaders(Map.of("Accept", compatibleMediaType))
123+
.withMethod(RestRequest.Method.GET)
124+
.withPath("/some_index/_source/id")
125+
.withParams(params)
126+
.build();
127+
dispatchRequest(request);
128+
assertWarnings(RestGetSourceAction.TYPES_DEPRECATION_MESSAGE);
129+
}
130+
131+
public void testTypeParameterAndHead() {
132+
Map<String, String> params = new HashMap<>();
133+
params.put("type", "some_type");
134+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
135+
.withHeaders(Map.of("Accept", compatibleMediaType))
136+
.withMethod(RestRequest.Method.HEAD)
137+
.withPath("/some_index/_source/id")
138+
.withParams(params)
139+
.build();
140+
dispatchRequest(request);
141+
assertWarnings(RestGetSourceAction.TYPES_DEPRECATION_MESSAGE);
142+
}
143+
76144
}

0 commit comments

Comments
 (0)