Skip to content

DateOnly not rendering correctly in Swagger #2771

@mattjohnsonpint

Description

@mattjohnsonpint

I think this may be a regression of #2319, but in investigating this stackoverflow question, I found that DateOnly fields are still not showing correctly in the Swagger UI in ASP.NET 8 latest.

To reproduce:

  • Download and install .NET 8 SDK 8.0.2 (SDK 8.0.201)
  • dotnet new webapi
  • dotnet run
  • Browse to Swagger endpoint, i.e. http://localhost:5261/swagger

Notice DateOnly and DayOfWeek have been broken out into properties as if custom types, rather than being a date.

image

Apply the workaround:

builder.Services.AddSwaggerGen(options => {
    options.MapType<DateOnly>(() => new OpenApiSchema { 
        Type = "string",
        Format = "date" });
});

Then it works:
image

This shouldn't be necessary though, right?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions