Skip to content

Honor field-level @Param in PageableSpringQueryMapEncoder - #1461

Open
rajeshvasa wants to merge 1 commit into
spring-cloud:mainfrom
rajeshvasa:gh-1452-field-param-alias
Open

rajeshvasa wants to merge 1 commit into
spring-cloud:mainfrom
rajeshvasa:gh-1452-field-param-alias

Conversation

@rajeshvasa

Copy link
Copy Markdown

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.

* 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.

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

This branch has not been deployed

No deployments
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