Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -101,10 +110,64 @@ else if (object instanceof HttpHeaders httpHeaders) {
return queryMap;
}
else {
return super.encode(object);
Map<String, Object> 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.
* <p>
* 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<String, Object> applyFieldParamAliases(Class<?> type, Map<String, Object> encoded) {
Map<String, String> aliases = fieldParamAliases(type);
Map<String, Object> queryMap = new HashMap<>();
for (Map.Entry<String, Object> 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<String, String> fieldParamAliases(Class<?> type) {
Map<String, String> 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<String, Object> queryMap, Sort sort) {
List<String> sortQueries = new ArrayList<>();
for (Sort.Order order : sort) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encodesSameNamesAsSpringFieldQueryMapEncoder only covers one simple bean. Please add cases for:

  • an alias that collides with another property's name
  • a bare @PARAM on a field

@rajeshvasa rajeshvasa Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both cases in 6836af9: an alias colliding with another property's name (expects an EncodeException), and a bare @Param on a field. Also added a swapped-aliases case.


private final PageableSpringQueryMapEncoder encoder = new PageableSpringQueryMapEncoder();

@Test
void honorsParamDeclaredOnField() {
Map<String, Object> 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<String, Object> 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<String, Object> queryMap = encoder.encode(new GetterParamCriteria("value1"));

assertThat(queryMap).containsOnly(Map.entry("getter-name", "value1"));
}

@Test
void skipsNullValues() {
Map<String, Object> 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<String, Object> queryMap = encoder.encode(new SwappedAliasCriteria("valueA", "valueB"));

assertThat(queryMap).containsOnly(Map.entry("b", "valueA"), Map.entry("a", "valueB"));
}

@Test
void bareParamOnFieldKeepsPropertyName() {
Map<String, Object> 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;
}

}

}
Loading