Skip to content

Commit 5b9234f

Browse files
committed
Fixed potential NullPointerException in PersistentPropertyResourceMapping.isExported().
Fixes GH-1994.
1 parent af9a26c commit 5b9234f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMapping.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ public boolean isExported() {
9191
}
9292

9393
ResourceMapping typeMapping = mappings.getMetadataFor(property.getAssociationTargetType());
94-
return !typeMapping.isExported() ? false : annotation.map(it -> it.exported()).orElse(true);
94+
95+
return typeMapping != null && typeMapping.isExported()
96+
? annotation.map(it -> it.exported()).orElse(true)
97+
: false;
9598
}
9699

97100
/*

spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.rest.core.mapping;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
1920

2021
import java.util.Arrays;
2122
import java.util.List;
@@ -27,6 +28,8 @@
2728
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
2829
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
2930
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
31+
import org.springframework.data.mapping.PersistentEntity;
32+
import org.springframework.data.mapping.PersistentProperty;
3033
import org.springframework.data.mapping.context.PersistentEntities;
3134
import org.springframework.data.rest.core.Path;
3235
import org.springframework.data.rest.core.annotation.Description;
@@ -98,6 +101,20 @@ public void considersAtDescription() {
98101
assertThat(description.getMessage()).isEqualTo("Some description");
99102
}
100103

104+
@Test // #1994
105+
public void doesNotConsiderPropertyExportedIfTargetTypeIsNotMapped() {
106+
107+
PersistentEntity<?, ?> entity = mappingContext.getRequiredPersistentEntity(Entity.class);
108+
PersistentProperty<?> property = entity.getRequiredPersistentProperty("third");
109+
110+
ResourceMappings mappings = mock(ResourceMappings.class);
111+
when(mappings.getMetadataFor(property.getType())).thenReturn(null);
112+
113+
ResourceMapping mapping = new PersistentPropertyResourceMapping(property, mappings);
114+
115+
assertThat(mapping.isExported()).isFalse();
116+
}
117+
101118
private ResourceMapping getPropertyMappingFor(Class<?> entity, String propertyName) {
102119

103120
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity);

0 commit comments

Comments
 (0)