Null array parameter in query string .net6 swaggerUI

Viewed 17

I am trying to allow support for querying on a date range in my get request. the "after" and "Between" work fine but I'd also like to implement "before". SwaggerUI will not allow me to leave an array value blank for 'Birthday'

Controller

[HttpGet]
    [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<Patient>))]
    public async Task<IActionResult> Read([FromQuery] GetPatientsQuery parameters)
    {
        var result = await _mediator.Dispatch(parameters);
        return Ok(result);

    }

Parameter object (Inherited by GetPatientsQuery)

public class GetPatientsParameters
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public DateTime?[]? Birthday { get; set; } //search a range
    public string? Gender { get; set; }
}

I thought settings a nullable array of nullables would work here

Linq To Entity Query

return await _context.Set<Patient>()
                .Where(x => query.FirstName == null || x.FirstName.ToLower().StartsWith(query.FirstName.ToLower()))
                .Where(x => query.LastName == null || x.LastName.ToLower().StartsWith(query.LastName.ToLower()))

                //support searching birthdays in a range
                .Where(x => query.Birthday == null || x.Birthday > (query.Birthday.ElementAtOrDefault(0) ?? DateTime.MinValue))
                .Where(x => query.Birthday == null || x.Birthday < (query.Birthday.ElementAtOrDefault(1) ?? DateTime.MaxValue))

                .Where(x => query.Gender == null || x.Gender.ToLower() == query.Gender.ToLower())

                .ToListAsync();

SwaggerUi expects value for nullable date time

1 Answers

May be a quirk of Swagger UI

calling this endpoint directly yields expected results

https://localhost:7283/Patient?Birthday=&Birthday=2022-09-06T21%3A59%3A00.660Z

but not this

https://localhost:7283/Patient?Birthday=null&Birthday=2022-09-06T21%3A59%3A00.660Z
Related