Skip to content

Commit 39b897e

Browse files
committed
DATAREST-1320 - Fixed lookup of cached values in PersistentEntitiesResourceMappings.
We now properly only answer PersistentEntitiesResourceMappings.hasMappingsFor(…) with true if we find a non-null value in the cache. Previously, even a failed attempt to create some ResourceMapping would've caused ….hasMappingsFor(…) to indicate it PersistentEntitiesResourceMappings contains a mapping. This lead to downstream NullPointerExceptions as ….getMetadataFor(…) was accessed without a null guard after ….hasMappingFor(…) returned true.
1 parent 8c0348f commit 39b897e

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ public boolean exportsTopLevelResourceFor(String path) {
152152
*/
153153
@Override
154154
public boolean hasMappingFor(Class<?> type) {
155-
156-
if (cache.containsKey(type)) {
157-
return true;
158-
}
159-
160-
return false;
155+
return cache.get(ProxyUtils.getUserClass(type)) != null;
161156
}
162157

163158
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.core.mapping;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
22+
import org.springframework.data.mapping.context.PersistentEntities;
23+
24+
/**
25+
* Unit tests for {@link PersistentEntitiesResourceMappings}.
26+
*
27+
* @author Oliver Gierke
28+
*/
29+
public class PersistentEntitiesResourceMappingsUnitTests {
30+
31+
@Test // DATAREST-1320
32+
public void doesNotConsiderCachedNullValuesToIndicateMappingAvailable() {
33+
34+
KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
35+
36+
PersistentEntitiesResourceMappings mappings = new PersistentEntitiesResourceMappings(
37+
PersistentEntities.of(context));
38+
39+
assertThat(mappings.getMetadataFor(String.class)).isNull();
40+
assertThat(mappings.hasMappingFor(String.class)).isFalse();
41+
}
42+
}

0 commit comments

Comments
 (0)