-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
UriBuilder - Overload queryParam to accept Optional and add only if value is present #25925
UriBuilder - Overload queryParam to accept Optional and add only if value is present #25925
Conversation
@robinroos Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@robinroos Thank you for signing the Contributor License Agreement! |
It would be readlly good to see this included in the up-coming Spring Boot 5.4.0 release. I am currently testing with Spring Boot 5.4.0-M3. |
As per the Javadoc of uriBuilder#queryParam it is possible to pass no values at all which results in a query parameter without a value. To be consistent a We also need to consider what should happen if a value passed into the existing If we wanted to have another method that does not add a query param at all if there is no value, that would have to be named differently in order to differentiate the semantics. But I am wondering if the existing method behavior, if extended to support |
Thanks for your comment.
Whereas my PR adds an overloading, my purposes would be equally well-served by effecting the change under the existing API. What is important for me is that the caller (my code) should not have to interrogate the Optional for isPresent(), and that the query param should NOT be added in the "absent" case. Would you like me to craft an alternative PR which does not extend the API at all but which works within the existing methods? |
In addition to the above, my personal feeling is that it would acceptable for the documentation to reflect that the parameter name will NOT be added to the url if the argument passed is Optional.empty(). Remember it was not present on the inbound request, which is why the query parameter was mapped to Optional.empty(). The delegation to a downstream service would be different in perhaps subtle ways if the parameter name was included albeit with no value. |
The existing queryParam methods have established behavior. Documenting that it works one way for
I do understand that your scenario does not fit this existing behavior. For that we can add a new method that is named differently and free to behave in a different way, e.g. |
I have raised an alternative (actually better) implementation as #25950. |
@rstoyanchev , I'm just now catching up with the most recent comment. I would be interested to hear your thoughts after you have read the PR description for #25950 - specifically about how Optional is currently treated. |
Once you have read the other PR, if you definitely want to proceed with a new method name then I will happily implement that as a third PR. May I suggest the method name be "optionalQueryParam" ? |
@robinroos I'm not sure what else there is to say. The additional PR only adds what I've already said we won't do. |
No problem. I'll implement an explicit optionalQueryParam method. |
This addresses a use-case in which a REST endpoint (A) has received an optional query param (as type Optional) and is delegating through WebClient to another service (B), probably with a similar uri structure.
Rather than have service (A) interrogate the parameter and branch its execution flow, it can merely include it in the chain of .queryParam() invocations on UriBuilder. The parameter will be added in the downstream call to service (B) only if a value is present.
Without this change:
With this change (queryParam for "context" is passed an Optional):