Skip to content

[Bug]: required nullable enum should include null #3225

@havarnov

Description

@havarnov

Describe the bug

When adding including a nullable enum it should be added to the schema with nullable: true and null should be included in the options, see https://swagger.io/docs/specification/v3_0/data-models/enums/#nullable-enums.

Expected behavior

An nullable enum should be added to the openapi schema on the following format:

type: string

nullable: true  # <---

enum:

  - asc

  - desc

  - null        # <--- without quotes, i.e. null not "null"

Actual behavior

This is how it's added now

type: string

enum:

  - asc

  - desc

Steps to reproduce

using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.Run();

[ApiController]
public class IndexController : ControllerBase
{
    [HttpGet("/index")]
    [ProducesResponseType<IndexResponseModel>(StatusCodes.Status200OK)]
    public IActionResult Index()
    {
        return Ok(new IndexResponseModel()
        {
            Timestamp = null,
            State = null,
        });
    }
}

public class IndexResponseModel
{
    public required DateTimeOffset? Timestamp { get; init; }
    public State? State { get; init; }
}

public enum State
{
    On = 1,
    Off = 2,
}

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

7.2.0

.NET Version

9.0.101

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions