-
Notifications
You must be signed in to change notification settings - Fork 22
Query body #212
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
Closed
Closed
Query body #212
Changes from all commits
Commits
Show all changes
3 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
89 changes: 89 additions & 0 deletions
89
src/main/java/org/phoebus/channelfinder/common/SearchParameterMergerUtil.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,89 @@ | ||
| package org.phoebus.channelfinder.common; | ||
|
|
||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import org.springframework.util.MultiValueMap; | ||
|
|
||
| /** | ||
| * Utility class for merging search parameters from URL query string and JSON request body. | ||
| * | ||
| * <p>Merging strategy: | ||
| * | ||
| * <ul> | ||
| * <li>For regular search parameters (e.g., ~name, property names, ~tag): values from both URL | ||
| * and body are added as separate values under the same key in the MultiValueMap. | ||
| * <li>For control parameters (~size, ~from, ~search_after, ~track_total_hits): URL values take | ||
| * precedence over body values (body values ignored if URL value exists). | ||
| * </ul> | ||
| */ | ||
| public final class SearchParameterMergerUtil { | ||
|
|
||
| private static final String CONTROL_SIZE = "~size"; | ||
| private static final String CONTROL_FROM = "~from"; | ||
| private static final String CONTROL_SEARCH_AFTER = "~search_after"; | ||
| private static final String CONTROL_TRACK_TOTAL_HITS = "~track_total_hits"; | ||
|
|
||
| private SearchParameterMergerUtil() { | ||
| // Utility class - private constructor | ||
| } | ||
|
|
||
| private static boolean isControlParameter(String key) { | ||
| return CONTROL_SIZE.equals(key) | ||
| || CONTROL_FROM.equals(key) | ||
| || CONTROL_SEARCH_AFTER.equals(key) | ||
| || CONTROL_TRACK_TOTAL_HITS.equals(key); | ||
| } | ||
|
|
||
| /** | ||
| * Merges URL request parameters with body parameters. | ||
| * | ||
| * <p>URL parameters take precedence for control keys. For regular search parameters, body values | ||
| * are added as additional values for the same key in the MultiValueMap. | ||
| * | ||
| * @param urlParams URL query parameters (may be null or empty) | ||
| * @param bodyParams JSON request body as a map (may be null or empty) | ||
| * @return merged parameters as a MultiValueMap | ||
| */ | ||
| public static MultiValueMap<String, String> mergeParameters( | ||
| MultiValueMap<String, String> urlParams, Map<String, String> bodyParams) { | ||
| MultiValueMap<String, String> merged = new LinkedMultiValueMap<>(); | ||
|
|
||
| // Add URL parameters first | ||
| if (urlParams != null && !urlParams.isEmpty()) { | ||
| for (Map.Entry<String, List<String>> entry : urlParams.entrySet()) { | ||
| merged.put(entry.getKey(), new LinkedList<>(entry.getValue())); | ||
| } | ||
| } | ||
|
|
||
| // Merge body parameters if present | ||
| if (bodyParams != null && !bodyParams.isEmpty()) { | ||
| mergeBodyParams(merged, bodyParams); | ||
| } | ||
|
|
||
| return merged; | ||
| } | ||
|
|
||
| private static void mergeBodyParams( | ||
| MultiValueMap<String, String> merged, Map<String, String> bodyParams) { | ||
| for (Map.Entry<String, String> entry : bodyParams.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String bodyValue = entry.getValue(); | ||
|
|
||
| if (bodyValue == null || bodyValue.trim().isEmpty()) { | ||
| continue; // Skip empty body values | ||
| } | ||
|
|
||
| if (isControlParameter(key)) { | ||
| // For control parameters, URL takes precedence | ||
| if (!merged.containsKey(key)) { | ||
| merged.set(key, bodyValue); | ||
| } | ||
| } else { | ||
| // For regular search parameters, add the body value to the same key. | ||
| merged.add(key, bodyValue); | ||
| } | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.phoebus.channelfinder.entity.Channel; | ||
| import org.phoebus.channelfinder.entity.SearchResult; | ||
| import org.springframework.util.MultiValueMap; | ||
|
|
@@ -27,7 +28,10 @@ public interface IChannel { | |
| @Operation( | ||
| summary = "Query channels", | ||
| description = | ||
| "Query a collection of Channel instances based on tags, property values, and channel names.", | ||
| "Query a collection of Channel instances based on tags, property values, and channel names. " | ||
| + "Search parameters can be provided via URL query string or JSON request body, or both. " | ||
| + "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). " | ||
| + "Regular search parameters from URL and body are combined as separate values in the query.", | ||
| operationId = "queryChannels", | ||
| tags = {"Channel"}) | ||
| @ApiResponses( | ||
|
|
@@ -49,12 +53,25 @@ public interface IChannel { | |
| @GetMapping | ||
| List<Channel> query( | ||
| @Parameter(description = SEARCH_PARAM_DESCRIPTION) @RequestParam | ||
| MultiValueMap<String, String> allRequestParams); | ||
| MultiValueMap<String, String> allRequestParams, | ||
| @Parameter( | ||
| description = | ||
| "Optional JSON request body containing search parameters. Used to bypass URL length limitations.") | ||
| @RequestBody(required = false) | ||
| Map<String, String> searchParamsBody); | ||
|
|
||
| // Backward-compatible overload when no request body is provided. | ||
| default List<Channel> query(MultiValueMap<String, String> allRequestParams) { | ||
| return query(allRequestParams, null); | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "Combined query for channels", | ||
| description = | ||
| "Query for a collection of Channel instances and get a count and the first 10k hits.", | ||
| "Query for a collection of Channel instances and get a count and the first 10k hits. " | ||
| + "Search parameters can be provided via URL query string or JSON request body, or both. " | ||
| + "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). " | ||
| + "Regular search parameters from URL and body are combined as separate values in the query.", | ||
| operationId = "combinedQueryChannels", | ||
| tags = {"Channel"}) | ||
| @ApiResponses( | ||
|
|
@@ -77,11 +94,25 @@ List<Channel> query( | |
| @GetMapping("/combined") | ||
| SearchResult combinedQuery( | ||
| @Parameter(description = SEARCH_PARAM_DESCRIPTION) @RequestParam | ||
| MultiValueMap<String, String> allRequestParams); | ||
| MultiValueMap<String, String> allRequestParams, | ||
| @Parameter( | ||
| description = | ||
| "Optional JSON request body containing search parameters. Used to bypass URL length limitations.") | ||
| @RequestBody(required = false) | ||
| Map<String, String> searchParamsBody); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like a generic Map<String, String> better to use an actual record record QueryOptions(String name, Map<String, String> properties, List tags) {} You can see it makes the swagger much much nicer. |
||
|
|
||
| // Backward-compatible overload when no request body is provided. | ||
| default SearchResult combinedQuery(MultiValueMap<String, String> allRequestParams) { | ||
| return combinedQuery(allRequestParams, null); | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "Count channels matching query", | ||
| description = "Get the number of channels matching the given query parameters.", | ||
| description = | ||
| "Get the number of channels matching the given query parameters. " | ||
| + "Search parameters can be provided via URL query string or JSON request body, or both. " | ||
| + "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). " | ||
| + "Regular search parameters from URL and body are combined as separate values in the query.", | ||
| operationId = "countChannels", | ||
| tags = {"Channel"}) | ||
| @ApiResponses( | ||
|
|
@@ -98,7 +129,17 @@ SearchResult combinedQuery( | |
| @GetMapping("/count") | ||
| long queryCount( | ||
| @Parameter(description = SEARCH_PARAM_DESCRIPTION) @RequestParam | ||
| MultiValueMap<String, String> allRequestParams); | ||
| MultiValueMap<String, String> allRequestParams, | ||
| @Parameter( | ||
| description = | ||
| "Optional JSON request body containing search parameters. Used to bypass URL length limitations.") | ||
| @RequestBody(required = false) | ||
| Map<String, String> searchParamsBody); | ||
|
|
||
| // Backward-compatible overload when no request body is provided. | ||
| default long queryCount(MultiValueMap<String, String> allRequestParams) { | ||
| return queryCount(allRequestParams, null); | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "Get channel by name", | ||
|
|
||
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
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.
I think you can just do two methods rather than mutually exclusive inputs.
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.
well this is not part of the current change so I would address this as a separate issue