From f6be7579aa895315f4fbc845f8401d3441bf83a6 Mon Sep 17 00:00:00 2001 From: masoodkhoshgard Date: Wed, 19 Jul 2023 12:50:43 +0330 Subject: [PATCH 1/2] Optimize HideAbpEndpoints for dont show Unused Schema --- .../AbpSwashbuckleDocumentFilter.cs | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) 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 class AbpSwashbuckleDocumentFilter : IDocumentFilter 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 class AbpSwashbuckleDocumentFilter : IDocumentFilter { 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 From 8b6a3757e9dd5c58b2ba0d16feb1b7a3a4be3ff5 Mon Sep 17 00:00:00 2001 From: masoodkhoshgard Date: Thu, 20 Jul 2023 13:26:12 +0330 Subject: [PATCH 2/2] revert Improved display for Floating Labels --- .../Form/AbpInputTagHelperService.cs | 7 +-- .../Form/AbpSelectTagHelperService.cs | 59 ++++++++++++------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs index 9923e57de0..981116aa9f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs @@ -227,11 +227,10 @@ public class AbpInputTagHelperService : AbpTagHelperService var attribute = TagHelper.AspFor.ModelExplorer.GetAttribute(); - if (attribute != null || TagHelper.FloatingLabel) + if (attribute != null) { - var placeholderLocalized = _tagHelperLocalizer.GetLocalizedText(attribute?.Value ?? string.Empty, TagHelper.AspFor.ModelExplorer); - if (string.IsNullOrWhiteSpace(placeholderLocalized)) - placeholderLocalized = TagHelper.AspFor.Name; + var placeholderLocalized = _tagHelperLocalizer.GetLocalizedText(attribute.Value, TagHelper.AspFor.ModelExplorer); + inputTagHelperOutput.Attributes.Add("placeholder", placeholderLocalized); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs index 82f4393c3b..0edc8f1008 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs @@ -1,14 +1,15 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text.Encodings.Web; -using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Diagnostics; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.TagHelpers; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.Extensions.Localization; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text.Encodings.Web; +using System.Threading.Tasks; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Extensions; using Volo.Abp.Localization; @@ -37,7 +38,7 @@ public class AbpSelectTagHelperService : AbpTagHelperService _abpEnumLocalizer = abpEnumLocalizer; } - public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { var childContent = await output.GetChildContentAsync(); @@ -73,8 +74,7 @@ public class AbpSelectTagHelperService : AbpTagHelperService var validation = await GetValidationAsHtmlAsync(context, output, selectTag); var infoText = GetInfoAsHtml(context, output, selectTag); - return TagHelper.FloatingLabel ? selectAsHtml + Environment.NewLine + label + Environment.NewLine + infoText + Environment.NewLine + validation : - label + Environment.NewLine + selectAsHtml + Environment.NewLine + infoText + Environment.NewLine + validation; + return label + Environment.NewLine + selectAsHtml + Environment.NewLine + infoText + Environment.NewLine + validation; } protected virtual string SurroundInnerHtmlAndGet(TagHelperContext context, TagHelperOutput output, string innerHtml) @@ -156,15 +156,23 @@ public class AbpSelectTagHelperService : AbpTagHelperService } var selectItemsAttribute = TagHelper.AspFor.ModelExplorer.GetAttribute(); - return selectItemsAttribute != null - ? GetSelectItemsFromAttribute(selectItemsAttribute, TagHelper.AspFor.ModelExplorer) - : throw new Exception("No items provided for select attribute."); + if (selectItemsAttribute != null) + { + return GetSelectItemsFromAttribute(selectItemsAttribute, TagHelper.AspFor.ModelExplorer); + } + + throw new Exception("No items provided for select attribute."); } private bool IsEnum() { var value = TagHelper.AspFor.Model; - return (value != null && value.GetType().IsEnum) || TagHelper.AspFor.ModelExplorer.Metadata.IsEnum; + if (value != null && value.GetType().IsEnum) + { + return true; + } + + return TagHelper.AspFor.ModelExplorer.Metadata.IsEnum; } protected virtual async Task GetLabelAsHtmlAsync(TagHelperContext context, TagHelperOutput output, TagHelperOutput selectTag) @@ -288,7 +296,12 @@ public class AbpSelectTagHelperService : AbpTagHelperService { var selectItems = selectItemsAttribute.GetItems(explorer)?.ToList(); - return selectItems ?? new List(); + if (selectItems == null) + { + return new List(); + } + + return selectItems; } protected virtual async Task GetLabelAsHtmlUsingTagHelperAsync(TagHelperContext context, TagHelperOutput output) @@ -324,13 +337,17 @@ public class AbpSelectTagHelperService : AbpTagHelperService TagHelper.Size = attribute.Size; } - return TagHelper.Size switch + switch (TagHelper.Size) { - AbpFormControlSize.Small => "form-select-sm", - AbpFormControlSize.Medium => "form-select-md", - AbpFormControlSize.Large => "form-select-lg", - _ => "", - }; + case AbpFormControlSize.Small: + return "form-select-sm"; + case AbpFormControlSize.Medium: + return "form-select-md"; + case AbpFormControlSize.Large: + return "form-select-lg"; + } + + return ""; } protected virtual TagHelperAttributeList GetInputAttributes(TagHelperContext context, TagHelperOutput output) @@ -359,7 +376,7 @@ public class AbpSelectTagHelperService : AbpTagHelperService foreach (var tagHelperAttribute in tagHelperAttributes) { - var nameWithoutPrefix = tagHelperAttribute.Name[groupPrefix.Length..]; + var nameWithoutPrefix = tagHelperAttribute.Name.Substring(groupPrefix.Length); var newAttritube = new TagHelperAttribute(nameWithoutPrefix, tagHelperAttribute.Value); output.Attributes.Add(newAttritube); }