aspnetcore: ValueProvider array of strings having a null value is processed wrong

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

public class SomeValueProvider : IValueProvider
{
  public bool ContainsPrefix(string prefix)
  {
    return true;
  }
  public ValueProviderResult GetValue(string key)
  {
    return new ValueProviderResult(new string[] { "a", "b", null, "c" });
  }
}

Controller method

public async Task<IActionResult> Index(List<string> list)
{
  await Task.Yield();

  // Returned list "a", b", "b",  "c"
  // Expected list "a", b", null, "c"

  return Content("<pre>returns "+string.Join(",", list)+ " expected a,b,,c</pre>", "text/html");
}

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

To elaborate on the null values, and a read-world (?!) example … Maybe in forms or querystring request strings null has no much of a meaning, but when posting json data to a MVC controller, and binding its parameters, we have to use the famous ValueProviderResult. And string.Empty has a complete different meaning then null. Turning string.Empty into null has also devastating effects, mostly gaining nothing, i call it a weird feature or artifact.

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 is a simple javascript function to make json post calls to an url. Source …/wwwroot/Scripts/netproxy.js

 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.