DATAREST-1382 - Avoid to call BackendIdConverter if no id used for the request mapping.

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.
This commit is contained in:
Oliver Drotbohm
2019-06-04 23:39:10 +02:00
parent cf18d6aff7
commit e5018e003c
2 changed files with 81 additions and 2 deletions

View File

@@ -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;
}
}

View File

@@ -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<BackendIdConverter> converters = Arrays.asList(converter);
Java8PluginRegistry<BackendIdConverter, Class<?>> 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);
}
}