Apache Solr open-source search software
SOLR-17721 - null pointer fix in the doAddDistinct method (#3436)
Puneet Sharmaa1ebd0433c71
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex ab044e2f409..04099f55806 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -218,6 +218,8 @@ Bug Fixes* SOLR-17824: RecoveryStrategy.pingLeader could NPE when there's no shard leader (David Smiley)+* SOLR-17721: NPE can occur when doing Atomic Update using Add Distinct on documents with a null field value. (puneetSharma via Eric Pugh)+Dependency Upgrades---------------------(No changes)diff --git a/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java b/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.javaindex dbfd33959bd..01fab007eb3 100644--- a/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java+++ b/solr/core/src/java/org/apache/solr/update/processor/AtomicUpdateDocumentMerger.java@@ -545,8 +545,11 @@ public class AtomicUpdateDocumentMerger {final String name = sif.getName();SolrInputField existingField = toDoc.get(name);- Collection<Object> original =- existingField != null ? existingField.getValues() : new ArrayList<>();+ Collection<Object> original = existingField != null ? existingField.getValues() : null;++ if (original == null) {+ original = new ArrayList<>();+ }int initialSize = original.size();if (fieldVal instanceof Collection) {diff --git a/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.java b/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.javaindex 5194e1ea1ba..96813d95344 100644--- a/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.java+++ b/solr/core/src/test/org/apache/solr/update/processor/AtomicUpdatesTest.java@@ -1704,4 +1704,27 @@ public class AtomicUpdatesTest extends SolrTestCaseJ4 {"*[count(//result/doc[1]/arr[@name='intRemove']/int)=1]","//result/doc[1]/arr[@name='intRemove']/int[1][.=333]");}++ @Test+ public void testAddDistinctToNullField() {+ // Test that add-distinct works correctly when the field value is null+ SolrInputDocument doc = new SolrInputDocument();+ doc.setField("id", 9999);+ doc.setField("cat", null); // Set field to null+ assertU(adoc(doc));+ assertU(commit());++ // Now try to add-distinct to the null field+ doc = new SolrInputDocument();+ doc.setField("id", 9999);+ doc.setField("cat", Map.of("add-distinct", "new_value"));+ assertU(adoc(doc));+ assertU(commit());++ // Verify the value was added+ assertQ(+ req("q", "id:9999", "indent", "true"),+ "//result[@numFound = '1']",+ "//doc/arr[@name='cat']/str[.='new_value']");+ }}