Skip to content

Compatible logic for include_type_param and RestCreateIndexAction #54197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
96ebb5c
init work. 221 vs 231 failing
pgomulka Mar 25, 2020
2e922bf
allow registering multiple rest actions under the same path
pgomulka Mar 27, 2020
e7eda97
revert out dirs
pgomulka Mar 27, 2020
5dac4b9
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka Mar 27, 2020
a213e6c
extend restcreateindexaction
pgomulka Mar 27, 2020
f1ad677
code style and discovery nodes in indexaction
pgomulka Mar 27, 2020
8ff3463
fix double registration
pgomulka Mar 30, 2020
1ee4487
additional testing and using version
pgomulka Apr 2, 2020
abe1d28
unused method
pgomulka Apr 2, 2020
d8fc2e5
method handlers - returning null when no handler under a method was r…
pgomulka Apr 2, 2020
ec8df43
remove unused constant
pgomulka Apr 3, 2020
8602f65
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka Apr 20, 2020
f3d25e0
fix javadoc
pgomulka Apr 20, 2020
7024913
compile
pgomulka Apr 20, 2020
f52dbc1
spotless
pgomulka Apr 20, 2020
0b07539
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka Apr 21, 2020
8135794
v7 name
pgomulka Apr 22, 2020
f819313
fix test
pgomulka Apr 22, 2020
6e28d24
javadoc
pgomulka Apr 22, 2020
a77716e
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka Apr 23, 2020
094a462
Apply suggestions from code review
pgomulka Apr 27, 2020
f704d0c
merge
pgomulka Apr 27, 2020
18651ae
compile fix
pgomulka Apr 27, 2020
0e1c917
assertion on version and test
pgomulka Apr 27, 2020
93bebbf
Update server/src/main/java/org/elasticsearch/rest/RestController.java
pgomulka Apr 28, 2020
281ec7f
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka Apr 28, 2020
4ee93ae
fix method not found test
pgomulka Apr 28, 2020
594b02f
Merge branch 'compat/create_index_include_type' of github.com:pgomulk…
pgomulka Apr 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.compat.version7.RestCreateIndexActionV7;
import org.elasticsearch.rest.compat.version7.RestGetActionV7;
import org.elasticsearch.rest.compat.version7.RestIndexActionV7;

Expand All @@ -54,7 +55,8 @@ public List<RestHandler> getRestHandlers(
new RestGetActionV7(),
new RestIndexActionV7.CompatibleRestIndexAction(),
new RestIndexActionV7.CompatibleCreateHandler(),
new RestIndexActionV7.CompatibleAutoIdHandler(nodesInCluster)
new RestIndexActionV7.CompatibleAutoIdHandler(nodesInCluster),
new RestCreateIndexActionV7()
);
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.rest.compat.version7;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.rest.action.admin.indices.RestCreateIndexAction;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.singletonMap;

public class RestCreateIndexActionV7 extends RestCreateIndexAction {

/**
* Parameter that controls whether certain REST apis should include type names in their requests.
*/
public static final String INCLUDE_TYPE_NAME_PARAMETER = "include_type_name";

@Override
public String getName() {
return "create_index_action_v7";
}

@Override
public Version compatibleWithVersion() {
return Version.V_7_0_0;
}

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
CreateIndexRequest createIndexRequest = prepareRequest(request);
return channel -> client.admin().indices().create(createIndexRequest, new RestToXContentListener<>(channel));
}

// default scope for testing
CreateIndexRequest prepareRequest(RestRequest request) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));

if (request.hasContent()) {
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false, request.getXContentType()).v2();

request.param(INCLUDE_TYPE_NAME_PARAMETER);// just consume, it is always replaced with _doc
sourceAsMap = prepareMappings(sourceAsMap, request);

createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE);
}

createIndexRequest.timeout(request.paramAsTime("timeout", createIndexRequest.timeout()));
createIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createIndexRequest.masterNodeTimeout()));
createIndexRequest.waitForActiveShards(ActiveShardCount.parseString(request.param("wait_for_active_shards")));
return createIndexRequest;
}

static Map<String, Object> prepareMappings(Map<String, Object> source, RestRequest request) {
final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, false);

@SuppressWarnings("unchecked")
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");

if (includeTypeName && mappings.size() == 1) {
Map<String, Object> newSource = new HashMap<>();

String typeName = mappings.keySet().iterator().next();
@SuppressWarnings("unchecked")
Map<String, Object> typedMappings = (Map<String, Object>) mappings.get(typeName);

// no matter what the type was, replace it with _doc, because the internal representation still uses single type `_doc`.
newSource.put("mappings", Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, typedMappings));
return newSource;
} else {
return prepareMappings(source);
}
}

static Map<String, Object> prepareMappings(Map<String, Object> source) {
if (source.containsKey("mappings") == false || (source.get("mappings") instanceof Map) == false) {
return source;
}

Map<String, Object> newSource = new HashMap<>(source);

@SuppressWarnings("unchecked")
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
if (MapperService.isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, mappings)) {
throw new IllegalArgumentException("The mapping definition cannot be nested under a type");
}

newSource.put("mappings", singletonMap(MapperService.SINGLE_MAPPING_NAME, mappings));
return newSource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public String getName() {

@Override
public List<Route> routes() {
assert Version.CURRENT.major == 8 : "REST API compatibility for version 7 is only supported on version 8";

return List.of(new Route(GET, "/{index}/{type}/{id}"), new Route(HEAD, "/{index}/{type}/{id}"));
}

Expand All @@ -58,7 +56,7 @@ public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient
}

@Override
public boolean compatibilityRequired() {
return true;
public Version compatibleWithVersion() {
return Version.V_7_0_0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import java.util.List;
import java.util.function.Supplier;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;

Expand All @@ -55,8 +53,6 @@ public String getName() {

@Override
public List<Route> routes() {
assert Version.CURRENT.major == 8 : "REST API compatilbity for version 7 is only supported on version 8";

return List.of(new Route(POST, "/{index}/{type}/{id}"), new Route(PUT, "/{index}/{type}/{id}"));
}

Expand All @@ -68,8 +64,8 @@ public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient
}

@Override
public boolean compatibilityRequired() {
return true;
public Version compatibleWithVersion() {
return Version.V_7_0_0;
}
}

Expand All @@ -82,9 +78,7 @@ public String getName() {

@Override
public List<Route> routes() {
return unmodifiableList(
asList(new Route(POST, "/{index}/{type}/{id}/_create"), new Route(PUT, "/{index}/{type}/{id}/_create"))
);
return List.of(new Route(POST, "/{index}/{type}/{id}/_create"), new Route(PUT, "/{index}/{type}/{id}/_create"));
}

@Override
Expand All @@ -95,8 +89,8 @@ public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient
}

@Override
public boolean compatibilityRequired() {
return true;
public Version compatibleWithVersion() {
return Version.V_7_0_0;
}
}

Expand Down Expand Up @@ -124,8 +118,8 @@ public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient
}

@Override
public boolean compatibilityRequired() {
return true;
public Version compatibleWithVersion() {
return Version.V_7_0_0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.rest.compat.version7;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
import org.junit.Before;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;

public class RestCreateIndexActionV7Tests extends RestActionTestCase {

String mimeType = "application/vnd.elasticsearch+json;compatible-with=7";
List<String> contentTypeHeader = Collections.singletonList(mimeType);

RestCreateIndexActionV7 restHandler = new RestCreateIndexActionV7();

@Before
public void setUpAction() {
controller().registerHandler(restHandler);
}

public void testTypeInMapping() throws IOException {
String content = "{\n"
+ " \"mappings\": {\n"
+ " \"some_type\": {\n"
+ " \"properties\": {\n"
+ " \"field1\": {\n"
+ " \"type\": \"text\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}";

Map<String, String> params = new HashMap<>();
params.put(RestCreateIndexActionV7.INCLUDE_TYPE_NAME_PARAMETER, "true");
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withPath("/some_index")
.withParams(params)
.withContent(new BytesArray(content), null)
.build();

CreateIndexRequest createIndexRequest = restHandler.prepareRequest(request);
// some_type is replaced with _doc
assertThat(createIndexRequest.mappings(), equalTo("{\"_doc\":{\"properties\":{\"field1\":{\"type\":\"text\"}}}}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.Map;

public class RestGetActionV7Tests extends RestActionTestCase {
final String mimeType = randomFrom("application/vnd.elasticsearch+json;compatible-with=7");
final String mimeType = "application/vnd.elasticsearch+json;compatible-with=7";
final List<String> contentTypeHeader = Collections.singletonList(mimeType);

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public class RestIndexActionV7Tests extends RestActionTestCase {

final String mimeType = randomFrom("application/vnd.elasticsearch+json;compatible-with=7");
final String mimeType = "application/vnd.elasticsearch+json;compatible-with=7";
final List<String> contentTypeHeader = Collections.singletonList(mimeType);

private final AtomicReference<ClusterState> clusterStateSupplier = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.Version;
import org.elasticsearch.rest.CompatibleConstants;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
Expand Down Expand Up @@ -82,7 +83,7 @@ private static Consumer<? super ExecutableSection> updateDoSection() {
doSection.setIgnoreWarnings(true);

String compatibleHeader = createCompatibleHeader();
// for cat apis accept headers would break tests which expect txt response
// TODO for cat apis accept headers would break tests which expect txt response
if (doSection.getApiCallSection().getApi().startsWith("cat") == false) {
doSection.getApiCallSection()
.addHeaders(
Expand All @@ -99,7 +100,7 @@ private static Consumer<? super ExecutableSection> updateDoSection() {
}

private static String createCompatibleHeader() {
return "application/vnd.elasticsearch+json;compatible-with=" + CompatibleConstants.COMPATIBLE_VERSION;
return "application/vnd.elasticsearch+json;compatible-with=" + Version.minimumRestCompatibilityVersion().major;
}

private static Map<ClientYamlTestCandidate, ClientYamlTestCandidate> getLocalCompatibilityTests() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public XContentBuilder newBuilder(@Nullable XContentType requestContentType, @Nu
}

builder.humanReadable(human);
String compatibleVersion = request.param(CompatibleConstants.COMPATIBLE_PARAMS_KEY);
builder.setCompatibleMajorVersion(compatibleVersion == null ? -1 : Byte.parseByte(compatibleVersion));

builder.setCompatibleMajorVersion(request.getCompatibleApiVersion().major);
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@

package org.elasticsearch.rest;

import org.elasticsearch.Version;

public class CompatibleConstants {

/**
* TODO revisit when https://github.com/elastic/elasticsearch/issues/52370 is resolved
*/
public static final String COMPATIBLE_ACCEPT_HEADER = "Accept";
public static final String COMPATIBLE_CONTENT_TYPE_HEADER = "Content-Type";
public static final String COMPATIBLE_PARAMS_KEY = "Compatible-With";
public static final String COMPATIBLE_VERSION = String.valueOf(Version.minimumRestCompatibilityVersion().major);

}
Loading