Skip to content
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

Minimal API : Converting empty string to Nullable (ex: int?) with [FromForm] binding #55202

Open
1 task done
JimmyLahaie opened this issue Apr 18, 2024 · 2 comments
Open
1 task done
Labels
area-blazor Includes: Blazor, Razor Components area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-blazor-form-validation This issue is related to forms validation in Blazor
Milestone

Comments

@JimmyLahaie
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

From a Minimal API project

With the HTML form

<form method="post" action="/SomeUrl">
<input type="text" name="someProp" />
<button type="submit">OK</button>
</form>

And Mapping
app.MapPost("/SomeUrl", ([FromForm] SomeModel theInputModel) => Results.Ok());

And Model

public class SomeModel
{
    public int? SomeProp { get; set; }
}

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:

Microsoft.AspNetCore.Http.BadHttpRequestException: The value '' is not valid for 'SomeProp'.

 ---> Microsoft.AspNetCore.Components.Endpoints.FormMapping.FormDataMappingException: An error occurred while trying to map a value from form data. For more details, see the 'Error' property and the 'InnerException' property.

        at Microsoft.AspNetCore.Components.Endpoints.FormMapping.FormDataReader.AddMappingError(Exception exception, String attemptedValue)

        at Microsoft.AspNetCore.Components.Endpoints.FormMapping.CompiledComplexTypeConverter`1.TryRead(FormDataReader& context, Type type, FormDataMapperOptions options, T& result, Boolean& found)

        at Microsoft.AspNetCore.Components.Endpoints.FormMapping.FormDataMapper.Map[T](FormDataReader reader, FormDataMapperOptions options)

        at lambda_method280(Closure, Object, HttpContext, Object)

        --- End of inner exception stack trace ---

        at Microsoft.AspNetCore.Http.RequestDelegateFactory.Log.FormDataMappingFailed(HttpContext httpContext, String parameterTypeName, String parameterName, FormDataMappingException exception, Boolean shouldThrow)

        at lambda_method280(Closure, Object, HttpContext, Object)

        at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass104_2.<<HandleRequestBodyAndCompileRequestDelegateForForm>b__2>d.MoveNext()

     --- End of stack trace from previous location ---

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Apr 18, 2024
@KennethHoff
Copy link

I've also experienced this issue. I "fixed" it by updating my frontend to delete all empty fields before sending it to the backend.

@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Apr 18, 2024
@captainsafia
Copy link
Member

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 captainsafia added this to the Backlog milestone Apr 30, 2024
@captainsafia captainsafia removed their assignment Apr 30, 2024
@captainsafia captainsafia added area-blazor Includes: Blazor, Razor Components feature-blazor-form-validation This issue is related to forms validation in Blazor labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-blazor-form-validation This issue is related to forms validation in Blazor
Projects
None yet
Development

No branches or pull requests

4 participants