diff --git a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java index 86636c9cd5f85..4bdcbc8527bf0 100644 --- a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java +++ b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java @@ -53,6 +53,7 @@ import static org.elasticsearch.cluster.routing.UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING; import static org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -1033,13 +1034,13 @@ public void testIncludeTypeNameOnMultiTypesIndices() throws IOException { } else { // GET mappings Request getMappingsRequest = new Request("GET", index + "/_mappings"); - getMappingsRequest.addParameter("include_type_name", "false"); + getMappingsRequest.addParameter(INCLUDE_TYPE_NAME_PARAMETER, "false"); ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(getMappingsRequest)); assertThat(EntityUtils.toString(ex.getResponse().getEntity()), Matchers.containsString("has multiple mappings")); // PUT mappings Request putMappingsRequest = new Request("PUT", index + "/_mappings"); - putMappingsRequest.addParameter("include_type_name", "false"); + putMappingsRequest.addParameter(INCLUDE_TYPE_NAME_PARAMETER, "false"); String mapping = Strings.toString(JsonXContent.contentBuilder() .startObject() .startObject("properties") @@ -1079,7 +1080,7 @@ public void testIncludeTypeNameOnSingleTypeIndices() throws IOException { } else { // PUT mappings Request putMappingsRequest = new Request("PUT", index + "/_mappings"); - putMappingsRequest.addParameter("include_type_name", "false"); + putMappingsRequest.addParameter(INCLUDE_TYPE_NAME_PARAMETER, "false"); String mapping = Strings.toString(JsonXContent.contentBuilder() .startObject() .startObject("properties") @@ -1093,7 +1094,7 @@ public void testIncludeTypeNameOnSingleTypeIndices() throws IOException { // GET mappings Request getMappingsRequest = new Request("GET", index + "/_mappings"); - getMappingsRequest.addParameter("include_type_name", "false"); + getMappingsRequest.addParameter(INCLUDE_TYPE_NAME_PARAMETER, "false"); if (getOldClusterVersion().before(Version.V_6_0_0)) { ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(getMappingsRequest)); assertThat(EntityUtils.toString(ex.getResponse().getEntity()), Matchers.containsString("has multiple mappings")); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java index bb285e90a199f..61db16c5021cc 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.util.Map; +import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER; public class GetMappingsResponse extends ActionResponse implements ToXContentFragment { @@ -132,7 +133,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params, boolea for (final ObjectObjectCursor typeEntry : indexEntry.value) { if (typeEntry.key.equals("_default_") == false) { if (mappings != null) { - throw new IllegalArgumentException("Cannot use [include_type_name=false] on index [" + indexEntry.key + + throw new IllegalArgumentException("Cannot use ["+ + INCLUDE_TYPE_NAME_PARAMETER +"=false] on index [" + indexEntry.key + "] that has multiple mappings: " + indexEntry.value.keys()); } mappings = typeEntry.value; diff --git a/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java b/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java index 4a982ac80a741..f14882237b0fe 100644 --- a/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java +++ b/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java @@ -58,6 +58,13 @@ public abstract class BaseRestHandler extends AbstractComponent implements RestH private final LongAdder usageCount = new LongAdder(); + /** + * Parameter that controls whether certain REST apis should include type names in their requests or responses. + * Note: Support for this parameter will be removed after the transition period to typeless APIs. + */ + public static final String INCLUDE_TYPE_NAME_PARAMETER = "include_type_name"; + public static final boolean DEFAULT_INCLUDE_TYPE_NAME_POLICY = true; + protected BaseRestHandler(Settings settings) { // TODO drop settings from ctor } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java index d38992fae00c4..eb36b71ec2bd3 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexAction.java @@ -54,7 +54,7 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { - final boolean includeTypeName = request.paramAsBoolean("include_type_name", true); + final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index")); if (request.hasContent()) { Map sourceAsMap = XContentHelper.convertToMap(request.content(), false, request.getXContentType()).v2(); diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingAction.java index 534aa26d55d26..fc7d65eda1083 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingAction.java @@ -81,7 +81,7 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { - final boolean includeTypeName = request.paramAsBoolean("include_type_name", true); + final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final String[] types = request.paramAsStringArrayOrEmptyIfAll("type"); final GetMappingsRequest getMappingsRequest = new GetMappingsRequest(); diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java index b4de25ab06d59..6881642649f54 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java @@ -73,7 +73,7 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { - final boolean includeTypeName = request.paramAsBoolean("include_type_name", true); + final boolean includeTypeName = request.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); PutMappingRequest putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index"))); final String type = request.param("type"); if (type != null && includeTypeName == false) {