From 3c635c3c8165075199e539ab1275ed4051bb04ed Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 4 Jun 2019 23:39:10 +0200 Subject: [PATCH] DATAREST-1382 - Avoid to call BackendIdConverter if no id used for the request mapping. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Contrary to what's stated in the Javadoc of BackendIdConverter, BackendIdConverterHandlerMethodArgumentResolver now skips the invocation of ….fromRequestId(…) if no identifier path variable is found for the mapping of the method invoked. --- ...ackendIdHandlerMethodArgumentResolver.java | 8 ++- ...andlerMethodArgumentResolverUnitTests.java | 71 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolverUnitTests.java 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 1acab29ef..367c73200 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.DefaultIdConv import org.springframework.data.rest.webmvc.util.UriUtils; import org.springframework.plugin.core.PluginRegistry; 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..27bb42f08 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/BackendIdHandlerMethodArgumentResolverUnitTests.java @@ -0,0 +1,71 @@ +/* + * 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 org.junit.Test; +import org.springframework.core.MethodParameter; +import org.springframework.data.rest.core.mapping.ResourceMetadata; +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.plugin.core.PluginRegistry; +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); + ResourceMetadataHandlerMethodArgumentResolver delegate = mock(ResourceMetadataHandlerMethodArgumentResolver.class); + BackendIdHandlerMethodArgumentResolver resolver = new BackendIdHandlerMethodArgumentResolver( + PluginRegistry.of(converter), 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); + } +}