From ab2a8611638c93b5e29a6846bab285b146b51c42 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 18 Aug 2021 17:33:50 +0200 Subject: [PATCH] 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 --- .../CompositeUriComponentsContributor.java | 23 +++++++++++-------- ...ompositeUriComponentsContributorTests.java | 21 +++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/support/CompositeUriComponentsContributor.java b/spring-web/src/main/java/org/springframework/web/method/support/CompositeUriComponentsContributor.java index e47ef428b3f..974acdd1e19 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/CompositeUriComponentsContributor.java +++ b/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"); * 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 - * 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 * @since 4.0 @@ -50,7 +50,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) { Collections.addAll(this.contributors, contributors); @@ -64,7 +64,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); @@ -80,7 +80,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 uriVariables) { - this.contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService); + contributeMethodArgument(parameter, value, builder, uriVariables, this.conversionService); } } diff --git a/spring-web/src/test/java/org/springframework/web/method/support/CompositeUriComponentsContributorTests.java b/spring-web/src/test/java/org/springframework/web/method/support/CompositeUriComponentsContributorTests.java index a9030b3ab79..1b698914aba 100644 --- a/spring-web/src/test/java/org/springframework/web/method/support/CompositeUriComponentsContributorTests.java +++ b/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"); * 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 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) { }