Browse Source

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
pull/27340/head
Sam Brannen 4 years ago
parent
commit
ab2a861163
  1. 23
      spring-web/src/main/java/org/springframework/web/method/support/CompositeUriComponentsContributor.java
  2. 21
      spring-web/src/test/java/org/springframework/web/method/support/CompositeUriComponentsContributorTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,8 +30,8 @@ import org.springframework.web.util.UriComponentsBuilder;
/** /**
* A {@link UriComponentsContributor} containing a list of other contributors * A {@link UriComponentsContributor} containing a list of other contributors
* to delegate and also encapsulating a specific {@link ConversionService} to * to delegate to and also encapsulating a specific {@link ConversionService} to
* use for formatting method argument values to Strings. * use for formatting method argument values as Strings.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
@ -50,7 +50,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter} * {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
* and provide that to this constructor. * and provide that to this constructor.
* @param contributors a collection of {@link UriComponentsContributor} * @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}. * or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
*/ */
public CompositeUriComponentsContributor(UriComponentsContributor... contributors) { public CompositeUriComponentsContributor(UriComponentsContributor... contributors) {
Collections.addAll(this.contributors, contributors); Collections.addAll(this.contributors, contributors);
@ -64,7 +64,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter} * {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
* and provide that to this constructor. * and provide that to this constructor.
* @param contributors a collection of {@link UriComponentsContributor} * @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}. * or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
*/ */
public CompositeUriComponentsContributor(Collection<?> contributors) { public CompositeUriComponentsContributor(Collection<?> contributors) {
this(contributors, null); this(contributors, null);
@ -80,7 +80,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* {@link org.springframework.format.support.DefaultFormattingConversionService} * {@link org.springframework.format.support.DefaultFormattingConversionService}
* will be used by default. * will be used by default.
* @param contributors a collection of {@link UriComponentsContributor} * @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 * @param cs a ConversionService to use when method argument values
* need to be formatted as Strings before being added to the URI * 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()); 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() { public boolean hasContributors() {
return this.contributors.isEmpty(); return !this.contributors.isEmpty();
} }
@Override @Override
@ -139,7 +144,7 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder, public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder,
Map<String, Object> uriVariables) { Map<String, Object> uriVariables) {
this.contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService); contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService);
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Unit tests for * Unit tests for {@link CompositeUriComponentsContributor}.
* {@link org.springframework.web.method.support.CompositeUriComponentsContributor}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
*/ */
public class CompositeUriComponentsContributorTests { class CompositeUriComponentsContributorTests {
@Test @Test
public void supportsParameter() { void supportsParameter() {
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>(); List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
resolvers.add(new RequestParamMethodArgumentResolver(false)); resolvers.add(new RequestParamMethodArgumentResolver(false));
resolvers.add(new RequestHeaderMethodArgumentResolver(null)); resolvers.add(new RequestHeaderMethodArgumentResolver(null));
resolvers.add(new RequestParamMethodArgumentResolver(true)); resolvers.add(new RequestParamMethodArgumentResolver(true));
Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);
CompositeUriComponentsContributor contributor = new CompositeUriComponentsContributor(resolvers); 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, 0))).isTrue();
assertThat(contributor.supportsParameter(new MethodParameter(method, 1))).isTrue(); assertThat(contributor.supportsParameter(new MethodParameter(method, 1))).isTrue();
assertThat(contributor.supportsParameter(new MethodParameter(method, 2))).isFalse(); 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) { public void handleRequest(@RequestParam String p1, String p2, @RequestHeader String h) {
} }

Loading…
Cancel
Save