Skip to content

Commit ba70ade

Browse files
committed
chore: include only referenced schema
1 parent 6fb72bc commit ba70ade

File tree

1 file changed

+70
-12
lines changed

1 file changed

+70
-12
lines changed

server/StrDss.Api/SwaggerDocumentFilter.cs

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,93 @@ public class SwaggerDocumentFilter : IDocumentFilter
77
{
88
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
99
{
10-
// Key is read-only so make a copy of the Paths property
11-
var pathsPerConsumer = new OpenApiPaths();
10+
var filteredPaths = new OpenApiPaths();
1211
var referencedSchemas = new HashSet<string>();
1312

1413
if (swaggerDoc.Info.Version == Common.ApiTags.Aps)
1514
{
1615
foreach (var path in swaggerDoc.Paths)
1716
{
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)))
2220
{
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);
2523
}
2624
}
27-
28-
2925
}
3026
else
3127
{
3228
foreach (var path in swaggerDoc.Paths)
3329
{
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]);
3551
}
3652
}
3753

38-
swaggerDoc.Paths = pathsPerConsumer;
54+
// Update the document's components with filtered schemas
55+
swaggerDoc.Components.Schemas = filteredSchemas;
3956
}
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+
4098
}
4199
}

0 commit comments

Comments
 (0)