From bab5ddade97cf32e2ce6a758b290d33427df6c10 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 24 Jan 2017 08:24:13 +0100 Subject: [PATCH] DATACMNS-867 - ReflectionEntityInformation getId(Object) considers potential null values. --- .../repository/core/support/ReflectionEntityInformation.java | 2 +- .../core/support/ReflectionEntityInformationUnitTests.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java b/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java index 819014d48..9e074212d 100644 --- a/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java +++ b/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java @@ -76,7 +76,7 @@ public class ReflectionEntityInformation extends Abs */ @SuppressWarnings("unchecked") public Optional getId(Object entity) { - return entity == null ? null : Optional.of((ID) ReflectionUtils.getField(field, entity)); + return entity == null ? null : Optional.ofNullable((ID) ReflectionUtils.getField(field, entity)); } /* diff --git a/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java index 23c99834b..1f0128b30 100755 --- a/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java @@ -57,6 +57,11 @@ public class ReflectionEntityInformationUnitTests { assertThat(information.isNew(primitiveId)).isFalse(); } + @Test // DATACMNS-867 + public void detectsNewStateForEntityWithNullId() { + assertThat(new ReflectionEntityInformation<>(Sample.class).isNew(new Sample())).isTrue(); + } + private static EntityInformation getEntityInformation(Class type) { return new ReflectionEntityInformation(type, Id.class); }