@@ -7,35 +7,93 @@ public class SwaggerDocumentFilter : IDocumentFilter
7
7
{
8
8
public void Apply ( OpenApiDocument swaggerDoc , DocumentFilterContext context )
9
9
{
10
- // Key is read-only so make a copy of the Paths property
11
- var pathsPerConsumer = new OpenApiPaths ( ) ;
10
+ var filteredPaths = new OpenApiPaths ( ) ;
12
11
var referencedSchemas = new HashSet < string > ( ) ;
13
12
14
13
if ( swaggerDoc . Info . Version == Common . ApiTags . Aps )
15
14
{
16
15
foreach ( var path in swaggerDoc . Paths )
17
16
{
18
- // If there are any tags (all methods are decorated with "SwaggerOperation(Tags = new[]...") with the current consumer name
19
- var p = path . Value ? . Operations ? . Values ? . FirstOrDefault ( ) ;
20
- if ( p != null && p . Tags
21
- . Where ( t => Common . ApiTags . ApsTagList . Contains ( t . Name ) ) . Any ( ) )
17
+ var operation = path . Value ? . Operations ? . Values ? . FirstOrDefault ( ) ;
18
+
19
+ if ( operation != null && operation . Tags . Any ( t => Common . ApiTags . ApsTagList . Contains ( t . Name ) ) )
22
20
{
23
- // Add the path to the collection of paths for current consumer
24
- pathsPerConsumer . Add ( path . Key , path . Value ) ;
21
+ filteredPaths . Add ( path . Key , path . Value ) ;
22
+ TrackSchemasInOperations ( operation , referencedSchemas ) ;
25
23
}
26
24
}
27
-
28
-
29
25
}
30
26
else
31
27
{
32
28
foreach ( var path in swaggerDoc . Paths )
33
29
{
34
- if ( path . Key != null && path . Value != null ) pathsPerConsumer . Add ( path . Key , path . Value ) ;
30
+ if ( path . Key != null && path . Value != null )
31
+ {
32
+ filteredPaths . Add ( path . Key , path . Value ) ;
33
+
34
+ foreach ( var operation in path . Value . Operations . Values )
35
+ {
36
+ TrackSchemasInOperations ( operation , referencedSchemas ) ;
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ swaggerDoc . Paths = filteredPaths ;
43
+
44
+ // Filter schemas in components to only include those that are referenced
45
+ var filteredSchemas = new Dictionary < string , OpenApiSchema > ( ) ;
46
+ foreach ( var schemaKey in swaggerDoc . Components . Schemas . Keys )
47
+ {
48
+ if ( referencedSchemas . Contains ( schemaKey ) )
49
+ {
50
+ filteredSchemas . Add ( schemaKey , swaggerDoc . Components . Schemas [ schemaKey ] ) ;
35
51
}
36
52
}
37
53
38
- swaggerDoc . Paths = pathsPerConsumer ;
54
+ // Update the document's components with filtered schemas
55
+ swaggerDoc . Components . Schemas = filteredSchemas ;
39
56
}
57
+
58
+ /// <summary>
59
+ /// Tracks the schemas used in the operation's parameters, request bodies, and responses.
60
+ /// </summary>
61
+ private void TrackSchemasInOperations ( OpenApiOperation operation , HashSet < string > referencedSchemas )
62
+ {
63
+ // Track schemas from parameters
64
+ foreach ( var parameter in operation . Parameters )
65
+ {
66
+ if ( parameter . Schema ? . Reference != null )
67
+ {
68
+ referencedSchemas . Add ( parameter . Schema . Reference . Id ) ;
69
+ }
70
+ }
71
+
72
+ // Track schemas from request bodies
73
+ if ( operation . RequestBody ? . Content != null )
74
+ {
75
+ foreach ( var content in operation . RequestBody . Content . Values )
76
+ {
77
+ if ( content . Schema ? . Reference != null )
78
+ {
79
+ referencedSchemas . Add ( content . Schema . Reference . Id ) ;
80
+ }
81
+ }
82
+ }
83
+
84
+ // Track schemas from responses
85
+ foreach ( var response in operation . Responses . Values )
86
+ {
87
+ foreach ( var content in response . Content . Values )
88
+ {
89
+ if ( content . Schema ? . Reference != null )
90
+ {
91
+ referencedSchemas . Add ( content . Schema . Reference . Id ) ;
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+
40
98
}
41
99
}
0 commit comments