From 6199fed1c13d228f8a6c8c479f6273f869261f96 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 16 Nov 2017 16:29:06 +0100 Subject: [PATCH] DATACMNS-1214 - Fixed AbstractMappingContext.getPersistentEntity(PersistentProperty) to now return null for non-entities. Previously, a call to AbstractMappingContext.getPersistentEntity(PersistentProperty) would've added potentially leniently added the type of the given PersistentProperty, no matter whether it's actually considered to be an entity in the first place. We now defensively check for whether the given property is to be considered an entity (taking potentially registered converters into account) before the potentially entity-creating by-type lookup. --- .../context/AbstractMappingContext.java | 4 +++ .../AbstractMappingContextUnitTests.java | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 2cc249242..387d5ce9a 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -195,6 +195,10 @@ public abstract class AbstractMappingContext typeInfo = persistentProperty.getTypeInformation(); return getPersistentEntity(typeInfo.getActualType()); } diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index c18778bf4..25a9d426f 100644 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.*; import groovy.lang.MetaClass; +import java.time.LocalDateTime; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -49,13 +50,12 @@ import org.springframework.data.util.TypeInformation; */ public class AbstractMappingContextUnitTests { - final SimpleTypeHolder holder = new SimpleTypeHolder(); SampleMappingContext context; @Before public void setUp() { context = new SampleMappingContext(); - context.setSimpleTypeHolder(holder); + context.setSimpleTypeHolder(new SimpleTypeHolder(Collections.singleton(LocalDateTime.class), true)); } @Test @@ -96,16 +96,15 @@ public class AbstractMappingContextUnitTests { @Test public void registersEntitiesOnInitialization() { - ApplicationContext context = mock(ApplicationContext.class); + ApplicationContext applicationContext = mock(ApplicationContext.class); - SampleMappingContext mappingContext = new SampleMappingContext(); - mappingContext.setInitialEntitySet(Collections.singleton(Person.class)); - mappingContext.setApplicationEventPublisher(context); + context.setInitialEntitySet(Collections.singleton(Person.class)); + context.setApplicationEventPublisher(applicationContext); - verify(context, times(0)).publishEvent(Mockito.any(ApplicationEvent.class)); + verify(applicationContext, times(0)).publishEvent(Mockito.any(ApplicationEvent.class)); - mappingContext.afterPropertiesSet(); - verify(context, times(1)).publishEvent(Mockito.any(ApplicationEvent.class)); + context.afterPropertiesSet(); + verify(applicationContext, times(1)).publishEvent(Mockito.any(ApplicationEvent.class)); } @Test // DATACMNS-214 @@ -233,6 +232,15 @@ public class AbstractMappingContextUnitTests { } } + @Test // DATACMNS-1214 + public void doesNotReturnPersistentEntityForCustomSimpleTypeProperty() { + + PersistentEntity entity = context.getPersistentEntity(Person.class); + SamplePersistentProperty property = entity.getPersistentProperty("date"); + + assertThat(context.getPersistentEntity(property), is(nullValue())); + } + private static void assertHasEntityFor(Class type, SampleMappingContext context, boolean expected) { boolean found = false; @@ -251,6 +259,7 @@ public class AbstractMappingContextUnitTests { class Person { String name; + LocalDateTime date; } class Unsupported {