提交 6770e4b3 编写于 作者: S Sam Brannen

Fix and document CompositeUriComponentsContributor#hasContributors()

Prior to this commit, the hasContributors() method incorrectly returned
false if contributors had been configured.

This commit fixes the logic in hasContributors() and documents it.

Closes #27271
上级 6177f00a
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
......@@ -31,8 +31,8 @@ import org.springframework.web.util.UriComponentsBuilder;
/**
* A {@link UriComponentsContributor} containing a list of other contributors
* to delegate and also encapsulating a specific {@link ConversionService} to
* use for formatting method argument values to Strings.
* to delegate to and also encapsulating a specific {@link ConversionService} to
* use for formatting method argument values as Strings.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
......@@ -52,7 +52,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
* and provide that to this constructor.
* @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
*/
public CompositeUriComponentsContributor(UriComponentsContributor... contributors) {
this.contributors = Arrays.asList((Object[]) contributors);
......@@ -66,7 +66,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
* and provide that to this constructor.
* @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
*/
public CompositeUriComponentsContributor(Collection<?> contributors) {
this(contributors, null);
......@@ -82,7 +82,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* {@link org.springframework.format.support.DefaultFormattingConversionService}
* will be used by default.
* @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
* @param cs a ConversionService to use when method argument values
* need to be formatted as Strings before being added to the URI
*/
......@@ -91,9 +91,14 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
this.conversionService = (cs != null ? cs : new DefaultFormattingConversionService());
}
/**
* Determine if this {@code CompositeUriComponentsContributor} has any
* contributors.
* @return {@code true} if this {@code CompositeUriComponentsContributor}
* was created with contributors to delegate to
*/
public boolean hasContributors() {
return this.contributors.isEmpty();
return !this.contributors.isEmpty();
}
@Override
......@@ -139,7 +144,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder,
Map<String, Object> uriVariables) {
this.contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService);
contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService);
}
}
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
......@@ -32,30 +32,33 @@ import org.springframework.web.method.annotation.RequestParamMethodArgumentResol
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for
* {@link org.springframework.web.method.support.CompositeUriComponentsContributor}.
* Unit tests for {@link CompositeUriComponentsContributor}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class CompositeUriComponentsContributorTests {
class CompositeUriComponentsContributorTests {
@Test
public void supportsParameter() {
void supportsParameter() {
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
resolvers.add(new RequestParamMethodArgumentResolver(false));
resolvers.add(new RequestHeaderMethodArgumentResolver(null));
resolvers.add(new RequestParamMethodArgumentResolver(true));
Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);
CompositeUriComponentsContributor contributor = new CompositeUriComponentsContributor(resolvers);
Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);
assertThat(contributor.supportsParameter(new MethodParameter(method, 0))).isTrue();
assertThat(contributor.supportsParameter(new MethodParameter(method, 1))).isTrue();
assertThat(contributor.supportsParameter(new MethodParameter(method, 2))).isFalse();
}
@Test
void hasContributors() {
assertThat(new CompositeUriComponentsContributor().hasContributors()).isFalse();
assertThat(new CompositeUriComponentsContributor(new RequestParamMethodArgumentResolver(true)).hasContributors()).isTrue();
}
public void handleRequest(@RequestParam String p1, String p2, @RequestHeader String h) {
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册