Skip to content

Commit ab2a861

Browse files
committed
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
1 parent f56d6ea commit ab2a861

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

spring-web/src/main/java/org/springframework/web/method/support/CompositeUriComponentsContributor.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,8 +30,8 @@
3030

3131
/**
3232
* A {@link UriComponentsContributor} containing a list of other contributors
33-
* to delegate and also encapsulating a specific {@link ConversionService} to
34-
* use for formatting method argument values to Strings.
33+
* to delegate to and also encapsulating a specific {@link ConversionService} to
34+
* use for formatting method argument values as Strings.
3535
*
3636
* @author Rossen Stoyanchev
3737
* @since 4.0
@@ -50,7 +50,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
5050
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
5151
* and provide that to this constructor.
5252
* @param contributors a collection of {@link UriComponentsContributor}
53-
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
53+
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
5454
*/
5555
public CompositeUriComponentsContributor(UriComponentsContributor... contributors) {
5656
Collections.addAll(this.contributors, contributors);
@@ -64,7 +64,7 @@ public CompositeUriComponentsContributor(UriComponentsContributor... contributor
6464
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
6565
* and provide that to this constructor.
6666
* @param contributors a collection of {@link UriComponentsContributor}
67-
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
67+
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
6868
*/
6969
public CompositeUriComponentsContributor(Collection<?> contributors) {
7070
this(contributors, null);
@@ -80,7 +80,7 @@ public CompositeUriComponentsContributor(Collection<?> contributors) {
8080
* {@link org.springframework.format.support.DefaultFormattingConversionService}
8181
* will be used by default.
8282
* @param contributors a collection of {@link UriComponentsContributor}
83-
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
83+
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
8484
* @param cs a ConversionService to use when method argument values
8585
* need to be formatted as Strings before being added to the URI
8686
*/
@@ -91,9 +91,14 @@ public CompositeUriComponentsContributor(@Nullable Collection<?> contributors, @
9191
this.conversionService = (cs != null ? cs : new DefaultFormattingConversionService());
9292
}
9393

94-
94+
/**
95+
* Determine if this {@code CompositeUriComponentsContributor} has any
96+
* contributors.
97+
* @return {@code true} if this {@code CompositeUriComponentsContributor}
98+
* was created with contributors to delegate to
99+
*/
95100
public boolean hasContributors() {
96-
return this.contributors.isEmpty();
101+
return !this.contributors.isEmpty();
97102
}
98103

99104
@Override
@@ -139,7 +144,7 @@ else if (contributor instanceof HandlerMethodArgumentResolver) {
139144
public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder,
140145
Map<String, Object> uriVariables) {
141146

142-
this.contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService);
147+
contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService);
143148
}
144149

145150
}

spring-web/src/test/java/org/springframework/web/method/support/CompositeUriComponentsContributorTests.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,30 +32,33 @@
3232
import static org.assertj.core.api.Assertions.assertThat;
3333

3434
/**
35-
* Unit tests for
36-
* {@link org.springframework.web.method.support.CompositeUriComponentsContributor}.
35+
* Unit tests for {@link CompositeUriComponentsContributor}.
3736
*
3837
* @author Rossen Stoyanchev
38+
* @author Sam Brannen
3939
*/
40-
public class CompositeUriComponentsContributorTests {
41-
40+
class CompositeUriComponentsContributorTests {
4241

4342
@Test
44-
public void supportsParameter() {
45-
43+
void supportsParameter() {
4644
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
4745
resolvers.add(new RequestParamMethodArgumentResolver(false));
4846
resolvers.add(new RequestHeaderMethodArgumentResolver(null));
4947
resolvers.add(new RequestParamMethodArgumentResolver(true));
5048

51-
Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);
52-
5349
CompositeUriComponentsContributor contributor = new CompositeUriComponentsContributor(resolvers);
50+
Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);
5451
assertThat(contributor.supportsParameter(new MethodParameter(method, 0))).isTrue();
5552
assertThat(contributor.supportsParameter(new MethodParameter(method, 1))).isTrue();
5653
assertThat(contributor.supportsParameter(new MethodParameter(method, 2))).isFalse();
5754
}
5855

56+
@Test
57+
void hasContributors() {
58+
assertThat(new CompositeUriComponentsContributor().hasContributors()).isFalse();
59+
assertThat(new CompositeUriComponentsContributor(new RequestParamMethodArgumentResolver(true)).hasContributors()).isTrue();
60+
}
61+
5962

6063
public void handleRequest(@RequestParam String p1, String p2, @RequestHeader String h) {
6164
}

0 commit comments

Comments
 (0)