Apache Lucene open-source search software
SOLR-12704: Guard AddSchemaFieldsUpdateProcessorFactory against null field names and field values
Varun Thackerd55a81df849e
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex f0d49ea7c61..94b12be4639 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -295,6 +295,9 @@ Bug Fixes* SOLR-11585: Solr SQL does not work with point numeric fields (Joel Bernstein, Kiran Chitturi)+* SOLR-12704: Guard AddSchemaFieldsUpdateProcessorFactory against null field names and field values.+ (Steve Rowe, Varun Thacker)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.javaindex 6b8f1d54595..131d72c9d9d 100644--- a/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java+++ b/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java@@ -481,13 +481,18 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcessoprivate void getUnknownFields(FieldNameSelector selector, SolrInputDocument doc, Map<String,List<SolrInputField>> unknownFields) {for (final String fieldName : doc.getFieldNames()) {- if (selector.shouldMutate(fieldName)) { // returns false if the field already exists in the current schema- List<SolrInputField> solrInputFields = unknownFields.get(fieldName);- if (null == solrInputFields) {- solrInputFields = new ArrayList<>();- unknownFields.put(fieldName, solrInputFields);+ //We do a assert and a null check because even after SOLR-12710 is addressed+ //older SolrJ versions can send null values causing an NPE+ assert fieldName != null;+ if (fieldName != null) {+ if (selector.shouldMutate(fieldName)) { // returns false if the field already exists in the current schema+ List<SolrInputField> solrInputFields = unknownFields.get(fieldName);+ if (null == solrInputFields) {+ solrInputFields = new ArrayList<>();+ unknownFields.put(fieldName, solrInputFields);+ }+ solrInputFields.add(doc.getField(fieldName));}- solrInputFields.add(doc.getField(fieldName));}}List<SolrInputDocument> childDocs = doc.getChildDocuments();@@ -506,15 +511,20 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcessoprivate TypeMapping mapValueClassesToFieldType(List<SolrInputField> fields) {NEXT_TYPE_MAPPING: for (TypeMapping typeMapping : typeMappings) {for (SolrInputField field : fields) {- NEXT_FIELD_VALUE: for (Object fieldValue : field.getValues()) {- for (Class<?> valueClass : typeMapping.valueClasses) {- if (valueClass.isInstance(fieldValue)) {- continue NEXT_FIELD_VALUE;+ //We do a assert and a null check because even after SOLR-12710 is addressed+ //older SolrJ versions can send null values causing an NPE+ assert field.getValues() != null;+ if (field.getValues() != null) {+ NEXT_FIELD_VALUE: for (Object fieldValue : field.getValues()) {+ for (Class<?> valueClass : typeMapping.valueClasses) {+ if (valueClass.isInstance(fieldValue)) {+ continue NEXT_FIELD_VALUE;+ }}+ // This fieldValue is not an instance of any of the mapped valueClass-s,+ // so mapping fails - go try the next type mapping.+ continue NEXT_TYPE_MAPPING;}- // This fieldValue is not an instance of any of the mapped valueClass-s,- // so mapping fails - go try the next type mapping.- continue NEXT_TYPE_MAPPING;}}// Success! Each of this field's values is an instance of a mapped valueClassdiff --git a/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java b/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.javaindex 96cc6766e93..a49e9fe9b77 100644--- a/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java+++ b/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java@@ -59,6 +59,21 @@ public class AddSchemaFieldsUpdateProcessorFactoryTest extends UpdateProcessorTeinitCore(SOLRCONFIG_XML, SCHEMA_XML, tmpSolrHome.getPath());}+ public void testEmptyValue() {+ IndexSchema schema = h.getCore().getLatestSchema();+ final String fieldName = "newFieldABC";+ assertNull(schema.getFieldOrNull(fieldName));+ //UpdateProcessorTestBase#doc doesn't deal with nulls+ SolrInputDocument doc = new SolrInputDocument();+ doc.addField("id", "1");+ doc.addField(fieldName, null);++ SolrInputDocument finalDoc = doc;+ expectThrows(AssertionError.class, () -> processAdd("add-fields-no-run-processor", finalDoc));++ expectThrows(AssertionError.class, () -> processAdd("add-fields-no-run-processor", new SolrInputDocument(null , null)));+ }+public void testSingleField() throws Exception {IndexSchema schema = h.getCore().getLatestSchema();final String fieldName = "newfield1";