From f0aae96f1111da3455ba99e5b00d23677205ddf9 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 21 Aug 2015 09:09:43 -0400 Subject: [PATCH 1/2] Utility method for getting the first version prior to given version --- .../test/java/org/elasticsearch/test/VersionUtils.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/test/VersionUtils.java b/core/src/test/java/org/elasticsearch/test/VersionUtils.java index 316a3926d5d00..ebdad0071df32 100644 --- a/core/src/test/java/org/elasticsearch/test/VersionUtils.java +++ b/core/src/test/java/org/elasticsearch/test/VersionUtils.java @@ -64,10 +64,16 @@ public class VersionUtils { public static List allVersions() { return Collections.unmodifiableList(SORTED_VERSIONS); } - + + public static Version getPreviousVersion(Version version) { + int index = SORTED_VERSIONS.indexOf(version); + assert index > 0; + return SORTED_VERSIONS.get(index - 1); + } + /** Returns the {@link Version} before the {@link Version#CURRENT} */ public static Version getPreviousVersion() { - Version version = SORTED_VERSIONS.get(SORTED_VERSIONS.size() - 2); + Version version = getPreviousVersion(Version.CURRENT); assert version.before(Version.CURRENT); return version; } From 1579e491d8f1d77d97b3df6af3b4328364353448 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 21 Aug 2015 07:57:27 -0400 Subject: [PATCH 2/2] Limit type name length This commit will limit type name length to be at most 255 characters. Closes #13021 --- .../index/mapper/MapperService.java | 3 ++ .../index/mapper/MapperServiceTest.java | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 8386932465f7e..db2d0b20390bd 100755 --- a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -261,6 +261,9 @@ private DocumentMapper merge(DocumentMapper mapper, boolean updateAllTypes) { if (mapper.type().length() == 0) { throw new InvalidTypeNameException("mapping type name is empty"); } + if (Version.indexCreated(indexSettings).onOrAfter(Version.V_2_0_0_beta1) && mapper.type().length() > 255) { + throw new InvalidTypeNameException("mapping type name [" + mapper.type() + "] is too long; limit is length 255 but was [" + mapper.type().length() + "]"); + } if (mapper.type().charAt(0) == '_') { throw new InvalidTypeNameException("mapping type name [" + mapper.type() + "] can't start with '_'"); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/MapperServiceTest.java b/core/src/test/java/org/elasticsearch/index/mapper/MapperServiceTest.java index 21f6fc8bebfff..d1c78c6b9f923 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/MapperServiceTest.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/MapperServiceTest.java @@ -19,12 +19,18 @@ package org.elasticsearch.index.mapper; +import org.elasticsearch.Version; +import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.test.ESSingleNodeTestCase; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import static org.elasticsearch.test.VersionUtils.getFirstVersion; +import static org.elasticsearch.test.VersionUtils.getPreviousVersion; +import static org.elasticsearch.test.VersionUtils.randomVersionBetween; import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.hasToString; public class MapperServiceTest extends ESSingleNodeTestCase { @@ -46,4 +52,39 @@ public void testTypeNameStartsWithIllegalDot() { .execute() .actionGet(); } + + @Test + public void testThatLongTypeNameIsNotRejectedOnPreElasticsearchVersionTwo() { + String index = "text-index"; + String field = "field"; + String type = new String(new char[256]).replace("\0", "a"); + + CreateIndexResponse response = + client() + .admin() + .indices() + .prepareCreate(index) + .setSettings(settings(randomVersionBetween(random(), getFirstVersion(), getPreviousVersion(Version.V_2_0_0_beta1)))) + .addMapping(type, field, "type=string") + .execute() + .actionGet(); + assertNotNull(response); + } + + @Test + public void testTypeNameTooLong() { + String index = "text-index"; + String field = "field"; + String type = new String(new char[256]).replace("\0", "a"); + + expectedException.expect(MapperParsingException.class); + expectedException.expect(hasToString(containsString("mapping type name [" + type + "] is too long; limit is length 255 but was [256]"))); + client() + .admin() + .indices() + .prepareCreate(index) + .addMapping(type, field, "type=string") + .execute() + .actionGet(); + } }