I have a WebAPI controller which looks like this:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public ActionResult<string> Get([FromQuery]DataFilter dataFilter)
{
return string.Join(Environment.NewLine, dataFilter?.Filter?.Select(f => f.ToString()) ?? Enumerable.Empty<string>());
}
}
Nothing fancy, just a controller which recevies some data from query string and outputs it as a response. The class it receives looks like this:
public class DataFilter
{
public IEnumerable<FilterType> Filter { get; set; }
}
public enum FilterType
{
One,
Two,
Three,
}
These are just sample classes to illustrate the problem, which is a validation error when trying to call this method like this:
/api/values?filter=
And the response:
{
"errors": {
"Filter": [
"The value '' is invalid."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "80000164-0002-fa00-b63f-84710c7967bb"
}
If i set my FilterType to be nullable, it works, but the array simply contains null values in this case. And if used like this:
/api/values?filter=&filter=&filter=
It will simply contain 3 null values. And i wanted it to be simply empty or null, since there are no real values passed.
The ASP .Net Core github account contains some similiar issues, but it is repored that they were fixed in 2.2, which is i'm using. But perhaps they are different or i missunderstand something.
EDIT_0: Just to show what i meant about nullable.
If i change my class to this:
public IEnumerable<FilterType?> Filter { get; set; } //notice that nullable is added to an Enum, not the list
Then when called like this:
/api/values?filter=&filter=&filter=
I get 3 elements in my "Filter" property. All nulls. Not exactly what i expect. Good as a workaround, but not a solution at all.
/api/values?