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:
@@ -195,6 +195,10 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!persistentProperty.isEntity()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TypeInformation<?> typeInfo = persistentProperty.getTypeInformation();
|
||||
return getPersistentEntity(typeInfo.getActualType());
|
||||
}
|
||||
|
||||
@@ -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<Object, SamplePersistentProperty> 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 {
|
||||
|
||||
Reference in New Issue
Block a user