Repository navigation
Honor field-level @Param in PageableSpringQueryMapEncoder - #1461
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.
There was a problem hiding this comment.
Done in 6836af9: it now builds a new map from the super.encode output and throws an EncodeException on a duplicate name, so the result no longer depends on iteration order and swapped aliases keep each value on its alias.
| * 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.
There was a problem hiding this comment.
Kept the property-name fallback. Feign 13.18 made the same change in FieldQueryMapEncoder (OpenFeign/feign#3658), so once Feign is upgraded both encoders produce the same 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
There was a problem hiding this comment.
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.
Address review feedback on spring-cloudgh-1452: instead of renaming entries in place, build the result from the encoded entries so it does not depend on property iteration order, and throw an EncodeException when two entries end up with the same name. A bare @PARAM on a field keeps the property name. Add tests for a colliding alias, swapped aliases and a bare @PARAM. Signed-off-by: Rajesh Vasa <guptha.rh@gmail.com>
|
Thanks @ryanjbaxter! Updated in 6836af9:
On the bare |
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