Skip to content

Apache Lucene open-source search software

SOLR-8194: Improve error reporting of nulls in UpdateRequest

Alan Woodward23dabd19ff31
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 2457082a7ae..df4bbb05027 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -554,6 +554,9 @@ Other Changes
* SOLR-8303: CustomBufferedIndexInput now includes resource description when
throwing EOFException. (Mike Drob via Uwe Schindler)
+* SOLR-8194: Improve error reporting for null documents in UpdateRequest (Markus
+ Jelsma, Alan Woodward)
+
================== 5.3.1 ==================
Bug Fixes
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java
index 3472347bdd3..4537c79144f 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java
@@ -17,16 +17,6 @@
package org.apache.solr.client.solrj.request;
-import org.apache.solr.client.solrj.impl.LBHttpSolrClient;
-import org.apache.solr.client.solrj.util.ClientUtils;
-import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.common.cloud.DocCollection;
-import org.apache.solr.common.cloud.DocRouter;
-import org.apache.solr.common.cloud.Slice;
-import org.apache.solr.common.params.ModifiableSolrParams;
-import org.apache.solr.common.util.ContentStream;
-import org.apache.solr.common.util.XML;
-
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
@@ -38,8 +28,19 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.Set;
+import org.apache.solr.client.solrj.impl.LBHttpSolrClient;
+import org.apache.solr.client.solrj.util.ClientUtils;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.DocRouter;
+import org.apache.solr.common.cloud.Slice;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.XML;
+
/**
*
*
@@ -88,25 +89,50 @@ public class UpdateRequest extends AbstractUpdateRequest {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
-
+
+ /**
+ * Add a SolrInputDocument to this request
+ *
+ * @throws NullPointerException if the document is null
+ */
public UpdateRequest add(final SolrInputDocument doc) {
+ Objects.requireNonNull(doc, "Cannot add a null SolrInputDocument");
if (documents == null) {
documents = new LinkedHashMap<>();
}
documents.put(doc, null);
return this;
}
-
+
+ /**
+ * Add a SolrInputDocument to this request
+ * @param doc the document
+ * @param overwrite true if the document should overwrite existing docs with the same id
+ * @throws NullPointerException if the document is null
+ */
public UpdateRequest add(final SolrInputDocument doc, Boolean overwrite) {
return add(doc, null, overwrite);
}
-
+
+ /**
+ * Add a SolrInputDocument to this request
+ * @param doc the document
+ * @param commitWithin the time horizon by which the document should be committed (in ms)
+ * @throws NullPointerException if the document is null
+ */
public UpdateRequest add(final SolrInputDocument doc, Integer commitWithin) {
return add(doc, commitWithin, null);
}
-
- public UpdateRequest add(final SolrInputDocument doc, Integer commitWithin,
- Boolean overwrite) {
+
+ /**
+ * Add a SolrInputDocument to this request
+ * @param doc the document
+ * @param commitWithin the time horizon by which the document should be committed (in ms)
+ * @param overwrite true if the document should overwrite existing docs with the same id
+ * @throws NullPointerException if the document is null
+ */
+ public UpdateRequest add(final SolrInputDocument doc, Integer commitWithin, Boolean overwrite) {
+ Objects.requireNonNull(doc, "Cannot add a null SolrInputDocument");
if (documents == null) {
documents = new LinkedHashMap<>();
}
@@ -118,12 +144,18 @@ public class UpdateRequest extends AbstractUpdateRequest {
return this;
}
-
+
+ /**
+ * Add a collection of SolrInputDocuments to this request
+ *
+ * @throws NullPointerException if any of the documents in the collection are null
+ */
public UpdateRequest add(final Collection<SolrInputDocument> docs) {
if (documents == null) {
documents = new LinkedHashMap<>();
}
for (SolrInputDocument doc : docs) {
+ Objects.requireNonNull(doc, "Cannot add a null SolrInputDocument");
documents.put(doc, null);
}
return this;
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestUpdateRequest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestUpdateRequest.java
new file mode 100644
index 00000000000..1c755a579c5
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestUpdateRequest.java
@@ -0,0 +1,69 @@
+package org.apache.solr.client.solrj.request;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.Arrays;
+
+import org.apache.solr.common.SolrInputDocument;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class TestUpdateRequest {
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ @Before
+ public void expectException() {
+ exception.expect(NullPointerException.class);
+ exception.expectMessage("Cannot add a null SolrInputDocument");
+ }
+
+ @Test
+ public void testCannotAddNullSolrInputDocument() {
+ UpdateRequest req = new UpdateRequest();
+ req.add((SolrInputDocument) null);
+ }
+
+ @Test
+ public void testCannotAddNullDocumentWithOverwrite() {
+ UpdateRequest req = new UpdateRequest();
+ req.add(null, true);
+ }
+
+ @Test
+ public void testCannotAddNullDocumentWithCommitWithin() {
+ UpdateRequest req = new UpdateRequest();
+ req.add(null, 1);
+ }
+
+ @Test
+ public void testCannotAddNullDocumentWithParameters() {
+ UpdateRequest req = new UpdateRequest();
+ req.add(null, 1, true);
+ }
+
+ @Test
+ public void testCannotAddNullDocumentAsPartOfList() {
+ UpdateRequest req = new UpdateRequest();
+ req.add(Arrays.asList(new SolrInputDocument(), new SolrInputDocument(), null));
+ }
+
+}