-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
ValueProvider array of strings having a null value is processed wrong #40929
Comments
@dougbu, @Tratcher, @BrennanConroy just confirming whether this is still expected behavior or if we want to take action here. The decision not to support Regardless, |
StringValues come from headers and query strings where That said, yes, returning |
Agreed. I thought my updates in #35547 were limited to simply skipping I can't think of a More generally, your repo is basically test code. Have you found a real-world case where |
I have build a full working Json value provider and came across this strange behaviour. For my JsonParametersValueProvider, have a look at: |
To elaborate on the null values, and a read-world (?!) example ... Here is some of my javascript code, which posts json to a controller. It can be bind to method parameters, if we all want it ;-) netproxy("./api/ComplexListNullableDouble", { list: [1.2, 3.4, 5.6, null, 7.8] }); To this controller method: [HttpPost]
[Route("~/api/ComplexListNullableDouble")]
public async Task<IActionResult> ComplexListNullableDouble(List<double?> list)
{
await Task.Yield();
return Ok();
} Results in the repeat bug: 1.2, 3.4, 5.6, 5.6, 7.8 Concluding, for having nullable values, this means also nullable strings, but also empty strings, and they are, clearly, not the same. |
Thanks for contacting us. |
I pinpointed the location of the repeat error. It is in the collection binder. Mvc.Core/src/ModelBinding/Binders/CollectionModelBinder.cs I cleared out all the other options, and the error stil exists :-) builder.Services.AddMvcCore().AddMvcOptions(options =>
{
options.OutputFormatters.Clear();
options.InputFormatters.Clear();
options.ValueProviderFactories.Clear();
options.ModelValidatorProviders.Clear();
options.Conventions.Clear();
options.Filters.Clear();
options.ModelMetadataDetailsProviders.Clear();
options.ModelValidatorProviders.Clear();
options.ModelMetadataDetailsProviders.Clear();
options.ModelBinderProviders.Clear();
options.ModelBinderProviders.Add(new CollectionModelBinderProvider()); // Repeat previous value on null value
options.ModelBinderProviders.Add(new SimpleTypeModelBinderProvider());
options.ValueProviderFactories.Add(new TestWeb.SomeValueProviderFactory());
}); I hope this can be fixed. |
That's a good start but there are a few layers that need to be debugged to find the root cause. |
You are absolutely right, it is rather complex, however, the solution is simpler than we think ;-) Example: internal async Task<CollectionResult> BindSimpleCollection(
ModelBindingContext bindingContext,
ValueProviderResult values)
{
var boundCollection = new List<TElement?>();
var elementMetadata = bindingContext.ModelMetadata.ElementMetadata!;
foreach (var value in values)
{
if (value == null)
{
//boundCollection.Add(ModelBindingHelper.CastOrDefault<TElement>(null));
boundCollection.Add(default);
continue;
}
.....
..... |
The interesting part of this bug is the repeated value and filling in |
Sorry to disappoint you ;-) The But thats half of the story. After doing some days of intensive debugging there is a far greater problem. |
Other than removing a number of layers of abstraction, it seems you're proposing something like an More specifically, what |
Inputformatters are of no use. They are made to deliver 1 model |
Hi all, thanks for the discussion here! We've currently assigned this to the |
Let's keep this issue open for now. |
Is there an existing issue for this?
Describe the bug
Controller method
Expected Behavior
When using an Array of strings having null values in it, it must not skip, nor must it repeat previous element values.
Steps To Reproduce
The bug is visible in this small test project:
https://github.com/alphons/ValueProviderBug
Exceptions (if any)
No exceptions, bug procudes wrong processing of Array elements having null values.
.NET Version
6.0.102
Anything else?
ASP.NET 6.0.2
The text was updated successfully, but these errors were encountered: