Repository navigation
Honor field-level @Param in PageableSpringQueryMapEncoder - #1461
rajeshvasa wants to merge 1 commit into
Conversation
When Spring Data is on the classpath, PageableSpringQueryMapEncoder is the default QueryMapEncoder. It extends Feign's BeanQueryMapEncoder, which only honors @PARAM on getters, so a @PARAM declared on a field of a @SpringQueryMap object was silently ignored and the Java property name was sent instead. Without Spring Data, SpringFieldQueryMapEncoder is used and honors the field-level @PARAM, so the query parameter names changed just by adding a Spring Data starter. Also honor @PARAM declared on the backing field (including inherited fields). A @PARAM on the getter still takes precedence. Fixes spring-cloudgh-1452 Signed-off-by: Rajesh Vasa <guptha.rh@gmail.com>
| * so that the query parameter names do not depend on whether Spring Data is present. | ||
| * A {@link Param} declared on the getter takes precedence. | ||
| */ | ||
| private static void applyFieldParamAliases(Class<?> type, Map<String, Object> queryMap) { |
There was a problem hiding this comment.
queryMap.put(alias.value(), queryMap.remove(propertyName)) changes the map in place, so the result depends on property iteration order, and it silently overwrites an existing entry under the alias name.
For example, suppose property a has a field @PARAM("b") and the bean also has a plain property b. Processing a overwrites b's value. A swap (a aliased to b and b aliased to a) can end up with both values on the wrong keys.
FieldQueryMapEncoder doesn't behave this way. It builds the map with Collectors.toMap, so a duplicate name throws IllegalStateException. Could you build a fresh result map from the super.encode output and fail with an EncodeException on a duplicate key? That also removes the order dependence.
| * so that the query parameter names do not depend on whether Spring Data is present. | ||
| * A {@link Param} declared on the getter takes precedence. | ||
| */ | ||
| private static void applyFieldParamAliases(Class<?> type, Map<String, Object> queryMap) { |
There was a problem hiding this comment.
Feign's FieldQueryMapEncoder uses alias.value() as-is, so a bare @PARAM on a field gives an empty-string key. This PR falls back to the property name. I think that is the saner behavior, but it means the two encoders don't produce identical names for this input.
| * | ||
| * @author Rajesh Vasa | ||
| */ | ||
| class PageableSpringQueryMapEncoderParamTests { |
There was a problem hiding this comment.
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
Fixes #1452
When Spring Data is on the classpath,
PageableSpringQueryMapEncoderis the defaultQueryMapEncoder. It extends Feign'sBeanQueryMapEncoder, which only honors@Paramon getters, so a@Paramdeclared on a field of a@SpringQueryMapobject was silently ignored and the Java property name was sent instead. Without Spring Data,SpringFieldQueryMapEncoderis used and honors the field-level@Param, so the query parameter names changed just by adding a Spring Data starter.This change also honors
@Paramdeclared on the backing field, including inherited fields. A@Paramon the getter still takes precedence, so existing behavior for getter annotations is unchanged.Tests
PageableSpringQueryMapEncoderParamTests: field@Param, inherited field@Param, getter precedence, null values skipped, and parity withSpringFieldQueryMapEncoderspring-cloud-openfeign-corebuild passes locally (./mvnw -pl spring-cloud-openfeign-core verify), including checkstyle