From 1a5a74d2e9b98b729d30bab9e831c0ed85597377 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 10 Jul 2020 10:59:17 +0200 Subject: [PATCH] DATAKV-310 - Guard repository implementations against null values. SimpleKeyValueRepository and QuerydslKeyValueRepository now reject null arguments as per their interface contract. Original Pull Request: #50 --- .../support/QuerydslKeyValueRepository.java | 23 +++++++++++++++++++ .../support/SimpleKeyValueRepository.java | 19 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java index a621c85..8233835 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java @@ -96,6 +96,8 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< @Override public Optional findOne(Predicate predicate) { + Assert.notNull(predicate, "Predicate must not be null"); + try { return Optional.ofNullable(prepareQuery(predicate).fetchOne()); } catch (NonUniqueResultException o_O) { @@ -109,6 +111,9 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< */ @Override public Iterable findAll(Predicate predicate) { + + Assert.notNull(predicate, "Predicate must not be null"); + return prepareQuery(predicate).fetchResults().getResults(); } @@ -119,6 +124,9 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< @Override public Iterable findAll(Predicate predicate, OrderSpecifier... orders) { + Assert.notNull(predicate, "Predicate must not be null"); + Assert.notNull(orders, "OrderSpecifiers must not be null"); + AbstractCollQuery query = prepareQuery(predicate); query.orderBy(orders); @@ -131,6 +139,10 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< */ @Override public Iterable findAll(Predicate predicate, Sort sort) { + + Assert.notNull(predicate, "Predicate must not be null"); + Assert.notNull(sort, "Sort must not be null"); + return findAll(predicate, toOrderSpecifier(sort, builder)); } @@ -141,6 +153,9 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< @Override public Page findAll(Predicate predicate, Pageable pageable) { + Assert.notNull(predicate, "Predicate must not be null"); + Assert.notNull(pageable, "Pageable must not be null"); + AbstractCollQuery query = prepareQuery(predicate); if (pageable.isPaged() || pageable.getSort().isSorted()) { @@ -163,6 +178,8 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< @Override public Iterable findAll(OrderSpecifier... orders) { + Assert.notNull(orders, "OrderSpecifiers must not be null"); + if (ObjectUtils.isEmpty(orders)) { return findAll(); } @@ -179,6 +196,9 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< */ @Override public long count(Predicate predicate) { + + Assert.notNull(predicate, "Predicate must not be null"); + return prepareQuery(predicate).fetchCount(); } @@ -188,6 +208,9 @@ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository< */ @Override public boolean exists(Predicate predicate) { + + Assert.notNull(predicate, "Predicate must not be null"); + return count(predicate) > 0; } diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java index 374b164..de84ce9 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java @@ -64,6 +64,9 @@ public class SimpleKeyValueRepository implements KeyValueRepository findAll(Sort sort) { + + Assert.notNull(sort, "Sort must not be null!"); + return operations.findAll(sort, entityInformation.getJavaType()); } @@ -111,6 +114,8 @@ public class SimpleKeyValueRepository implements KeyValueRepository Iterable saveAll(Iterable entities) { + Assert.notNull(entities, "The given Iterable of entities must not be null"); + List saved = new ArrayList<>(); for (S entity : entities) { @@ -126,6 +131,9 @@ public class SimpleKeyValueRepository implements KeyValueRepository findById(ID id) { + + Assert.notNull(id, "The given id must not be null"); + return operations.findById(id, entityInformation.getJavaType()); } @@ -154,6 +162,8 @@ public class SimpleKeyValueRepository implements KeyValueRepository findAllById(Iterable ids) { + Assert.notNull(ids, "The given Iterable of id's must not be null"); + List result = new ArrayList<>(); ids.forEach(id -> findById(id).ifPresent(result::add)); @@ -176,6 +186,9 @@ public class SimpleKeyValueRepository implements KeyValueRepository implements KeyValueRepository implements KeyValueRepository entities) { + + Assert.notNull(entities, "The given Iterable of entities must not be null"); + entities.forEach(this::delete); }