Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit a4ff950

Browse files
committed
Aggiunto supporto Swagger Date and Time Only
1 parent 372ea21 commit a4ff950

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace NET6CustomLibrary.Swagger;
2+
3+
public static class SwaggerDateTime
4+
{
5+
public static IServiceCollection AddSwaggerGenDateTimeConfig(this IServiceCollection services, string title,
6+
string version, string description = "", bool extendSchema = false, string xmlCommentsPath = "")
7+
{
8+
services
9+
.AddEndpointsApiExplorer()
10+
.AddSwaggerGen(options =>
11+
{
12+
options.OperationFilter<CultureAwareOperationFilter>();
13+
options.SwaggerDoc($"{version}", new OpenApiInfo
14+
{
15+
Title = $"{title}",
16+
Version = $"{version}",
17+
Description = $"{description}",
18+
});
19+
20+
options.MapType<DateOnly>(() => new OpenApiSchema
21+
{
22+
Type = "string",
23+
Format = "date"
24+
});
25+
26+
options.MapType<TimeOnly>(() => new OpenApiSchema
27+
{
28+
Type = "string",
29+
Format = "time",
30+
Example = new OpenApiString(TimeOnly.FromDateTime(System.DateTime.Now).ToString("HH:mm:ss"))
31+
});
32+
33+
if (extendSchema)
34+
options.UseAllOfToExtendReferenceSchemas();
35+
36+
if (xmlCommentsPath is not (null or ""))
37+
options.IncludeXmlComments(xmlCommentsPath);
38+
});
39+
40+
return services;
41+
}
42+
}

0 commit comments

Comments
 (0)