-
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
HttpServiceProxyFactory
should omit optional @RequestParam
if converted from null
to empty string
#33794
Comments
From what I see, the |
I can definetely think of usecases where I'd want to explicitly pass an empty string as parameter. A better approach would be to just not call the conversion service for null values. It is already getting skipped if the value is a string. |
I fully agree with you about passing empty strings as query parameter values. With the following test I was able to reproduce the case when passing null to a not required converted request param in @GetExchange
void executeDateNotRequired(@Nullable @TestValue(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate value);
@Test
void dateTestNotRequiredNull() {
this.service.executeDateNotRequired(null);
assertTestValue("value");
} And then skip conversion when value null in if (value != null && this.conversionService != null && !(value instanceof String)) {
//
}
if (value == null) {
Assert.isTrue(!required, () -> "Missing " + valueLabel + " value '" + name + "'");
return;
} If you agree with this I can submit a PR. |
I don't think we need to prevent We have similar post conversion checks on the server side, but in the opposite direction where request values are converted to objects. |
HttpServiceProxyFactory
should omit optional @RequestParam
if converted from null
to empty string
Spring version: 6.1.14
When using HttpServiceProxyFactory, we can define request parameters as optional, so that a value of
null
leads to the parameter being omitted completely:Here, calling
testString(null)
will result in a request with url/test
. The query parameter is omitted completely.However, If the value is not a string and run through conversion, this does not work.
In this example:
Calling
testDate(null)
makes a request with/test?param=
, setting the query parameter to an empty string.This behaviour is inconsistent and counterintuitive. Also, a server expecting a date value in the parameter is likely to not accept an empty string.
You can work around this by customizing the conversion service or the argument resolvers in the HttpServiceProxyFactory, but there really should be a consistent solution in spring itself.
A full test case for reference:
The text was updated successfully, but these errors were encountered: