Skip to content

Commit e2bac30

Browse files
committed
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.
1 parent 24c1b82 commit e2bac30

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ public E getPersistentEntity(P persistentProperty) {
239239

240240
Assert.notNull(persistentProperty, "PersistentProperty must not be null!");
241241

242+
if (!persistentProperty.isEntity()) {
243+
return null;
244+
}
245+
242246
TypeInformation<?> typeInfo = persistentProperty.getTypeInformation();
243247
return getPersistentEntity(typeInfo.getRequiredActualType());
244248
}

src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import groovy.lang.MetaClass;
2222

23+
import java.time.LocalDateTime;
2324
import java.util.Collections;
2425
import java.util.Iterator;
2526
import java.util.List;
@@ -49,13 +50,12 @@
4950
*/
5051
public class AbstractMappingContextUnitTests {
5152

52-
final SimpleTypeHolder holder = SimpleTypeHolder.DEFAULT;
5353
SampleMappingContext context;
5454

5555
@Before
5656
public void setUp() {
5757
context = new SampleMappingContext();
58-
context.setSimpleTypeHolder(holder);
58+
context.setSimpleTypeHolder(new SimpleTypeHolder(Collections.singleton(LocalDateTime.class), true));
5959
}
6060

6161
@Test
@@ -96,16 +96,15 @@ public void verify() {
9696
@Test
9797
public void registersEntitiesOnInitialization() {
9898

99-
ApplicationContext context = mock(ApplicationContext.class);
99+
ApplicationContext applicationContext = mock(ApplicationContext.class);
100100

101-
SampleMappingContext mappingContext = new SampleMappingContext();
102-
mappingContext.setInitialEntitySet(Collections.singleton(Person.class));
103-
mappingContext.setApplicationEventPublisher(context);
101+
context.setInitialEntitySet(Collections.singleton(Person.class));
102+
context.setApplicationEventPublisher(applicationContext);
104103

105-
verify(context, times(0)).publishEvent(Mockito.any(ApplicationEvent.class));
104+
verify(applicationContext, times(0)).publishEvent(Mockito.any(ApplicationEvent.class));
106105

107-
mappingContext.afterPropertiesSet();
108-
verify(context, times(1)).publishEvent(Mockito.any(ApplicationEvent.class));
106+
context.afterPropertiesSet();
107+
verify(applicationContext, times(1)).publishEvent(Mockito.any(ApplicationEvent.class));
109108
}
110109

111110
@Test // DATACMNS-214
@@ -247,13 +246,20 @@ public void cachesPersistentPropertyPaths() {
247246
@Test // DATACMNS-1208
248247
public void ensureHasPersistentEntityReportsFalseForTypesThatShouldntBeCreated() {
249248

250-
SampleMappingContext context = new SampleMappingContext();
251-
252249
assertThat(context.hasPersistentEntityFor(String.class)).isFalse();
253250
assertThat(context.getPersistentEntity(String.class)).isNull();
254251
assertThat(context.hasPersistentEntityFor(String.class)).isFalse();
255252
}
256253

254+
@Test // DATACMNS-1214
255+
public void doesNotReturnPersistentEntityForCustomSimpleTypeProperty() {
256+
257+
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Person.class);
258+
SamplePersistentProperty property = entity.getRequiredPersistentProperty("date");
259+
260+
assertThat(context.getPersistentEntity(property)).isNull();
261+
}
262+
257263
private static void assertHasEntityFor(Class<?> type, SampleMappingContext context, boolean expected) {
258264

259265
boolean found = false;
@@ -272,6 +278,7 @@ private static void assertHasEntityFor(Class<?> type, SampleMappingContext conte
272278

273279
class Person {
274280
String name;
281+
LocalDateTime date;
275282
}
276283

277284
class Unsupported {

0 commit comments

Comments
 (0)