SOLR-9411: Better validation for Schema API add-field

This commit is contained in:
Steve Rowe 2016-09-27 18:16:55 -04:00
parent a84d9a5d69
commit 9f35a6b829
3 changed files with 46 additions and 15 deletions

View File

@ -129,7 +129,7 @@ Bug Fixes
* SOLR-9330: Fix AlreadyClosedException on admin/mbeans?stats=true (Mikhail Khludnev)
* SOLR-9411: Better validation for Schema REST API add-dynamic-field (janhoy)
* SOLR-9411: Better validation for Schema API add-field and add-dynamic-field (janhoy, Steve Rowe)
Optimizations
----------------------

View File

@ -226,13 +226,8 @@ public class SchemaManager {
String type = op.getStr(TYPE);
if (op.hasError())
return false;
FieldType ft = mgr.managedIndexSchema.getFieldTypeByName(type);
if (ft == null) {
op.addError("No such field type '" + type + "'");
return false;
}
try {
SchemaField field = SchemaField.create(name, ft, op.getValuesExcluding(NAME, TYPE));
SchemaField field = mgr.managedIndexSchema.newField(name, type, op.getValuesExcluding(NAME, TYPE));
mgr.managedIndexSchema
= mgr.managedIndexSchema.addFields(singletonList(field), Collections.emptyMap(), false);
return true;
@ -248,11 +243,6 @@ public class SchemaManager {
String type = op.getStr(TYPE);
if (op.hasError())
return false;
FieldType ft = mgr.managedIndexSchema.getFieldTypeByName(type);
if (ft == null) {
op.addError("No such field type '" + type + "'");
return false;
}
try {
SchemaField field = mgr.managedIndexSchema.newDynamicField(name, type, op.getValuesExcluding(NAME, TYPE));
mgr.managedIndexSchema

View File

@ -92,14 +92,13 @@ public class TestBulkSchemaAPI extends RestTestBase {
String response = restTestHarness.post("/schema?wt=json", json(payload));
Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
List l = (List) map.get("errors");
assertNotNull("No errors", l);
List errorList = (List) ((Map) l.get(0)).get("errorMessages");
assertEquals(1, errorList.size());
assertTrue (((String)errorList.get(0)).contains("No such field type"));
assertTrue (((String)errorList.get(0)).contains("Field 'a1': Field type 'string1' not found.\n"));
errorList = (List) ((Map) l.get(1)).get("errorMessages");
assertEquals(1, errorList.size());
assertTrue (((String)errorList.get(0)).contains("is a required field"));
}
public void testAnalyzerClass() throws Exception {
@ -217,6 +216,48 @@ public class TestBulkSchemaAPI extends RestTestBase {
assertNull(newFieldName + " illegal dynamic field should not have been added to schema", map);
}
public void testAddIllegalFields() throws Exception {
RestTestHarness harness = restTestHarness;
// 1. Make sure you can't create a new field with an asterisk in its name
String newFieldName = "asterisk*";
String payload = "{\n" +
" 'add-field' : {\n" +
" 'name':'" + newFieldName + "',\n" +
" 'type':'string',\n" +
" 'stored':true,\n" +
" 'indexed':true\n" +
" }\n" +
"}";
String response = harness.post("/schema?wt=json", json(payload));
Map map = (Map)ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
assertNotNull(response, map.get("errors"));
map = getObj(harness, newFieldName, "fields");
assertNull(newFieldName + " illegal dynamic field should not have been added to schema", map);
// 2. Make sure you get an error when you try to create a field that already exists
// Make sure 'wdf_nocase' field exists
newFieldName = "wdf_nocase";
Map m = getObj(harness, newFieldName, "fields");
assertNotNull("'" + newFieldName + "' field does not exist in the schema", m);
payload = "{\n" +
" 'add-field' : {\n" +
" 'name':'" + newFieldName + "',\n" +
" 'type':'string',\n" +
" 'stored':true,\n" +
" 'indexed':true\n" +
" }\n" +
"}";
response = harness.post("/schema?wt=json", json(payload));
map = (Map)ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
assertNotNull(response, map.get("errors"));
}
public void testAddFieldWithExistingCatchallDynamicField() throws Exception {
RestTestHarness harness = restTestHarness;