Merge pull request #13036 from jasontedor/limit-type-name-length

Limit type name length
This commit is contained in:
Jason Tedor 2015-08-21 09:19:11 -04:00
commit 13c1c27d5a
3 changed files with 52 additions and 2 deletions

View File

@ -261,6 +261,9 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
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 '_'");
}

View File

@ -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 class MapperServiceTest extends ESSingleNodeTestCase {
.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();
}
}

View File

@ -65,9 +65,15 @@ public class VersionUtils {
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;
}