Skip to content

Commit dff8716

Browse files
GH-2301 - Assert not null on domain class.
1 parent e1ea132 commit dff8716

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ public <T> List<T> saveAll(Iterable<T> instances) {
310310
}
311311

312312
Class<T> domainClass = (Class<T>) TemplateSupport.findCommonElementType(entities);
313-
Neo4jPersistentEntity entityMetaData = neo4jMappingContext.getPersistentEntity(domainClass);
313+
Assert.notNull(domainClass, "Could not determine common domain class to save.");
314+
Neo4jPersistentEntity<?> entityMetaData = neo4jMappingContext.getPersistentEntity(domainClass);
314315
if (entityMetaData.isUsingInternalIds() || entityMetaData.hasVersionProperty()
315316
|| entityMetaData.getDynamicLabelsProperty().isPresent()) {
316317
log.debug("Saving entities using single statements.");

src/main/java/org/springframework/data/neo4j/core/ReactiveNeo4jTemplate.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ public <T> Flux<T> saveAll(Iterable<T> instances) {
306306
}
307307

308308
Class<T> domainClass = (Class<T>) TemplateSupport.findCommonElementType(entities);
309-
Neo4jPersistentEntity entityMetaData = neo4jMappingContext.getPersistentEntity(domainClass);
309+
Assert.notNull(domainClass, "Could not determine common domain class to save.");
310+
Neo4jPersistentEntity<?> entityMetaData = neo4jMappingContext.getPersistentEntity(domainClass);
310311

311312
if (entityMetaData.isUsingInternalIds() || entityMetaData.hasVersionProperty()
312313
|| entityMetaData.getDynamicLabelsProperty().isPresent()) {

src/main/java/org/springframework/data/neo4j/core/TemplateSupport.java

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ final class TemplateSupport {
4242
@Nullable
4343
static Class<?> findCommonElementType(Iterable<?> collection) {
4444

45+
if (collection == null) {
46+
return null;
47+
}
48+
4549
List<Class<?>> allClasses = StreamSupport.stream(collection.spliterator(), true)
4650
.filter(o -> o != null)
4751
.map(Object::getClass).collect(Collectors.toList());

src/test/java/org/springframework/data/neo4j/core/TemplateSupportTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919

2020
import java.util.Arrays;
21+
import java.util.Collections;
2122

2223
import org.junit.jupiter.api.Test;
2324

@@ -76,6 +77,20 @@ void shouldNotFailWithNull() {
7677
assertThat(type).isNotNull().isEqualTo(A.class);
7778
}
7879

80+
@Test
81+
void shouldNotFailWithNullInput() {
82+
83+
Class<?> type = TemplateSupport.findCommonElementType(null);
84+
assertThat(type).isNull();
85+
}
86+
87+
@Test
88+
void shouldNotFailWithEmptyInput() {
89+
90+
Class<?> type = TemplateSupport.findCommonElementType(Collections.emptyList());
91+
assertThat(type).isNull();
92+
}
93+
7994
@Test
8095
void shouldFindCommonElementTypeOfHumongousCollection() {
8196

0 commit comments

Comments
 (0)