14
14
using VirtoCommerce . Platform . Core . Common ;
15
15
using VirtoCommerce . Platform . Core . Modularity ;
16
16
using VirtoCommerce . Platform . Core . Settings ;
17
- using VirtoCommerce . Platform . Data . Common ;
18
17
19
18
namespace VirtoCommerce . Platform . Web . Swagger
20
19
{
@@ -34,16 +33,22 @@ public static void RegisterRoutes(IUnityContainer container)
34
33
var xmlCommentsFilePaths = xmlCommentsDirectoryPaths . SelectMany ( GetXmlFilesPaths ) . ToArray ( ) ;
35
34
36
35
// Add separate swagger generator for platform
37
- EnableSwagger ( "VirtoCommerce.Platform" , httpConfiguration , container , routePrefix , xmlCommentsFilePaths , false , Assembly . GetExecutingAssembly ( ) ) ;
36
+ EnableSwagger ( "VirtoCommerce.Platform" , httpConfiguration , container , routePrefix , xmlCommentsFilePaths , false , Assembly . GetExecutingAssembly ( ) , null ) ;
38
37
39
38
// Add separate swagger generator for each installed module
40
39
var allmodules = container . Resolve < IModuleCatalog > ( ) . Modules . OfType < ManifestModuleInfo > ( ) . Where ( m => m . ModuleInstance != null ) ;
41
40
41
+ ICollection < Type > allPolymorphicTypes = new List < Type > ( ) ;
42
+
43
+ // Add separate swagger generator for each installed module
42
44
foreach ( var module in allmodules )
43
45
{
44
- EnableSwagger ( module . ModuleName , httpConfiguration , container , routePrefix , xmlCommentsFilePaths , module . UseFullTypeNameInSwagger , module . ModuleInstance . GetType ( ) . Assembly ) ;
46
+ var polimorphicTypes = GetPolymorphicTypes ( module ) ;
47
+
48
+ allPolymorphicTypes . AddRange ( polimorphicTypes ) ;
49
+
50
+ EnableSwagger ( module . ModuleName , httpConfiguration , container , routePrefix , xmlCommentsFilePaths , module . UseFullTypeNameInSwagger , module . ModuleInstance . GetType ( ) . Assembly , polimorphicTypes ) ;
45
51
}
46
- var allModuleAssemblies = allmodules . Select ( x => x . ModuleInstance . GetType ( ) . Assembly ) . Concat ( new [ ] { Assembly . GetExecutingAssembly ( ) } ) ;
47
52
48
53
// Add full swagger generator
49
54
httpConfiguration . EnableSwagger ( routePrefix + "docs/{apiVersion}" , c =>
@@ -67,7 +72,7 @@ public static void RegisterRoutes(IUnityContainer container)
67
72
. Replace ( ',' , '-' )
68
73
) ;
69
74
70
- AddProlymorphicFilters ( c , allModuleAssemblies ) ;
75
+ AddProlymorphicFilters ( c , allPolymorphicTypes . ToArray ( ) ) ;
71
76
72
77
ApplyCommonSwaggerConfiguration ( c , container , string . Empty , xmlCommentsFilePaths ) ;
73
78
} )
@@ -86,7 +91,32 @@ public static void RegisterRoutes(IUnityContainer container)
86
91
} ) ;
87
92
}
88
93
89
- private static void EnableSwagger ( string moduleName , HttpConfiguration httpConfiguration , IUnityContainer container , string routePrefix , string [ ] xmlCommentsFilePaths , bool useFullTypeNameInSchemaIds , Assembly apiAssembly )
94
+ private static Type [ ] GetPolymorphicTypes ( ManifestModuleInfo module )
95
+ {
96
+ //TODO: Could move type loading to module loading mechanism with its error handling
97
+ Type [ ] polimorphicTypes = null ;
98
+ try
99
+ {
100
+ polimorphicTypes = module . OpenAPIPolymorphicTypes
101
+ . Select ( x => Type . GetType ( x , false ) )
102
+ . Where ( x => x != null )
103
+ . ToArray ( ) ;
104
+ }
105
+ // Need to add catch as even with GetType throwOnError = false it could throw
106
+ catch { }
107
+
108
+ return polimorphicTypes ;
109
+ }
110
+
111
+ private static void EnableSwagger (
112
+ string moduleName ,
113
+ HttpConfiguration httpConfiguration ,
114
+ IUnityContainer container ,
115
+ string routePrefix ,
116
+ string [ ] xmlCommentsFilePaths ,
117
+ bool useFullTypeNameInSchemaIds ,
118
+ Assembly apiAssembly ,
119
+ Type [ ] polymorphicTypes )
90
120
{
91
121
var routeName = string . Concat ( "swagger_" , moduleName ) ;
92
122
var routeTemplate = string . Concat ( routePrefix , "docs/" , moduleName , "/{apiVersion}" ) ;
@@ -106,14 +136,13 @@ private static void EnableSwagger(string moduleName, HttpConfiguration httpConfi
106
136
ApplyCommonSwaggerConfiguration ( c , container , moduleName , xmlCommentsFilePaths ) ;
107
137
c . OperationFilter ( ( ) => new ModuleTagsFilter ( moduleName ) ) ;
108
138
109
- AddProlymorphicFilters ( c , new [ ] { apiAssembly } ) ;
139
+ AddProlymorphicFilters ( c , polymorphicTypes ) ;
110
140
} ) ;
111
141
}
112
142
113
- private static void AddProlymorphicFilters ( SwaggerDocsConfig swaggerDocsConfig , IEnumerable < Assembly > assemblies )
143
+ private static void AddProlymorphicFilters ( SwaggerDocsConfig swaggerDocsConfig , Type [ ] polymorphicTypes )
114
144
{
115
- var polymorphicTypes = assemblies . SelectMany ( x => x . GetTypesWithAttribute ( typeof ( PolymorphicBaseClassAttribute ) , false ) ) . ToArray ( ) ;
116
- if ( polymorphicTypes . Any ( ) )
145
+ if ( polymorphicTypes ? . Any ( ) == true )
117
146
{
118
147
swaggerDocsConfig . DocumentFilter ( ( ) => new PolymorphismDocumentFilter ( polymorphicTypes ) ) ;
119
148
swaggerDocsConfig . SchemaFilter ( ( ) => new PolymorphismSchemaFilter ( polymorphicTypes ) ) ;
0 commit comments