DATAREST-1332 - Fixed regression in CORS request handling.

The lookup of the RequestMappingInfo in exposeEffectiveLookupPathKey(…) yields null for CORS preflight requests to resources that do not support OPTIONAL explicitly. We now guard against that to avoid a subsequent NullPointerException.
This commit is contained in:
Oliver Drotbohm
2019-02-07 20:49:22 +01:00
parent 542b8eb2b0
commit e4fc7ae65a
2 changed files with 21 additions and 0 deletions

View File

@@ -261,6 +261,11 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
String repositoryBasePath) {
RequestMappingInfo mappingInfo = getMappingForMethod(method.getMethod(), method.getBeanType());
if (mappingInfo == null) {
return;
}
String pattern = mappingInfo.getPatternsCondition() //
.getMatchingCondition(request)//
.getPatterns() //

View File

@@ -38,6 +38,7 @@ import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.data.rest.webmvc.support.DefaultedPageable;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext;
import org.springframework.util.ClassUtils;
@@ -287,6 +288,21 @@ public class RepositoryRestHandlerMappingUnitTests {
});
}
@Test // DATAREST-1332
public void handlesCorsPreflightRequestsProperly() throws Exception {
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/people/search");
request.addHeader(HttpHeaders.ORIGIN, "test case");
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
handlerMapping.afterPropertiesSet();
assertThatCode(() -> handlerMapping.lookupHandlerMethod("/people/search", request)) //
.doesNotThrowAnyException();
}
private static Class<?> createProxy(Object source) {
ProxyFactory factory = new ProxyFactory(source);