-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigureSwaggerOptions.cs
86 lines (74 loc) · 3.06 KB
/
ConfigureSwaggerOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using testSwaggerUIError.Services;
using Unchase.Swashbuckle.AspNetCore.Extensions.Extensions;
namespace testSwaggerUIError
{
public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
{
this.provider = provider;
}
public void Configure(SwaggerGenOptions options)
{
// add swagger document for every API version discovered
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
description.GroupName,
CreateVersionInfo(description));
}
// SwaggerDocFilter removes 'internal API' controllers and methods from swagger json
options.DocumentFilter<SwaggerDocFilter>();
options.OperationFilter<SwaggerDefaultValues>();
options.MapType<TimeSpan>(() => new OpenApiSchema
{
Type = "string",
Example = new OpenApiString("02:00:00"),
Format = "time-span"
});
options.CustomOperationIds(api =>
{
var group = api.GroupBySwaggerGroupAttribute();
var controller = group != null && group.Length > 0
? group[0]
: api.ActionDescriptor.RouteValues["controller"];
var action = api.ActionDescriptor.RouteValues["action"];
var operationId = $"{controller}_{action}";
return operationId;
});
// GroupBySwaggerGroupAttribute replaces standard tagging by controller name
// with custom grouping by GroupName of a SwaggerGroupAttribute
options.TagActionsBy(api => api.GroupBySwaggerGroupAttribute());
options.UseAllOfToExtendReferenceSchemas();
options.AddEnumsWithValuesFixFilters();
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
}
public void Configure(string name, SwaggerGenOptions options)
{
Configure(options);
}
private OpenApiInfo CreateVersionInfo(ApiVersionDescription description)
{
var info = new OpenApiInfo()
{
Title = "kBridge.WebApi",
Version = description.ApiVersion.ToString()
};
if (description.IsDeprecated)
{
info.Description += " This API version has been deprecated.";
}
return info;
}
}
}