diff --git a/src/main/java/org/springframework/data/elasticsearch/Document.java b/src/main/java/org/springframework/data/elasticsearch/Document.java index 703e40c25..5e68afebd 100644 --- a/src/main/java/org/springframework/data/elasticsearch/Document.java +++ b/src/main/java/org/springframework/data/elasticsearch/Document.java @@ -37,6 +37,7 @@ import org.springframework.util.Assert; * {@link UnsupportedOperationException} when calling modifying methods. * * @author Mark Paluch + * @author Peter-Josef Meisch * @since 4.0 */ public interface Document extends Map { @@ -53,7 +54,7 @@ public interface Document extends Map { /** * Create a {@link Document} from a {@link Map} containing key-value pairs and sub-documents. * - * @param map source map containing key-value pairs and sub-documents. + * @param map source map containing key-value pairs and sub-documents. must not be {@literal null}. * @return a new {@link Document}. */ static Document from(Map map) { @@ -74,6 +75,9 @@ public interface Document extends Map { * @return the parsed {@link Document}. */ static Document parse(String json) { + + Assert.notNull(json, "JSON must not be null"); + try { return new MapDocument(MapDocument.OBJECT_MAPPER.readerFor(Map.class).readValue(json)); } catch (IOException e) { @@ -84,11 +88,14 @@ public interface Document extends Map { /** * {@link #put(Object, Object)} the {@code key}/{@code value} tuple and return {@code this} {@link Document}. * - * @param key key with which the specified value is to be associated. + * @param key key with which the specified value is to be associated. must not be {@literal null}. * @param value value to be associated with the specified key. * @return {@code this} {@link Document}. */ default Document append(String key, Object value) { + + Assert.notNull(key, "Key must not be null"); + put(key, value); return this; } @@ -369,12 +376,15 @@ public interface Document extends Map { *

* Any exception thrown by the function will be propagated to the caller. * - * @param transformer functional interface to a apply + * @param transformer functional interface to a apply. must not be {@literal null}. * @param class of the result * @return the result of applying the function to this string * @see java.util.function.Function */ default R transform(Function transformer) { + + Assert.notNull(transformer, "transformer must not be null"); + return transformer.apply(this); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DocumentAdapters.java b/src/main/java/org/springframework/data/elasticsearch/core/DocumentAdapters.java index 9315da5c2..be965a8a6 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/DocumentAdapters.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/DocumentAdapters.java @@ -44,8 +44,9 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; /** - * Utility class to adapt {@link org.elasticsearch.common.document.DocumentField} to - * {@link org.springframework.data.elasticsearch.Document}. + * Utility class to adapt {@link org.elasticsearch.action.get.GetResponse}, + * {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.search.SearchHit}, + * {@link org.elasticsearch.common.document.DocumentField} to {@link org.springframework.data.elasticsearch.Document}. * * @author Mark Paluch * @since 4.0 diff --git a/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java index 77804f211..5417e88cc 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java @@ -29,7 +29,6 @@ import org.elasticsearch.common.text.Text; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.search.SearchHit; import org.junit.Test; - import org.springframework.data.elasticsearch.Document; import org.springframework.data.elasticsearch.SearchDocument; @@ -37,6 +36,7 @@ import org.springframework.data.elasticsearch.SearchDocument; * Unit tests for {@link DocumentAdapters}. * * @author Mark Paluch + * @author Peter-Josef Meisch */ public class DocumentAdaptersUnitTests { @@ -46,7 +46,7 @@ public class DocumentAdaptersUnitTests { Map fields = Collections.singletonMap("field", new DocumentField("field", Arrays.asList("value"))); - GetResult getResult = new GetResult("index", "type", "my-id", 1, 0, 42, true, null, fields); + GetResult getResult = new GetResult("index", "type", "my-id", 1, 1, 42, true, null, fields, null); GetResponse response = new GetResponse(getResult); Document document = DocumentAdapters.from(response); @@ -63,7 +63,7 @@ public class DocumentAdaptersUnitTests { BytesArray source = new BytesArray("{\"field\":\"value\"}"); - GetResult getResult = new GetResult("index", "type", "my-id", 1, 0, 42, true, source, Collections.emptyMap()); + GetResult getResult = new GetResult("index", "type", "my-id", 1, 1, 42, true, source, Collections.emptyMap(), null); GetResponse response = new GetResponse(getResult); Document document = DocumentAdapters.from(response);