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
+ }
0 commit comments