Fixed potential NullPointerException in PersistentPropertyResourceMapping.isExported().

Fixes GH-1994.
This commit is contained in:
Oliver Drotbohm
2021-04-07 08:22:02 +02:00
parent 904f99f31d
commit 20cb33512e
2 changed files with 21 additions and 1 deletions

View File

@@ -91,7 +91,10 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
}
ResourceMapping typeMapping = mappings.getMetadataFor(property.getActualType());
return !typeMapping.isExported() ? false : annotation.map(it -> it.exported()).orElse(true);
return typeMapping != null && typeMapping.isExported()
? annotation.map(it -> it.exported()).orElse(true)
: false;
}
/*

View File

@@ -16,6 +16,7 @@
package org.springframework.data.rest.core.mapping;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.List;
@@ -27,6 +28,8 @@ import org.springframework.data.annotation.Reference;
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.Description;
@@ -98,6 +101,20 @@ public class PersistentPropertyResourceMappingUnitTests {
assertThat(description.getMessage()).isEqualTo("Some description");
}
@Test // #1994
public void doesNotConsiderPropertyExportedIfTargetTypeIsNotMapped() {
PersistentEntity<?, ?> entity = mappingContext.getRequiredPersistentEntity(Entity.class);
PersistentProperty<?> property = entity.getRequiredPersistentProperty("third");
ResourceMappings mappings = mock(ResourceMappings.class);
when(mappings.getMetadataFor(property.getType())).thenReturn(null);
ResourceMapping mapping = new PersistentPropertyResourceMapping(property, mappings);
assertThat(mapping.isExported()).isFalse();
}
private ResourceMapping getPropertyMappingFor(Class<?> entity, String propertyName) {
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity);