-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compatible logic for include_type_param and RestCreateIndexAction (#5…
…4197) Refactoring of the compatible infrastructure to allow registering multiple RestAction under the same path. It only extends the current mechanism which allowed registering RestAction under the same path but with different method. Now it also uses a version together with a method to find a matching RestAction This PR also provides a V7 compatible RestCreateIndexAction that needs include_Type_param and a different logic for parsing mapping from its body. fixed get/index tests CompatRestIT. test {yaml=get/21_stored_fields_with_types/Stored fields} CompatRestIT. test {yaml=get/71_source_filtering_with_types/Source filtering} CompatRestIT. test {yaml=index/70_mix_typeless_typeful/Index call that introduces new field mappings} CompatRestIT. test {yaml=index/70_mix_typeless_typeful/Index with typeless API on an index that has types} however the last one from get is still failing CompatRestIT. test {yaml=get/100_mix_typeless_typeful/GET with typeless API on an index that has relates #54160
- Loading branch information
Showing
20 changed files
with
318 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...ibility/src/main/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ty/src/test/java/org/elasticsearch/rest/compat/version7/RestCreateIndexActionV7Tests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}}}}")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.