From b01ebbbc6e632de70089277dbf19c62850a9637b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 15 Jan 2019 14:14:23 +0100 Subject: [PATCH 1/4] Allow indices.get_mapping response parsing without types This change adds deprecation warning to the indices.get_mapping API in case the "inlcude_type_name" parameter is set to "true" and changes the parsing code in GetMappingsResponse to parse the type-less response instead of the one containing types. As a consequence the HLRC client doesn't need to force "include_type_name=true" any more and the GetMappingsResponseTests can be adapted to the new format as well. Also removing some "include_type_name" parameters in yaml test and docs where not necessary. --- .../client/IndicesRequestConverters.java | 1 - .../elasticsearch/client/IndicesClientIT.java | 4 +- .../client/IndicesRequestConvertersTests.java | 1 - .../IndicesClientDocumentationIT.java | 2 - .../high-level/indices/get_mappings.asciidoc | 1 - .../api/indices.get_mapping.json | 2 +- .../test/indices.get_mapping/10_basic.yml | 2 - .../11_basic_with_types.yml | 13 --- .../61_empty_with_types.yml | 20 ----- .../mapping/get/GetMappingsResponse.java | 20 ++--- .../admin/indices/RestGetMappingAction.java | 7 ++ .../mapping/get/GetMappingsResponseTests.java | 81 +++++++++++++++---- .../indices/RestGetMappingActionTests.java | 16 ++++ 13 files changed, 97 insertions(+), 73 deletions(-) delete mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index 79b0646770845..f3ce8d2a935ce 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -150,7 +150,6 @@ static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOExcep parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout()); parameters.withIndicesOptions(getMappingsRequest.indicesOptions()); parameters.withLocal(getMappingsRequest.local()); - parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true"); return request; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index f3a2fd2baaa3a..0f29950355472 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -443,9 +443,7 @@ public void testGetMapping() throws IOException { Map getIndexResponse = getAsMap(indexName); assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse)); - GetMappingsRequest request = new GetMappingsRequest() - .indices(indexName) - .types("_doc"); + GetMappingsRequest request = new GetMappingsRequest().indices(indexName); GetMappingsResponse getMappingsResponse = execute(request, highLevelClient().indices()::getMapping, highLevelClient().indices()::getMappingAsync); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index 663c40b17a8b2..308c576edafe6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -217,7 +217,6 @@ public void testGetMapping() throws IOException { getMappingRequest::indicesOptions, expectedParams); RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams); RequestConvertersTests.setRandomLocal(getMappingRequest, expectedParams); - expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "true"); Request request = IndicesRequestConverters.getMappings(getMappingRequest); StringJoiner endpoint = new StringJoiner("/", "/", ""); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 8f9d8a069fd48..3d1b77562215b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -609,7 +609,6 @@ public void testGetMapping() throws IOException { // tag::get-mappings-request GetMappingsRequest request = new GetMappingsRequest(); // <1> request.indices("twitter"); // <2> - request.types("_doc"); // <3> // end::get-mappings-request // tag::get-mappings-request-masterTimeout @@ -665,7 +664,6 @@ public void testGetMappingAsync() throws Exception { { GetMappingsRequest request = new GetMappingsRequest(); request.indices("twitter"); - request.types("_doc"); // tag::get-mappings-execute-listener ActionListener listener = diff --git a/docs/java-rest/high-level/indices/get_mappings.asciidoc b/docs/java-rest/high-level/indices/get_mappings.asciidoc index c8616cdab9271..a42a8ac77b338 100644 --- a/docs/java-rest/high-level/indices/get_mappings.asciidoc +++ b/docs/java-rest/high-level/indices/get_mappings.asciidoc @@ -18,7 +18,6 @@ include-tagged::{doc-tests-file}[{api}-request] -------------------------------------------------- <1> An empty request that will return all indices and types <2> Setting the indices to fetch mapping for -<3> The types to be returned ==== Optional arguments The following arguments can also optionally be provided: diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/indices.get_mapping.json b/rest-api-spec/src/main/resources/rest-api-spec/api/indices.get_mapping.json index ccec2ddffdd0c..d9016ec402498 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/api/indices.get_mapping.json +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/indices.get_mapping.json @@ -18,7 +18,7 @@ "params": { "include_type_name": { "type" : "boolean", - "description" : "Whether to add the type name to the response" + "description" : "Whether to add the type name to the response (default: false)" }, "ignore_unavailable": { "type" : "boolean", diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/10_basic.yml index d9ea7d325e3a8..76519cc4c4c01 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/10_basic.yml @@ -5,13 +5,11 @@ setup: reason: include_type_name defaults to true before 7.0 - do: indices.create: - include_type_name: false index: test_1 body: mappings: {} - do: indices.create: - include_type_name: false index: test_2 body: mappings: {} diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml index 1c26a2a3d3d13..6aa7b23e96c6f 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml @@ -13,20 +13,7 @@ setup: mappings: doc: {} --- -"Get /{index}/_mapping with empty mappings": - - do: - indices.create: - index: t - - - do: - indices.get_mapping: - include_type_name: true - index: t - - - match: { t.mappings: {}} - ---- "Get /_mapping": - do: diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml deleted file mode 100644 index 6da7f4a2c6946..0000000000000 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml +++ /dev/null @@ -1,20 +0,0 @@ ---- -setup: - - - do: - indices.create: - index: test_1 - - - do: - indices.create: - index: test_2 - ---- -"Check empty mapping when getting all mappings via /_mapping": - - - do: - indices.get_mapping: - include_type_name: true - - - match: { test_1.mappings: {}} - - match: { test_2.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 4c037bd1d6df9..50b7a36426802 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 @@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.rest.BaseRestHandler; import java.io.IOException; @@ -101,22 +102,17 @@ public static GetMappingsResponse fromXContent(XContentParser parser) throws IOE for (Map.Entry entry : parts.entrySet()) { final String indexName = entry.getKey(); assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass(); - @SuppressWarnings("unchecked") - final Map mapping = (Map) ((Map) entry.getValue()).get(MAPPINGS.getPreferredName()); - ImmutableOpenMap.Builder typeBuilder = new ImmutableOpenMap.Builder<>(); - for (Map.Entry typeEntry : mapping.entrySet()) { - final String typeName = typeEntry.getKey(); - assert typeEntry.getValue() instanceof Map : "expected a map as inner type mapping, but got: " + - typeEntry.getValue().getClass(); - @SuppressWarnings("unchecked") - final Map fieldMappings = (Map) typeEntry.getValue(); - MappingMetaData mmd = new MappingMetaData(typeName, fieldMappings); - typeBuilder.put(typeName, mmd); + @SuppressWarnings("unchecked") + final Map fieldMappings = (Map) ((Map) entry.getValue()) + .get(MAPPINGS.getPreferredName()); + if (fieldMappings.isEmpty() == false) { + assert fieldMappings instanceof Map : "expected a map as inner type mapping, but got: " + fieldMappings.getClass(); + MappingMetaData mmd = new MappingMetaData(MapperService.SINGLE_MAPPING_NAME, fieldMappings); + typeBuilder.put(MapperService.SINGLE_MAPPING_NAME, mmd); } builder.put(indexName, typeBuilder.build()); } - return new GetMappingsResponse(builder.build()); } 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 da7f2af501db2..6301581bd032a 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 @@ -20,6 +20,7 @@ package org.elasticsearch.rest.action.admin.indices; import com.carrotsearch.hppc.cursors.ObjectCursor; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; @@ -59,6 +60,8 @@ public class RestGetMappingAction extends BaseRestHandler { private static final Logger logger = LogManager.getLogger(RestGetMappingAction.class); private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger); + static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using `include_type_name` in get mapping requests is deprecated. " + + "The parameter will be removed in the next major version."; public RestGetMappingAction(final Settings settings, final RestController controller) { super(settings); @@ -90,6 +93,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC throw new IllegalArgumentException("Types cannot be provided in get mapping requests, unless" + " include_type_name is set to true."); } + // TODO q: do we want to deprecate any use of the parameter or just "true" + if (includeTypeName == true) { + deprecationLogger.deprecatedAndMaybeLog("get_mapping_with_types", TYPES_DEPRECATION_MESSAGE); + } final GetMappingsRequest getMappingsRequest = new GetMappingsRequest(); getMappingsRequest.indices(indices).types(types); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java index 633d74acde174..a93633e64d16e 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java @@ -24,8 +24,11 @@ import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContent.Params; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.test.AbstractStreamableXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; @@ -38,7 +41,7 @@ import java.util.Map; import java.util.Objects; -import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER; +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; public class GetMappingsResponseTests extends AbstractStreamableXContentTestCase { @@ -86,12 +89,6 @@ protected GetMappingsResponse mutateInstance(GetMappingsResponse instance) throw return mutate(instance); } - public static ImmutableOpenMap createMappingsForIndex() { - // rarely have no types - int typeCount = rarely() ? 0 : scaledRandomIntBetween(1, 3); - return createMappingsForIndex(typeCount, true); - } - public static ImmutableOpenMap createMappingsForIndex(int typeCount, boolean randomTypeName) { List typeMappings = new ArrayList<>(typeCount); @@ -122,22 +119,18 @@ public static ImmutableOpenMap createMappingsForIndex(i @Override protected GetMappingsResponse createTestInstance() { + return createTestInstance(true); + } + + private GetMappingsResponse createTestInstance(boolean randomTypeNames) { ImmutableOpenMap.Builder> indexBuilder = ImmutableOpenMap.builder(); - indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex()); + int typeCount = rarely() ? 0 : 1; + indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex(typeCount, randomTypeNames)); GetMappingsResponse resp = new GetMappingsResponse(indexBuilder.build()); logger.debug("--> created: {}", resp); return resp; } - /** - * For now, we only unit test the legacy typed responses. This will soon no longer be the - * case, as we introduce support for typeless xContent parsing in {@link GetMappingsResponse}. - */ - @Override - protected ToXContent.Params getToXContentParams() { - return new ToXContent.MapParams(Collections.singletonMap(INCLUDE_TYPE_NAME_PARAMETER, "true")); - } - // Not meant to be exhaustive private static Map randomFieldMapping() { Map mappings = new HashMap<>(); @@ -170,4 +163,58 @@ private static Map randomFieldMapping() { } return mappings; } + + @Override + protected GetMappingsResponse createXContextTestInstance(XContentType xContentType) { + // don't use random type names for XContent roundtrip tests because we cannot parse them back anymore + return createTestInstance(false); + } + + /** + * check that the "old" legacy response format with types works as expected + * @throws IOException + */ + public void testToXContentWithTypes() throws IOException { + Params params = new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "true")); + xContentTester(this::createParser, t -> createTestInstance(), params, this::fromXContentLegacy) + .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + .supportsUnknownFields(supportsUnknownFields()) + .shuffleFieldsExceptions(getShuffleFieldsExceptions()) + .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) + .assertEqualsConsumer(this::assertEqualInstances) + .assertToXContentEquivalence(true) + .test(); + } + + /** + * including the pre-7.0 parsing code here to test that older HLRC clients using this can parse the responses that are + * returned when "include_type_name=true" + */ + private GetMappingsResponse fromXContentLegacy(XContentParser parser) throws IOException { + if (parser.currentToken() == null) { + parser.nextToken(); + } + assert parser.currentToken() == XContentParser.Token.START_OBJECT; + Map parts = parser.map(); + + ImmutableOpenMap.Builder> builder = new ImmutableOpenMap.Builder<>(); + for (Map.Entry entry : parts.entrySet()) { + final String indexName = entry.getKey(); + assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass(); + final Map mapping = (Map) ((Map) entry.getValue()).get("mappings"); + + ImmutableOpenMap.Builder typeBuilder = new ImmutableOpenMap.Builder<>(); + for (Map.Entry typeEntry : mapping.entrySet()) { + final String typeName = typeEntry.getKey(); + assert typeEntry.getValue() instanceof Map : "expected a map as inner type mapping, but got: " + + typeEntry.getValue().getClass(); + final Map fieldMappings = (Map) typeEntry.getValue(); + MappingMetaData mmd = new MappingMetaData(typeName, fieldMappings); + typeBuilder.put(typeName, mmd); + } + builder.put(indexName, typeBuilder.build()); + } + + return new GetMappingsResponse(builder.build()); + } } diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java index 8eea9dc34c2fb..36bb337811455 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java @@ -69,4 +69,20 @@ public void testTypeInPath() { assertEquals(1, channel.errors().get()); assertEquals(RestStatus.BAD_REQUEST, channel.capturedResponse().status()); } + + public void testTypeUrlParamerterDeprecation() throws Exception { + Map params = new HashMap<>(); + params.put(INCLUDE_TYPE_NAME_PARAMETER, "true"); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) + .withMethod(RestRequest.Method.GET) + .withParams(params) + .withPath("some_index/some_type/_mapping/some_field") + .build(); + + RestGetMappingAction handler = new RestGetMappingAction(Settings.EMPTY, mock(RestController.class)); + handler.prepareRequest(request, mock(NodeClient.class)); + + assertWarnings(RestGetMappingAction.TYPES_DEPRECATION_MESSAGE); + } + } From d1271961dc87054c3ad3b95443537c09e5da040e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 16 Jan 2019 11:20:48 +0100 Subject: [PATCH 2/4] iter --- .../11_basic_with_types.yml | 13 ++++++++++++ .../test/indices.get_mapping/60_empty.yml | 3 --- .../admin/indices/RestGetMappingAction.java | 3 +-- .../mapping/get/GetMappingsResponseTests.java | 5 ++--- .../indices/RestGetMappingActionTests.java | 20 ++++++++++++++----- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml index 6aa7b23e96c6f..1c26a2a3d3d13 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/11_basic_with_types.yml @@ -13,7 +13,20 @@ setup: mappings: doc: {} --- +"Get /{index}/_mapping with empty mappings": + - do: + indices.create: + index: t + + - do: + indices.get_mapping: + include_type_name: true + index: t + + - match: { t.mappings: {}} + +--- "Get /_mapping": - do: diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/60_empty.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/60_empty.yml index e2a502f30a84d..b5069295a1fa6 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/60_empty.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/60_empty.yml @@ -1,8 +1,5 @@ --- setup: - - skip: - version: " - 6.99.99" - reason: include_type_name defaults to true before 7.0 - do: indices.create: index: test_1 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 6301581bd032a..c3d5c4ed50e6b 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 @@ -93,8 +93,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC throw new IllegalArgumentException("Types cannot be provided in get mapping requests, unless" + " include_type_name is set to true."); } - // TODO q: do we want to deprecate any use of the parameter or just "true" - if (includeTypeName == true) { + if (request.hasParam(INCLUDE_TYPE_NAME_PARAMETER)) { deprecationLogger.deprecatedAndMaybeLog("get_mapping_with_types", TYPES_DEPRECATION_MESSAGE); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java index a93633e64d16e..7d1a19c65ed52 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java @@ -172,11 +172,10 @@ protected GetMappingsResponse createXContextTestInstance(XContentType xContentTy /** * check that the "old" legacy response format with types works as expected - * @throws IOException */ public void testToXContentWithTypes() throws IOException { Params params = new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "true")); - xContentTester(this::createParser, t -> createTestInstance(), params, this::fromXContentLegacy) + xContentTester(this::createParser, t -> createTestInstance(), params, this::fromXContentWithTypes) .numberOfTestRuns(NUMBER_OF_TEST_RUNS) .supportsUnknownFields(supportsUnknownFields()) .shuffleFieldsExceptions(getShuffleFieldsExceptions()) @@ -190,7 +189,7 @@ public void testToXContentWithTypes() throws IOException { * including the pre-7.0 parsing code here to test that older HLRC clients using this can parse the responses that are * returned when "include_type_name=true" */ - private GetMappingsResponse fromXContentLegacy(XContentParser parser) throws IOException { + private GetMappingsResponse fromXContentWithTypes(XContentParser parser) throws IOException { if (parser.currentToken() == null) { parser.nextToken(); } diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java index 36bb337811455..7ce32e371de6e 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingActionTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.test.rest.FakeRestChannel; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; import java.util.HashMap; import java.util.Map; @@ -37,6 +38,11 @@ public class RestGetMappingActionTests extends RestActionTestCase { + @Before + public void setUpAction() { + new RestGetMappingAction(Settings.EMPTY, controller()); + } + public void testTypeExistsDeprecation() throws Exception { Map params = new HashMap<>(); params.put("type", "_doc"); @@ -70,17 +76,21 @@ public void testTypeInPath() { assertEquals(RestStatus.BAD_REQUEST, channel.capturedResponse().status()); } - public void testTypeUrlParamerterDeprecation() throws Exception { + /** + * Setting "include_type_name" to true or false should cause a deprecation warning starting in 7.0 + */ + public void testTypeUrlParameterDeprecation() throws Exception { Map params = new HashMap<>(); - params.put(INCLUDE_TYPE_NAME_PARAMETER, "true"); + params.put(INCLUDE_TYPE_NAME_PARAMETER, Boolean.toString(randomBoolean())); RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) .withMethod(RestRequest.Method.GET) .withParams(params) - .withPath("some_index/some_type/_mapping/some_field") + .withPath("/some_index/_mappings") .build(); - RestGetMappingAction handler = new RestGetMappingAction(Settings.EMPTY, mock(RestController.class)); - handler.prepareRequest(request, mock(NodeClient.class)); + FakeRestChannel channel = new FakeRestChannel(request, false, 1); + ThreadContext threadContext = new ThreadContext(Settings.EMPTY); + controller().dispatchRequest(request, channel, threadContext); assertWarnings(RestGetMappingAction.TYPES_DEPRECATION_MESSAGE); } From a21ef8da9ea1058a40cc7464a3a39e46a7adbd83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 16 Jan 2019 23:59:41 +0100 Subject: [PATCH 3/4] Remove backticks --- .../rest/action/admin/indices/RestGetMappingAction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c3d5c4ed50e6b..8826932e252ba 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 @@ -60,7 +60,7 @@ public class RestGetMappingAction extends BaseRestHandler { private static final Logger logger = LogManager.getLogger(RestGetMappingAction.class); private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger); - static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using `include_type_name` in get mapping requests is deprecated. " + static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using include_type_name in get mapping requests is deprecated. " + "The parameter will be removed in the next major version."; public RestGetMappingAction(final Settings settings, final RestController controller) { From cd718cd025e0d0f54c613116d19d0b9309a9da5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 17 Jan 2019 14:23:20 +0100 Subject: [PATCH 4/4] Revert removing 61_empty_with_types.yml --- .../61_empty_with_types.yml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml new file mode 100644 index 0000000000000..6da7f4a2c6946 --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/61_empty_with_types.yml @@ -0,0 +1,20 @@ +--- +setup: + + - do: + indices.create: + index: test_1 + + - do: + indices.create: + index: test_2 + +--- +"Check empty mapping when getting all mappings via /_mapping": + + - do: + indices.get_mapping: + include_type_name: true + + - match: { test_1.mappings: {}} + - match: { test_2.mappings: {}}