Skip to content

Honor field-level @Param in PageableSpringQueryMapEncoder - #1461

Merged
ryanjbaxter merged 2 commits into
spring-cloud:mainfrom
rajeshvasa:gh-1452-field-param-alias
Nov 23, 2026
Merged

ryanjbaxter merged 2 commits into
spring-cloud:mainfrom
rajeshvasa:gh-1452-field-param-alias

Conversation

@rajeshvasa

Copy link
Copy Markdown
Contributor

Fixes #1452

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.

This change also honors @Param declared on the backing field, including inherited fields. A @Param on the getter still takes precedence, so existing behavior for getter annotations is unchanged.

Tests

  • New PageableSpringQueryMapEncoderParamTests: field @Param, inherited field @Param, getter precedence, null values skipped, and parity with SpringFieldQueryMapEncoder
  • Full spring-cloud-openfeign-core build passes locally (./mvnw -pl spring-cloud-openfeign-core verify), including checkstyle

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) {

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.

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.

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.

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) {

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.

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.

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.

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 {

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.

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>
@rajeshvasa

Copy link
Copy Markdown
Contributor Author

Thanks @ryanjbaxter! Updated in 6836af9:

  • The encoder now builds a new map from the super.encode output instead of renaming entries in place, so the result no longer depends on property iteration order, and it throws an EncodeException when two entries end up with the same name (for example an alias that is also another property's name). Swapped aliases now keep each value on its alias.
  • Added tests for an alias colliding with another property's name, swapped aliases, and a bare @Param on a field.

On the bare @Param: I kept the fallback to the property name. 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.

@ryanjbaxter
ryanjbaxter merged commit 0b4e279 into spring-cloud:main Nov 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@SpringQueryMap does not honor @Param properties containing dash when spring-boot-starter-data-jdbc is on the classpath

3 participants