-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathOpenApi.Extensions.cs
85 lines (73 loc) · 3.02 KB
/
OpenApi.Extensions.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
using Asp.Versioning;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Scalar.AspNetCore;
namespace eShop.ServiceDefaults;
public static partial class Extensions
{
public static IApplicationBuilder UseDefaultOpenApi(this WebApplication app)
{
var configuration = app.Configuration;
var openApiSection = configuration.GetSection("OpenApi");
if (!openApiSection.Exists())
{
return app;
}
app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapScalarApiReference(options =>
{
// Disable default fonts to avoid download unnecessary fonts
options.DefaultFonts = false;
});
app.MapGet("/", () => Results.Redirect("/scalar/v1")).ExcludeFromDescription();
}
return app;
}
public static IHostApplicationBuilder AddDefaultOpenApi(
this IHostApplicationBuilder builder,
IApiVersioningBuilder? apiVersioning = default)
{
var openApi = builder.Configuration.GetSection("OpenApi");
var identitySection = builder.Configuration.GetSection("Identity");
var scopes = identitySection.Exists()
? identitySection.GetRequiredSection("Scopes").GetChildren().ToDictionary(p => p.Key, p => p.Value)
: new Dictionary<string, string?>();
if (!openApi.Exists())
{
return builder;
}
if (apiVersioning is not null)
{
// the default format will just be ApiVersion.ToString(); for example, 1.0.
// this will format the version as "'v'major[.minor][-status]"
var versioned = apiVersioning.AddApiExplorer(options => options.GroupNameFormat = "'v'VVV");
string[] versions = ["v1", "v2"];
foreach (var description in versions)
{
builder.Services.AddOpenApi(description, options =>
{
options.ApplyApiVersionInfo(openApi.GetRequiredValue("Document:Title"), openApi.GetRequiredValue("Document:Description"));
options.ApplyAuthorizationChecks([.. scopes.Keys]);
options.ApplySecuritySchemeDefinitions();
options.ApplyOperationDeprecatedStatus();
options.ApplyApiVersionDescription();
options.ApplySchemaNullableFalse();
// Clear out the default servers so we can fallback to
// whatever ports have been allocated for the service by Aspire
options.AddDocumentTransformer((document, context, cancellationToken) =>
{
document.Servers = [];
return Task.CompletedTask;
});
});
}
}
return builder;
}
}