Skip to content

Commit

Permalink
Merge pull request #17151 from masoodkhoshgard/Optimize-HideAbpEndPoints
Browse files Browse the repository at this point in the history
Optimize hide abp end points
  • Loading branch information
EngincanV committed Jul 20, 2023
2 parents cd8ce38 + 8b6a375 commit 4429ed5
Showing 1 changed file with 48 additions and 3 deletions.
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;
}
}

0 comments on commit 4429ed5

Please sign in to comment.