Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateOnly not rendering correctly in Swagger #2771

Closed
mattjohnsonpint opened this issue Mar 6, 2024 · 5 comments · Fixed by #2799
Closed

DateOnly not rendering correctly in Swagger #2771

mattjohnsonpint opened this issue Mar 6, 2024 · 5 comments · Fixed by #2799
Labels

Comments

@mattjohnsonpint
Copy link

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?

@ghost
Copy link

ghost commented Mar 20, 2024

Also experiancing this with Controllers, when I tried upgrading to Swashbuckle.AspNetCore 6.5.0 to fix DateOnly serialisation fixed in #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
Copy link
Collaborator

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
Copy link
Collaborator

Looks like this will be resolved by #2799.

image

martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue Apr 14, 2024
Add test for domaindrivendev#2771 for `DateOnly` values.
@martincostello martincostello added bug and removed help-wanted A change up for grabs for contributions from the community labels Apr 14, 2024
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue Apr 16, 2024
Add test for domaindrivendev#2771 for `DateOnly` values.
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue Apr 23, 2024
Add test for domaindrivendev#2771 for `DateOnly` values.
@vierlijner
Copy link

vierlijner commented Apr 25, 2024

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
Copy link
Collaborator

#2799 and #2804 should fix this issue.

martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 1, 2024
Add test for domaindrivendev#2771 for `DateOnly` values.
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 8, 2024
Add test for domaindrivendev#2771 for `DateOnly` values.
martincostello added a commit that referenced this issue May 12, 2024
- Add support for .NET 8.
- Bump version to `6.6.0`.
- Add WeatherForecast .NET 8 Minimal API test application to resolve #2791.
- Add test for #2771 for `DateOnly` values.
- Use `JsonOptions` for Minimal APIs in .NET 8+ if not available from MVC.
- Use `JsonSerializerOptions.Default` where available.
- Use the Regex source generator for .NET 7+ to resolve #2794.
- Refactor JsonSerializerOptions resolution
- Make `ISerializerDataContractResolver` a singleton by default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants