domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.25k stars 1.31k forks source link

DateOnly not rendering correctly in Swagger #2771

Closed mattjohnsonpint closed 5 months ago

mattjohnsonpint commented 7 months ago

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:

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?

ghost commented 7 months ago

Also experiancing this with Controllers, when I tried upgrading to Swashbuckle.AspNetCore 6.5.0 to fix DateOnly serialisation fixed in https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2319 but the Swagger.json broke with missing elements so I had to revert back to Swashbuckle.AspNetCore 6.4.0.

Test

  1. Create a new Controller API project using the default template.
  2. Upgrade to Swashbuckle.AspNetCore 6.5.0.
  3. Enable IncludeXmlComments and wire it up.
  4. Run project and notice WeatherForecast no longer shows up in the SwaggerUI schema section.

dotnet --version 8.0.200

martincostello commented 6 months ago

We should probably make a copy of WeatherForecast and add it to our integration tests, as it seems to come up often as the repro for issues 😄

martincostello commented 6 months ago

Looks like this will be resolved by #2799.

image

vierlijner commented 6 months ago

TimeOnly also not for me. The config.MapType<TimeOnly>(() => new OpenApiSchema { Type = "string", Format = "time" }); also won't fix this in AddSwaggerGen. image I expect "14:32:55.348".

Maybe: https://learn.microsoft.com/en-us/dotnet/standard/datetime/how-to-use-dateonly-timeonly#serialize-dateonly-and-timeonly-types

martincostello commented 6 months ago

2799 and #2804 should fix this issue.

johanvaneck commented 5 months ago

It would be nice to have a default value that works out of the box, similar to System.DateTime 🙂

cremor commented 4 months ago

@johanvaneck Swashbuckle could in theory set an example value for all DateOnly fields. But it doesn't do it for DateTime fields either, that default value is created by Swagger UI. Therefore I think you should create an issue for that (or vote on an existing one) in the https://github.com/swagger-api/swagger-ui repository.