diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java index 01d1492bc..6cf15f53d 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoder.java @@ -16,16 +16,24 @@ package org.springframework.cloud.openfeign.support; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import feign.Param; +import feign.codec.EncodeException; import feign.querymap.BeanQueryMapEncoder; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.http.HttpHeaders; +import org.springframework.util.ReflectionUtils; /** * Provides support for encoding Pageable annotated as @@ -40,6 +48,7 @@ * @author Hyeonmin Park * @author Yanming Zhou * @author Gokalp Kuscu + * @author Rajesh Vasa * @since 2.2.8 */ public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder { @@ -101,10 +110,64 @@ else if (object instanceof HttpHeaders httpHeaders) { return queryMap; } else { - return super.encode(object); + Map queryMap = super.encode(object); + return (object != null) ? applyFieldParamAliases(object.getClass(), queryMap) : queryMap; } } + /** + * {@link BeanQueryMapEncoder} only honors {@link Param} when it is declared on a + * getter. Since {@link SpringFieldQueryMapEncoder}, the default when Spring Data is + * not on the classpath, honors {@link Param} declared on a field, also honor it here + * so that the query parameter names do not depend on whether Spring Data is present. + * A {@link Param} declared on the getter takes precedence, and a {@link Param} with + * an empty value keeps the property name. + *

+ * The result is built as a new map from the encoded entries, so it does not depend on + * the property iteration order. If two entries end up with the same name, for example + * an alias that is also the name of another property, an {@link EncodeException} is + * thrown instead of one value silently replacing the other. + */ + private static Map applyFieldParamAliases(Class type, Map encoded) { + Map aliases = fieldParamAliases(type); + Map queryMap = new HashMap<>(); + for (Map.Entry entry : encoded.entrySet()) { + String name = aliases.getOrDefault(entry.getKey(), entry.getKey()); + if (queryMap.containsKey(name)) { + throw new EncodeException( + "Duplicate query parameter name '" + name + "' when encoding " + type.getName()); + } + queryMap.put(name, entry.getValue()); + } + return queryMap; + } + + /** + * Returns the {@link Param} names declared on the fields of the given type, keyed by + * property name, for the properties whose getter has no {@link Param}. + */ + private static Map fieldParamAliases(Class type) { + Map aliases = new HashMap<>(); + try { + for (PropertyDescriptor descriptor : Introspector.getBeanInfo(type).getPropertyDescriptors()) { + Method readMethod = descriptor.getReadMethod(); + if (readMethod == null || readMethod.isAnnotationPresent(Param.class)) { + continue; + } + String propertyName = descriptor.getName(); + Field field = ReflectionUtils.findField(type, propertyName); + Param alias = (field != null) ? field.getAnnotation(Param.class) : null; + if (alias != null && !alias.value().isEmpty()) { + aliases.put(propertyName, alias.value()); + } + } + } + catch (IntrospectionException ex) { + throw new EncodeException("Failure encoding object into query map", ex); + } + return aliases; + } + private void applySort(Map queryMap, Sort sort) { List sortQueries = new ArrayList<>(); for (Sort.Order order : sort) { diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderParamTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderParamTests.java new file mode 100644 index 000000000..8935beb1b --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderParamTests.java @@ -0,0 +1,211 @@ +/* + * Copyright 2013-present 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 + * + * https://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.cloud.openfeign.support; + +import java.util.Map; + +import feign.Param; +import feign.codec.EncodeException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests that {@link PageableSpringQueryMapEncoder} honors {@link Param} on fields the + * same way {@link SpringFieldQueryMapEncoder} does. + * + * @author Rajesh Vasa + */ +class PageableSpringQueryMapEncoderParamTests { + + private final PageableSpringQueryMapEncoder encoder = new PageableSpringQueryMapEncoder(); + + @Test + void honorsParamDeclaredOnField() { + Map queryMap = encoder.encode(new FieldParamCriteria("value1", "value2")); + + assertThat(queryMap).containsOnly(Map.entry("dummy-param1", "value1"), Map.entry("plainParam", "value2")); + } + + @Test + void encodesSameNamesAsSpringFieldQueryMapEncoder() { + FieldParamCriteria criteria = new FieldParamCriteria("value1", "value2"); + + assertThat(encoder.encode(criteria)).isEqualTo(new SpringFieldQueryMapEncoder().encode(criteria)); + } + + @Test + void honorsParamDeclaredOnInheritedField() { + Map queryMap = encoder.encode(new ChildCriteria("value1", "value2", "value3")); + + assertThat(queryMap).containsOnly(Map.entry("dummy-param1", "value1"), Map.entry("plainParam", "value2"), + Map.entry("child-param", "value3")); + } + + @Test + void paramOnGetterTakesPrecedenceOverParamOnField() { + Map queryMap = encoder.encode(new GetterParamCriteria("value1")); + + assertThat(queryMap).containsOnly(Map.entry("getter-name", "value1")); + } + + @Test + void skipsNullValues() { + Map queryMap = encoder.encode(new FieldParamCriteria(null, "value2")); + + assertThat(queryMap).containsOnly(Map.entry("plainParam", "value2")); + } + + @Test + void failsWhenAliasCollidesWithAnotherPropertyName() { + assertThatExceptionOfType(EncodeException.class) + .isThrownBy(() -> encoder.encode(new CollidingAliasCriteria("valueA", "valueB"))) + .withMessageContaining("'b'"); + } + + @Test + void keepsEachValueOnItsAliasWhenAliasesAreSwapped() { + Map queryMap = encoder.encode(new SwappedAliasCriteria("valueA", "valueB")); + + assertThat(queryMap).containsOnly(Map.entry("b", "valueA"), Map.entry("a", "valueB")); + } + + @Test + void bareParamOnFieldKeepsPropertyName() { + Map queryMap = encoder.encode(new BareParamCriteria("value1")); + + assertThat(queryMap).containsOnly(Map.entry("value", "value1")); + } + + // BeanQueryMapEncoder invokes getters reflectively without making them accessible, + // so query map beans must be public, as they are in real applications. + public static class FieldParamCriteria { + + @Param("dummy-param1") + private String dummyParam1; + + private String plainParam; + + FieldParamCriteria(String dummyParam1, String plainParam) { + this.dummyParam1 = dummyParam1; + this.plainParam = plainParam; + } + + public String getDummyParam1() { + return dummyParam1; + } + + public String getPlainParam() { + return plainParam; + } + + } + + public static class ChildCriteria extends FieldParamCriteria { + + @Param("child-param") + private String childParam; + + ChildCriteria(String dummyParam1, String plainParam, String childParam) { + super(dummyParam1, plainParam); + this.childParam = childParam; + } + + public String getChildParam() { + return childParam; + } + + } + + public static class GetterParamCriteria { + + @Param("field-name") + private String value; + + GetterParamCriteria(String value) { + this.value = value; + } + + @Param("getter-name") + public String getValue() { + return value; + } + + } + + public static class CollidingAliasCriteria { + + @Param("b") + private String a; + + private String b; + + CollidingAliasCriteria(String a, String b) { + this.a = a; + this.b = b; + } + + public String getA() { + return a; + } + + public String getB() { + return b; + } + + } + + public static class SwappedAliasCriteria { + + @Param("b") + private String a; + + @Param("a") + private String b; + + SwappedAliasCriteria(String a, String b) { + this.a = a; + this.b = b; + } + + public String getA() { + return a; + } + + public String getB() { + return b; + } + + } + + public static class BareParamCriteria { + + @Param + private String value; + + BareParamCriteria(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + } + +}