Repository navigation
Honor field-level @Param in PageableSpringQueryMapEncoder #1461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ryanjbaxter
merged 2 commits into
spring-cloud:main
from
rajeshvasa:gh-1452-field-param-alias
Nov 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
.../org/springframework/cloud/openfeign/support/PageableSpringQueryMapEncoderParamTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
@Paramon a field. Also added a swapped-aliases case.