Repository navigation
Send the nested objects of @SpringQueryMap as nested query parameters #1448
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 6 commits into
spring-cloud:main
from
sharanggupta:gh-442-nested-query-map
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1b13146
Send the nested objects of @SpringQueryMap as nested query parameters
sharanggupta 57b3d87
Reject query map values nested in themselves
sharanggupta b72671a
Keep JDK values and mixed collections as is
sharanggupta 8eadbe9
Keep nested objects without properties as is
sharanggupta cf6a86a
Configure nested query maps per client
sharanggupta d003160
Log skipped objects and iterate the elements
sharanggupta 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
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
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
193 changes: 193 additions & 0 deletions
193
...rc/main/java/org/springframework/cloud/openfeign/support/NestedObjectQueryMapEncoder.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,193 @@ | ||
| /* | ||
| * 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.lang.reflect.Method; | ||
| import java.lang.reflect.RecordComponent; | ||
| import java.util.Collections; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import feign.QueryMapEncoder; | ||
| import feign.codec.EncodeException; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
|
|
||
| import org.springframework.beans.BeanUtils; | ||
| import org.springframework.util.Assert; | ||
| import org.springframework.util.CollectionUtils; | ||
| import org.springframework.util.ReflectionUtils; | ||
|
|
||
| /** | ||
| * A {@link QueryMapEncoder} that sends the nested objects of a query map parameter as | ||
| * nested query parameters, with the property path notation that Spring MVC binds. | ||
| * <p> | ||
| * The query map is produced by the delegate encoder. Then, each nested object, that is a | ||
| * value that is neither a simple value (see {@link BeanUtils#isSimpleValueType(Class)}) | ||
| * nor of a {@code java.*} or {@code javax.*} type, is encoded with the delegate too, and | ||
| * its entries are added with the name of the value as a prefix: | ||
| * <ul> | ||
| * <li>a nested object: {@code range.from=1&range.to=2}</li> | ||
| * <li>the objects of a collection or an array: {@code filters[0].name=status}</li> | ||
| * <li>the entries of a map: {@code attributes[color]=red}</li> | ||
| * </ul> | ||
| * Simple values and the values of other JDK types, such as an {@code Optional} or a | ||
| * {@code Duration}, are kept as is. A nested object whose properties are all {@code null} | ||
| * is skipped, like a {@code null} value. The components of a record are used when the | ||
| * delegate finds no properties in it, as a {@code BeanQueryMapEncoder} does, such as the | ||
| * one for Spring Data. A collection or an array is encoded with the index of its elements | ||
| * only when none of them is such a value, so a collection that has some, such as a | ||
| * {@code List<String>}, is still sent as repeated query parameters. A map key with a | ||
| * square bracket, and a value that is nested in itself, directly or through other values, | ||
| * are rejected with an {@link EncodeException}. | ||
| * | ||
| * @author Sharang Gupta | ||
| * @since 5.1.0 | ||
| * @see org.springframework.cloud.openfeign.FeignClientProperties.FeignClientConfiguration#getNestedQueryMapEnabled() | ||
| */ | ||
| public class NestedObjectQueryMapEncoder implements QueryMapEncoder { | ||
|
|
||
| private static final Log LOG = LogFactory.getLog(NestedObjectQueryMapEncoder.class); | ||
|
|
||
| private final QueryMapEncoder delegate; | ||
|
|
||
| public NestedObjectQueryMapEncoder(QueryMapEncoder delegate) { | ||
| Assert.notNull(delegate, "delegate must not be null"); | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> encode(Object object) throws EncodeException { | ||
| if (object == null) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| Map<String, Object> queryMap = new LinkedHashMap<>(); | ||
| Set<Object> path = Collections.newSetFromMap(new IdentityHashMap<>()); | ||
| path.add(object); | ||
| addAll(queryMap, "", delegate.encode(object), path); | ||
| return queryMap; | ||
| } | ||
|
|
||
| private void addAll(Map<String, Object> queryMap, String prefix, Map<String, Object> values, Set<Object> path) { | ||
| values.forEach((name, value) -> add(queryMap, prefix + name, value, path)); | ||
| } | ||
|
|
||
| private void add(Map<String, Object> queryMap, String name, Object value, Set<Object> path) { | ||
| if (value == null) { | ||
| return; | ||
| } | ||
| if (isPlainValue(value)) { | ||
| queryMap.put(name, value); | ||
| return; | ||
| } | ||
| // Track the maps, collections, arrays and objects on the current path, so that | ||
| // a value nested in itself is rejected instead of recursing forever | ||
| if (!path.add(value)) { | ||
| throw new EncodeException("Cannot encode the query map: an object of type " + value.getClass().getName() | ||
| + " is nested in itself under '" + name + "'"); | ||
| } | ||
| addNested(queryMap, name, value, path); | ||
| path.remove(value); | ||
| } | ||
|
|
||
| private void addNested(Map<String, Object> queryMap, String name, Object value, Set<Object> path) { | ||
| if (value instanceof Map<?, ?> map) { | ||
| map.forEach((key, entryValue) -> add(queryMap, name + "[" + mapKey(name, key) + "]", entryValue, path)); | ||
| } | ||
| else if (value instanceof Iterable<?> || value.getClass().isArray()) { | ||
| addElements(queryMap, name, value, path); | ||
| } | ||
| else { | ||
| Map<String, Object> properties = delegate.encode(value); | ||
| if (properties.isEmpty() && value instanceof Record record) { | ||
| // A BeanQueryMapEncoder finds no properties in a record | ||
| properties = componentsOf(record); | ||
| } | ||
| if (properties.isEmpty()) { | ||
| // An object whose properties are all null adds nothing, like a null value | ||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Skipping the nested object '" + name + "' of type " + value.getClass().getName() | ||
| + " from the query map, as none of its properties has a value"); | ||
| } | ||
| return; | ||
| } | ||
| addAll(queryMap, name + ".", properties, path); | ||
| } | ||
| } | ||
|
|
||
| private void addElements(Map<String, Object> queryMap, String name, Object value, Set<Object> path) { | ||
| Iterable<?> elements = (value instanceof Iterable<?> iterable) ? iterable : CollectionUtils.arrayToList(value); | ||
| if (!areAllNested(elements)) { | ||
| // A plain value has no property path to index, so keep the elements as Feign | ||
| // sends them: an Iterable as repeated query parameters | ||
| queryMap.put(name, elements); | ||
| return; | ||
| } | ||
| int index = 0; | ||
| for (Object element : elements) { | ||
| add(queryMap, name + "[" + index++ + "]", element, path); | ||
| } | ||
| } | ||
|
|
||
| private static String mapKey(String name, Object key) { | ||
| String mapKey = String.valueOf(key); | ||
| if (mapKey.indexOf('[') >= 0 || mapKey.indexOf(']') >= 0) { | ||
| throw new EncodeException("Cannot encode the query map: the key '" + mapKey + "' of the map under '" + name | ||
| + "' has a square bracket, which the property path of its entry cannot hold"); | ||
| } | ||
| return mapKey; | ||
| } | ||
|
|
||
| private static boolean areAllNested(Iterable<?> elements) { | ||
| boolean anyPresent = false; | ||
| for (Object element : elements) { | ||
| if (element == null) { | ||
| continue; | ||
| } | ||
| if (isPlainValue(element)) { | ||
| return false; | ||
| } | ||
| anyPresent = true; | ||
| } | ||
| return anyPresent; | ||
| } | ||
|
|
||
| private static Map<String, Object> componentsOf(Record record) { | ||
| Map<String, Object> components = new LinkedHashMap<>(); | ||
| for (RecordComponent component : record.getClass().getRecordComponents()) { | ||
| Method accessor = component.getAccessor(); | ||
| ReflectionUtils.makeAccessible(accessor); | ||
| Object componentValue = ReflectionUtils.invokeMethod(accessor, record); | ||
| if (componentValue != null) { | ||
| components.put(component.getName(), componentValue); | ||
| } | ||
| } | ||
| return components; | ||
| } | ||
|
|
||
| private static boolean isPlainValue(Object value) { | ||
| if (value instanceof Map<?, ?> || value instanceof Iterable<?> || value.getClass().isArray()) { | ||
| return false; | ||
| } | ||
| String typeName = value.getClass().getName(); | ||
| return BeanUtils.isSimpleValueType(value.getClass()) || typeName.startsWith("java.") | ||
| || typeName.startsWith("javax."); | ||
| } | ||
|
|
||
| } | ||
37 changes: 37 additions & 0 deletions
37
...e/src/main/java/org/springframework/cloud/openfeign/support/NestedQueryMapCapability.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,37 @@ | ||
| /* | ||
| * 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 feign.Capability; | ||
| import feign.QueryMapEncoder; | ||
|
|
||
| /** | ||
| * A {@link Capability} that decorates the {@link QueryMapEncoder} of a Feign client, | ||
| * whichever it is, with the {@link NestedObjectQueryMapEncoder}. | ||
| * | ||
| * @author Sharang Gupta | ||
| * @since 5.1.0 | ||
| * @see org.springframework.cloud.openfeign.FeignClientProperties.FeignClientConfiguration#getNestedQueryMapEnabled() | ||
| */ | ||
| public class NestedQueryMapCapability implements Capability { | ||
|
|
||
| @Override | ||
| public QueryMapEncoder enrich(QueryMapEncoder queryMapEncoder) { | ||
| return new NestedObjectQueryMapEncoder(queryMapEncoder); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
Maybe we should log something if an object with null values is dropped?
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.
Good idea. In d003160, the encoder logs at debug level when it skips a nested object because none of its properties has a value, with the name of the parameter and the type of the object.