From e4c1625447d2dacfacdf4918d8cdf5ba77efdbdd Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 7 Feb 2019 20:49:22 +0100 Subject: [PATCH] DATAREST-1332 - Fixed regression in CORS request handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../webmvc/RepositoryRestHandlerMapping.java | 5 +++++ .../RepositoryRestHandlerMappingUnitTests.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index 6ed732cd0..1bc91dcdd 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -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() // diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java index cda3df521..13df485e3 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java @@ -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);