Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize hide abp end points #17151

Merged
merged 7 commits into from
Jul 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,8 +28,29 @@ public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext cont
swaggerDoc
.Paths
.RemoveAll(path => !actionUrls.Contains(path.Key));

var actionSchemas = new HashSet<string>();
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<string> GetSchemaIdList(OpenApiPathItem pathItem, IDictionary<string, OpenApiSchema> schemas)
{
var selectedSchemas = new HashSet<OpenApiSchema>();
var schemaIds = new HashSet<string>();

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("?", "");
Expand All @@ -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<string> MakeListSchemaId(OpenApiSchema schema, IDictionary<string, OpenApiSchema> contextSchemas)
{
var schemasId = new HashSet<string>();
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;
}
}
Loading