From 725a04e17148e33c1466ab3a2da21905e094290e Mon Sep 17 00:00:00 2001 From: Masood Khoshgrd Date: Wed, 5 Jul 2023 15:12:31 +0330 Subject: [PATCH] Human Friendly Enum in Swagger --- .../Mvc/Json/MvcCoreBuilderExtensions.cs | 2 ++ .../AbpSwaggerGenOptionsExtensions.cs | 5 +++++ .../AbpSwashbuckleDocumentFilter.cs | 16 ++++++++++++++ .../AbpSwashbuckleEnumSchemaFilter.cs | 21 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleEnumSchemaFilter.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs index 8c6edd2eb0..b6d17d77b2 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -19,6 +20,7 @@ public static class MvcCoreBuilderExtensions options.JsonSerializerOptions.AllowTrailingCommas = true; options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory()); + options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter()); options.JsonSerializerOptions.Converters.Add(new AbpStringToGuidConverter()); options.JsonSerializerOptions.Converters.Add(new AbpNullableStringToGuidConverter()); diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs index 4313041064..240da0fc4e 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs @@ -9,4 +9,9 @@ public static class AbpSwaggerGenOptionsExtensions { swaggerGenOptions.DocumentFilter(); } + + public static void UserFriendlyEnums(this SwaggerGenOptions swaggerGenOptions) + { + swaggerGenOptions.SchemaFilter(); + } } \ No newline at end of file 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 dd1b9705b8..3f21df192c 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs @@ -28,6 +28,22 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter swaggerDoc .Paths .RemoveAll(path => !actionUrls.Contains(path.Key)); + + foreach (OpenApiSchema schema in swaggerDoc.Components.Schemas.Values) + { + ICollection properties = schema.Properties?.Values; + if (properties != null) + { + foreach (OpenApiSchema property in properties) + { + List propertyEnums = property.Enum?.Cast().ToList(); + if (propertyEnums != null && propertyEnums.Any()) + { + property.Description += "

Possible values:
" + string.Join("
", propertyEnums); + } + } + } + } } protected virtual string RemoveRouteParameterConstraints(ActionDescriptor actionDescriptor) diff --git a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleEnumSchemaFilter.cs b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleEnumSchemaFilter.cs new file mode 100644 index 0000000000..28f243aa9f --- /dev/null +++ b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleEnumSchemaFilter.cs @@ -0,0 +1,21 @@ +using Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using System; +using System.Linq; + +namespace Volo.Abp.Swashbuckle; + +public class AbpSwashbuckleEnumSchemaFilter : ISchemaFilter +{ + public void Apply(OpenApiSchema schema, SchemaFilterContext context) + { + if (context.Type.IsEnum) + { + schema.Enum.Clear(); + Enum.GetNames(context.Type) + .ToList() + .ForEach(name => schema.Enum.Add(new OpenApiString($"{name}"))); + } + } +} \ No newline at end of file