|
| 1 | +using Microsoft.OpenApi.Models; |
| 2 | +using Swashbuckle.AspNetCore.SwaggerGen; |
| 3 | + |
| 4 | +namespace StrDss.Api |
| 5 | +{ |
| 6 | + public class SwaggerDocumentFilter : IDocumentFilter |
| 7 | + { |
| 8 | + public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) |
| 9 | + { |
| 10 | + var filteredPaths = new OpenApiPaths(); |
| 11 | + var referencedSchemas = new HashSet<string>(); |
| 12 | + |
| 13 | + if (swaggerDoc.Info.Version == Common.ApiTags.Aps) |
| 14 | + { |
| 15 | + foreach (var path in swaggerDoc.Paths) |
| 16 | + { |
| 17 | + var operation = path.Value?.Operations?.Values?.FirstOrDefault(); |
| 18 | + |
| 19 | + if (operation != null && operation.Tags.Any(t => Common.ApiTags.ApsTagList.Contains(t.Name))) |
| 20 | + { |
| 21 | + filteredPaths.Add(path.Key, path.Value); |
| 22 | + TrackSchemasInOperations(operation, referencedSchemas); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + foreach (var path in swaggerDoc.Paths) |
| 29 | + { |
| 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]); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Update the document's components with filtered schemas |
| 55 | + swaggerDoc.Components.Schemas = filteredSchemas; |
| 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 | + |
| 98 | + } |
| 99 | +} |
0 commit comments