Skip to content

Commit 08c2099

Browse files
author
Chris Young
committed
Added SwaggerOptions back
1 parent ff14c02 commit 08c2099

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using ArchitectNow.Web.Models;
5+
using Microsoft.AspNetCore.Builder.Internal;
6+
using NJsonSchema;
7+
using NSwag.AspNetCore;
8+
using NSwag.SwaggerGeneration.WebApi;
9+
10+
namespace ArchitectNow.Web.Configuration
11+
{
12+
public static class SwaggerExtensions
13+
{
14+
public static void ConfigureSwaggerUi3(this ApplicationBuilder builder, IEnumerable<SwaggerOptions> options, Action<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>> configure)
15+
{
16+
foreach (var option in options)
17+
{
18+
19+
if (option.Controllers?.Any() == true)
20+
{
21+
builder.UseSwaggerUi3(option.Controllers, settings => { ConfigureSettings(settings, configure, option); });
22+
}
23+
else
24+
{
25+
builder.UseSwaggerUi3(option.ControllerAssembly, settings => { ConfigureSettings(settings, configure, option); });
26+
}
27+
}
28+
}
29+
30+
public static void ConfigureSwaggerUi(this ApplicationBuilder builder, IEnumerable<SwaggerOptions> options, Action<SwaggerUiSettings<WebApiToSwaggerGeneratorSettings>> configure)
31+
{
32+
foreach (var option in options)
33+
{
34+
35+
if (option.Controllers?.Any() == true)
36+
{
37+
builder.UseSwaggerUi(option.Controllers, settings => { ConfigureSettings(settings, configure, option); });
38+
}
39+
else
40+
{
41+
builder.UseSwaggerUi(option.ControllerAssembly, settings => { ConfigureSettings(settings, configure, option); });
42+
}
43+
}
44+
}
45+
46+
public static void ConfigureSwaggerReDoc(this ApplicationBuilder builder, IEnumerable<SwaggerOptions> options, Action<SwaggerReDocSettings<WebApiToSwaggerGeneratorSettings>> configure)
47+
{
48+
foreach (var option in options)
49+
{
50+
51+
if (option.Controllers?.Any() == true)
52+
{
53+
builder.UseSwaggerReDoc(option.Controllers, settings => { ConfigureSettings(settings, configure, option); });
54+
}
55+
else
56+
{
57+
builder.UseSwaggerReDoc(option.ControllerAssembly, settings => { ConfigureSettings(settings, configure, option); });
58+
}
59+
}
60+
}
61+
62+
private static void ConfigureSettings<T>(T settings, Action<T> configure, SwaggerOptions option) where T: SwaggerUiSettingsBase<WebApiToSwaggerGeneratorSettings>
63+
{
64+
settings.SwaggerRoute = option.SwaggerRoute;
65+
settings.SwaggerUiRoute = option.SwaggerUiRoute;
66+
settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase;
67+
settings.GeneratorSettings.Title = option.Title;
68+
settings.GeneratorSettings.FlattenInheritanceHierarchy = true;
69+
settings.GeneratorSettings.IsAspNetCore = true;
70+
71+
foreach (var optionDocumentProcessor in option.DocumentProcessors)
72+
{
73+
settings.GeneratorSettings.DocumentProcessors.Add(optionDocumentProcessor);
74+
}
75+
76+
foreach (var operationProcessor in option.OperationProcessors)
77+
{
78+
settings.GeneratorSettings.OperationProcessors.Add(operationProcessor);
79+
}
80+
81+
configure?.Invoke(settings);
82+
}
83+
}
84+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Reflection;
5+
using NSwag;
6+
using NSwag.SwaggerGeneration.Processors;
7+
using NSwag.SwaggerGeneration.Processors.Security;
8+
9+
namespace ArchitectNow.Web.Models
10+
{
11+
public class SwaggerOptions
12+
{
13+
[SuppressMessage("ReSharper", "UnusedMember.Global")]
14+
public readonly string DefaultSwaggerRoute = "/docs/v1/swagger.json";
15+
16+
[SuppressMessage("ReSharper", "UnusedMember.Global")]
17+
public readonly string DefaultSwaggerUiRoute = "/docs";
18+
19+
public string Name { get; set; }
20+
public string Version { get; set; }
21+
public string Description { get; set; }
22+
public string Title { get; set; }
23+
public string SwaggerRoute { get; set; } = "/docs/v1/swagger.json";
24+
public string SwaggerUiRoute { get; set; } = "/docs";
25+
public IEnumerable<Type> Controllers { get; set; }
26+
public Assembly ControllerAssembly { get; set; } = Assembly.GetEntryAssembly();
27+
// public Action<SwaggerUiSettingsBase<WebApiToSwaggerGeneratorSettings>> Configure { get; set; }
28+
29+
public IList<IDocumentProcessor> DocumentProcessors { get; set; } = new List<IDocumentProcessor>();
30+
31+
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Global")]
32+
public IList<IOperationProcessor> OperationProcessors { get; set; } = new List<IOperationProcessor>();
33+
34+
public SwaggerOptions(bool includeAuthorizationHeader = true)
35+
{
36+
if (includeAuthorizationHeader)
37+
{
38+
DocumentProcessors.Add(
39+
new SecurityDefinitionAppender("Authorization", new SwaggerSecurityScheme
40+
{
41+
Type = SwaggerSecuritySchemeType.ApiKey,
42+
Name = "Authorization",
43+
In = SwaggerSecurityApiKeyLocation.Header
44+
})
45+
);
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)