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.15k stars 9.92k forks source link

DictionaryModelBinder not working with null default value in .Net 7 #56710

Closed FabricioMedeiros closed 1 month ago

FabricioMedeiros commented 1 month ago

Is there an existing issue for this?

Describe the bug

Hey guys,

I'm trying to create an endpoint with the "filters" parameter being optional, but an error occurs when it is not filled in.

public async Task GetAll([FromQuery] Dictionary<string, string>? filters = null) { Ok("Test ok"); }

Expected Behavior

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DictionaryModelBinder`2.BindModelAsync(ModelBindingContext bindingContext) at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value, Object container) at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>cDisplayClass0_0.<gBind|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gAwaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.gAwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS

Accept: / Accept-Encoding: gzip, deflate, br, zstd Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7 Connection: close Host: localhost:44364 Referer: https://localhost:44364/swagger/index.html User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 sec-ch-ua: "Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" sec-fetch-site: same-origin sec-fetch-mode: cors sec-fetch-dest: empty priority: u=1, i

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

.NET 7

Anything else?

No response

martincostello commented 1 month ago

.NET 7 is no longer in support.

Do you get the same exception with the latest release, .NET 8.0.7?

FabricioMedeiros commented 1 month ago

martincostello

Hello Martincostello, I'm sorry for the delay in responding, but I was sick the last few days.

I'm going to migrate my project to .Net 8 and check if the problem persists.

Regards,

Fabrício Medeiros

FabricioMedeiros commented 1 month ago

.NET 7 is no longer in support.

Do you get the same exception with the latest release, .NET 8.0.7?

Hi Martincostello, works fine in .NET 8.0.7, thanks.

Regards,

Fabrício Medeiros