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.
This commit is contained in:
Oliver Gierke
2017-11-16 16:29:06 +01:00
parent 875cb11db1
commit 39c67e3fef
2 changed files with 22 additions and 11 deletions

View File

@@ -239,6 +239,10 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
Assert.notNull(persistentProperty, "PersistentProperty must not be null!");
if (!persistentProperty.isEntity()) {
return null;
}
TypeInformation<?> typeInfo = persistentProperty.getTypeInformation();
return getPersistentEntity(typeInfo.getRequiredActualType());
}

View File

@@ -20,6 +20,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.util.StringUtils;
*/
public class AbstractMappingContextUnitTests {
final SimpleTypeHolder holder = SimpleTypeHolder.DEFAULT;
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
@@ -247,13 +246,20 @@ public class AbstractMappingContextUnitTests {
@Test // DATACMNS-1208
public void ensureHasPersistentEntityReportsFalseForTypesThatShouldntBeCreated() {
SampleMappingContext context = new SampleMappingContext();
assertThat(context.hasPersistentEntityFor(String.class)).isFalse();
assertThat(context.getPersistentEntity(String.class)).isNull();
assertThat(context.hasPersistentEntityFor(String.class)).isFalse();
}
@Test // DATACMNS-1214
public void doesNotReturnPersistentEntityForCustomSimpleTypeProperty() {
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Person.class);
SamplePersistentProperty property = entity.getRequiredPersistentProperty("date");
assertThat(context.getPersistentEntity(property)).isNull();
}
private static void assertHasEntityFor(Class<?> type, SampleMappingContext context, boolean expected) {
boolean found = false;
@@ -272,6 +278,7 @@ public class AbstractMappingContextUnitTests {
class Person {
String name;
LocalDateTime date;
}
class Unsupported {