Search code examples
c#.netasp.net-coreswaggerswagger-ui

Is there a way to send empty strings in a query parameter array in swagger for .NET 6?


I am working on a NET6.0 controller endpoint where I want the user to be able to provide two query parameters that will be arrays of strings. One of the arrays should require all items in the array to have a value (a guid), while the other should be able to accept null or empty values.

Below is the code I have currently:

Program.cs

services.AddSwaggerGen(options =>
{
    options.SupportNonNullableReferenceTypes();
}

NewController.cs

[ApiController]
[ApiVersion("1.0")]
[Route("company/v{api-version:apiVersion}/[controller]")]
public class NewController : ControllerBase
{
    [HttpGet("{entityId:int}/lookup")]
    public IActionResult LookUpBeforeCreate(
        [FromRoute] int entityId,
        [BindRequired, FromQuery(Name = "attr")] IEnumerable<Guid> attributes,
        [BindRequired, FromQuery(Name = "val")] IEnumerable<string?> values)
    {
        Console.WriteLine("attributeGuids:");
        foreach (var guid in attributes)
        {
            Console.WriteLine($"Id: {guid}");
        }

        Console.WriteLine("attributeValues:");
        foreach (var value in values)
        {
            if (value is null)
            {
                Console.WriteLine($"Value is null");
            }
            else if (value.Equals(string.Empty))
            {
                Console.WriteLine($"Value is empty");
            }
            else
            {
                Console.WriteLine($"Value: {value}");
            }
        }

        return Ok();
    }
}

Current swagger page:

Current swagger page

Currently, I am unable to send an empty string, and can only send null values by leaving the swagger fields empty. Is there any way I can configure the endpoint/swagger to allow me to send null and empty values?


Solution

  • The comment reply to my original post answered the question. Thank you browsermator.

    The answer can be found here:

    In asp.net core, are empty string converted to NULL?