-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Minimal API : Converting empty string to Nullable (ex: int?) with [FromForm] binding #55202
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
Comments
I've also experienced this issue. I "fixed" it by updating my frontend to delete all empty fields before sending it to the backend. |
The form binding behavior that minimal APIs uses is shared with Blazor. I'm open to changing this behavior to be more inline with how empty strings are handled elsewhere in minimal APIs (and in forms in MVC). @javiercn Any objections to modifying the form binding behavior for empty strings here? |
@captainsafia I have this same issue with
|
@captainsafia I'm also experiencing issues with [AsParameters] and all Nullable primitive types. This is causing significant problems. As a workaround, I've implemented an Axios interceptor to remove empty, null, and undefined values, but some of our customers are unhappy with this solution. I've tried other approaches, including custom JSON converters, without success. Is there a plan to improve Minimal API model binding to match the behavior of MVC Model Binder? If anyone's interested, here's my Axios interceptor: const $http = axios.create({
baseURL: '/your-endpoint'
});
function cleanObject(obj) {
for (let propName in obj) {
if (Object.prototype.hasOwnProperty.call(obj, propName)) {
if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
delete obj[propName];
}
}
}
return obj;
}
$http.interceptors.request.use((req) => {
if (req.data) req.data = cleanObject(req.data);
if (req.params) req.params = cleanObject(req.params);
return req;
}); |
Please do it :) |
This is still a big issue in Blazor SSR in the current .NET 9 preview: When a [SupplyParameterFromForm] model contains a nullable int (or double, etc.), an empty input value results in a form-binding validation error (and not in a null value without an error, as you would expect). |
Unfortunatelly it seems few people are using minimal api and form binding as this issue is not getting traction. I just found out this behavior also impacts nullable enumeration bidding, which fails with the same error. I will assume the workaround is going to be not include the fields from the front end, but the behavior should match other areas of aspnet, in concrete I am talking about SupplyParameterFromForm from Blazor SSR, which this scenario works fine (at least with enumerations) |
Just ran into this issue too. It would be great to have a fix for this. |
There's been some recent action on a PR to fix this over at #52499. |
@captainsafia The PR you referenced seems to be specific to binding from forms. Are there any plans on the similar issue when binding from queries? I'm facing the same problem as mentioned by dimenus:
In my case the parameter is a strongly typed ID which implements IParsable, and the parameter is nullable. I've had no luck with handling this particular scenario |
We merged a community contribution for this: But we still want to limit the change to a well-known subset of types. |
fixed in #60498 |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
From a Minimal API project
With the HTML form
And Mapping
app.MapPost("/SomeUrl", ([FromForm] SomeModel theInputModel) => Results.Ok());
And Model
If I enter a number in "SomeProp" input field, it works ok.
But if I leave "SomeProp" input field empty and the submit the form, I get the exception:
That is because the request sent to the server when posting the form is a POST with content :
someProp=
The only workaroung I found is using a string instead of int? and then convert string to int? by myself which is not the best solution.
Describe the solution you'd like
When binding [FromForm], I beleive that Minimal API should convert empty string to null when converting to a Nullable type (ex int?).
Or at least give any option to do so.
Additional context
No response
The text was updated successfully, but these errors were encountered: