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

.Net 6 TimeSpan is generated as schema in OpenAPI document but serialized as "time-span" formatted string #2505

Open
older opened this issue Sep 23, 2022 · 7 comments

Comments

@older
Copy link

older commented Sep 23, 2022

I'm using the latest .Net SDK 6.0.401 and Swashbuckle.AspNetCore 6.4.0

Application is generated using dotnet new webapi which includes Swashbuckle and uses System.Text.Json for serialization.
The way TimeSpan properties are described in generated OpenAPI document does not match the way it is actually gets serialized by the application. In the OpenAPI document TimeSpan properties get this schema: "$ref": "#/components/schemas/TimeSpan" but in runtime it is just serialized as a string in time-span format, for example: "00:01:02"
Maybe this breaking cnange in .Net 6.0.2 is a possible reason for this?

Minimal project demonstrating the problem is attached: SwashbuckleTimeSpan.zip

@douggish
Copy link

I ran into the same issue. For now I'm using this work-around:

builder.Services.AddSwaggerGen(options =>
{
	options.MapType<TimeSpan>(() => new OpenApiSchema
	{
		Type = "string",
		Example = new OpenApiString("00:00:00")
	});
});

@cremor
Copy link
Contributor

cremor commented Sep 12, 2023

@domaindrivendev This is still a problem with .NET 7 and Swashbuckle.AspNetCore v6.5.0.
System.Text.Json serializes TimeSpan as [0.]00:00:00[.000] but Swashbuckle generates a schema with all the properties of the TimeSpan type.

Related issue: #1630
From that it seems like Swashbuckle.AspNetCore.Newtonsoft adds this as date-span format to the schema? I couldn't find anything about that format, I assume it isn't a standard?

But it seems like there is also no standard for the format used by System.Text.Json. https://spec.openapis.org/registry/format/ contains duration, but that has a completely different syntax.

@ilya-scale
Copy link

ilya-scale commented Mar 5, 2024

It would be very nice if a part of addressing this issue would be providing the proper "format" and/or "pattern" field for the OpenApi. I am not sure if this is 100% correct, but I came up with this regex:

Format = "[-][d.]hh:mm:ss[.fffffff]",
Pattern = @"^(-)?(\d{1,4}\.)?\d{2}:\d{2}:\d{2}(\.\d{1,7})?$"

P.S. It limits the "days" part to only four digits, I guess it is really int.maxValue that is allowed, so a general case can support more of "days" digits

@Havunen
Copy link

Havunen commented Mar 5, 2024

Can you test if this works in DotSwashbuckle, there is custom mapping for TimeSpan to string // "date-span"

Copy link
Contributor

github-actions bot commented May 5, 2024

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

@github-actions github-actions bot added the stale Stale issues or pull requests label May 5, 2024
@agross
Copy link

agross commented May 5, 2024

Unstale

@martincostello
Copy link
Collaborator

#2804

@github-actions github-actions bot removed the stale Stale issues or pull requests label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants