-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
pgomulka
merged 28 commits into
elastic:compat_rest_api
from
pgomulka:compat/create_index_include_type
Apr 28, 2020
Merged
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 2e922bf
allow registering multiple rest actions under the same path
pgomulka e7eda97
revert out dirs
pgomulka 5dac4b9
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka a213e6c
extend restcreateindexaction
pgomulka f1ad677
code style and discovery nodes in indexaction
pgomulka 8ff3463
fix double registration
pgomulka 1ee4487
additional testing and using version
pgomulka abe1d28
unused method
pgomulka d8fc2e5
method handlers - returning null when no handler under a method was r…
pgomulka ec8df43
remove unused constant
pgomulka 8602f65
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka f3d25e0
fix javadoc
pgomulka 7024913
compile
pgomulka f52dbc1
spotless
pgomulka 0b07539
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka 8135794
v7 name
pgomulka f819313
fix test
pgomulka 6e28d24
javadoc
pgomulka a77716e
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka 094a462
Apply suggestions from code review
pgomulka f704d0c
merge
pgomulka 18651ae
compile fix
pgomulka 0e1c917
assertion on version and test
pgomulka 93bebbf
Update server/src/main/java/org/elasticsearch/rest/RestController.java
pgomulka 281ec7f
Merge branch 'compat_rest_api' into compat/create_index_include_type
pgomulka 4ee93ae
fix method not found test
pgomulka 594b02f
Merge branch 'compat/create_index_include_type' of github.com:pgomulk…
pgomulka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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)); | ||
jakelandis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.