diff --git a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs index d35443f98b..c4c9c86c7f 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs @@ -13,7 +13,7 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter protected virtual string[] ActionUrlPrefixes { get; set; } = new[] { "Volo." }; protected virtual string RegexConstraintPattern => @":regex\(([^()]*)\)"; - + public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { var actionUrls = context.ApiDescriptions @@ -28,8 +28,29 @@ public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext cont swaggerDoc .Paths .RemoveAll(path => !actionUrls.Contains(path.Key)); + + var actionSchemas = new HashSet(); + actionSchemas.UnionWith(swaggerDoc.Paths.Select(path => new { path = path.Value, schemas = swaggerDoc.Components.Schemas }).SelectMany(x => GetSchemaIdList(x.path, x.schemas))); + + swaggerDoc.Components.Schemas.RemoveAll(schema => !actionSchemas.Contains(schema.Key)); } + protected virtual HashSet GetSchemaIdList(OpenApiPathItem pathItem, IDictionary schemas) + { + var selectedSchemas = new HashSet(); + var schemaIds = new HashSet(); + + selectedSchemas.UnionWith(pathItem.Parameters.Select(parameter => parameter.Schema)); + selectedSchemas.UnionWith(pathItem.Parameters.SelectMany(parameter => parameter.Content.Select(x => x.Value.Schema))); + selectedSchemas.UnionWith(pathItem.Operations.Values.SelectMany(c => c.Responses.SelectMany(x => x.Value.Content.Values.Select(x => x.Schema)))); + selectedSchemas.UnionWith(pathItem.Operations.Values.SelectMany(c => c.Parameters.Select(x => x.Schema))); + + foreach (var schema in selectedSchemas) + { + schemaIds.UnionWith(MakeListSchemaId(schema, schemas)); + } + return schemaIds; + } protected virtual string? RemoveRouteParameterConstraints(ActionDescriptor actionDescriptor) { var route = actionDescriptor.AttributeRouteInfo?.Template?.EnsureStartsWith('/').Replace("?", ""); @@ -49,10 +70,34 @@ public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext cont { break; } - - route = route.Remove(startIndex, (endIndex - startIndex)); + + route = route.Remove(startIndex, endIndex - startIndex); } return route; } + + private HashSet MakeListSchemaId(OpenApiSchema schema, IDictionary contextSchemas) + { + var schemasId = new HashSet(); + if (schema != null) + { + if (schema.Reference != null) + { + schemasId.Add(schema.Reference.Id); + foreach (var sc in contextSchemas.Where(x => x.Key == schema.Reference.Id).Select(x => x.Value)) + { + foreach (var property in sc.Properties.Values) + { + schemasId.UnionWith(MakeListSchemaId(property, contextSchemas)); + } + } + } + if (schema.Items != null) + { + schemasId.UnionWith(MakeListSchemaId(schema.Items, contextSchemas)); + } + } + return schemasId; + } } \ No newline at end of file