dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

[FromBody] attribute with an empty body request should set the default value of nullable value types to null. #57055

Open opuzakov opened 1 month ago

opuzakov commented 1 month ago

Is there an existing issue for this?

Describe the bug

Controlled-based API uses additional conditions to check the nullable value type

https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/Mvc/Mvc.Core/src/Infrastructure/ParameterDefaultValues.cs#L40 https://github.com/dotnet/runtime/blob/2aade6beb02ea367fd97c4070a4198802fe61c03/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs#L35,

which doesn't exist in the minimal API approach

https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/Http/Http.Extensions/src/RequestDelegateFactory.cs#L1323

Expected Behavior

The default value of nullable value types should be null when the request body is empty.

Steps To Reproduce

This repo contains two web API projects: controller-based and minimal. The controller-based approach sets the int? default value to null, and the minimal API to 0.

Exceptions (if any)

No response

.NET Version

8.0.303

Anything else?

No response

captainsafia commented 3 weeks ago

@opuzakov Thanks for opening this issue!

I think that using the ParameterDefaultValue APIs in RDF seems like a valid choice. We'll have to confirm that we get similar behavior in the scenario where request delegates are generated statically via RDG.

Putting this in the backlog for now since I don't think it's particularly high-priority but I'd be open to reviewing a PR if you're interested in opening one.