Fixing an issue of a null reference exception where the model binder is checked against a null BindingSource (#371)

This commit is contained in:
Gokhan Altinoren 2020-08-17 18:57:48 +03:00 committed by GitHub
parent 3b5065f283
commit aa52a5d6a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -29,7 +29,7 @@ namespace Dapr.AspNetCore
private static bool CanBind(ModelBinderProviderContext context, out Type type)
{
if (context.BindingInfo.BindingSource.Id == "state")
if (context.BindingInfo.BindingSource?.Id == "state")
{
// [FromState]
type = Unwrap(context.Metadata.ModelType);

View File

@ -46,5 +46,12 @@ namespace Dapr.AspNetCore.IntegrationTest.App
state.Value.Count++;
await state.SaveAsync();
}
[HttpPost("/echo-user")]
public ActionResult<UserInfo> EchoUser([FromQuery]UserInfo user)
{
// To simulate an action where there's no Dapr attribute, yet MVC still checks the list of available model binder providers.
return user;
}
}
}

View File

@ -69,5 +69,19 @@ namespace Dapr.AspNetCore.IntegrationTest
widget.Count.Should().Be(18);
}
}
[Fact]
public async Task ModelBinder_CanGetOutOfTheWayWhenTheresNoBinding()
{
using (var factory = new AppWebApplicationFactory())
{
var httpClient = factory.CreateClient();
var daprClient = factory.DaprClient;
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/echo-user?name=jimmy");
var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
}
}
}
}