diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolver.java index 1ba390295..b9d18e934 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolver.java @@ -26,6 +26,7 @@ import org.springframework.data.rest.webmvc.spi.BackendIdConverter; import org.springframework.data.rest.webmvc.spi.BackendIdConverter.DefaultIdConverter; import org.springframework.data.rest.webmvc.util.UriUtils; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; @@ -98,7 +99,10 @@ public class BackendIdHandlerMethodArgumentResolver implements HandlerMethodArgu BackendIdConverter pluginFor = idConverters.getPluginFor(metadata.getDomainType()) .orElse(DefaultIdConverter.INSTANCE); String lookupPath = baseUri.getRepositoryLookupPath(request); - return pluginFor.fromRequestId(UriUtils.findMappingVariable("id", parameter.getMethod(), lookupPath), - metadata.getDomainType()); + String idSource = UriUtils.findMappingVariable("id", parameter.getMethod(), lookupPath); + + return StringUtils.hasText(idSource) // + ? pluginFor.fromRequestId(idSource, metadata.getDomainType()) + : null; } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolverUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolverUnitTests.java new file mode 100644 index 000000000..b6523ec6f --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolverUnitTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.support; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; +import org.springframework.core.MethodParameter; +import org.springframework.data.rest.core.mapping.ResourceMetadata; +import org.springframework.data.rest.core.util.Java8PluginRegistry; +import org.springframework.data.rest.webmvc.BaseUri; +import org.springframework.data.rest.webmvc.config.ResourceMetadataHandlerMethodArgumentResolver; +import org.springframework.data.rest.webmvc.spi.BackendIdConverter; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.ServletWebRequest; +import org.springframework.web.method.support.ModelAndViewContainer; + +/** + * Unit tests for {@link BackendIdHandlerMethodArgumentResolver}. + * + * @author Oliver Drotbohm + */ +public class BackendIdHandlerMethodArgumentResolverUnitTests { + + BackendIdConverter converter = mock(BackendIdConverter.class); + List converters = Arrays.asList(converter); + Java8PluginRegistry> plugins = Java8PluginRegistry.of(converters); + ResourceMetadataHandlerMethodArgumentResolver delegate = mock(ResourceMetadataHandlerMethodArgumentResolver.class); + BackendIdHandlerMethodArgumentResolver resolver = new BackendIdHandlerMethodArgumentResolver(plugins, delegate, + BaseUri.NONE); + + @Test // DATAREST-1382 + public void returnsNullForUrisNotNotContainingUri() throws Exception { + + ResourceMetadata metadata = mock(ResourceMetadata.class); + + doReturn(metadata).when(delegate).resolveArgument(any(), any(), any(), any()); + doReturn(true).when(converter).supports(any()); + + MethodParameter parameter = new MethodParameter(Sample.class.getMethod("sampleMethod", Serializable.class), 0); + + Serializable result = resolver.resolveArgument(parameter, new ModelAndViewContainer(), + new ServletWebRequest(new MockHttpServletRequest()), mock(WebDataBinderFactory.class)); + + assertThat(result).isNull(); + verify(converter, never()).fromRequestId(eq(null), any()); + } + + interface Sample { + + @RequestMapping("/{repository}") + void sampleMethod(Serializable parameter); + } +}