From c48232cfa09ade23aa2b1addbddd2d0e5bebe502 Mon Sep 17 00:00:00 2001 From: Chebotov Nikolay Date: Wed, 25 Mar 2020 16:37:37 +0300 Subject: [PATCH] Add option to choose descriptions source - "FixEnumOptions.DescriptionSource"; Fix small bugs. --- CHANGELOG.md | 5 ++ README.md | 3 + .../Extensions/EnumTypeExtensions.cs | 80 ++++++++++++++----- .../Filters/XEnumNamesParameterFilter.cs | 6 +- .../Filters/XEnumNamesSchemaFilter.cs | 6 +- .../Options/FixEnumsOptions.cs | 6 ++ ...e.Swashbuckle.AspNetCore.Extensions.csproj | 6 +- ...hase.Swashbuckle.AspNetCore.Extensions.xml | 25 ++++++ test/WebApi3.1-Swashbuckle/Startup.cs | 3 + 9 files changed, 113 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba18e52..0887da6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ These are the changes to each version that has been released on the [nuget](https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions/). +## v2.3.3 `(2020-03-25)` + +- [x] Add option to choose descriptions source - `FixEnumOptions.DescriptionSource` +- [x] Fix small bugs + ## v2.3.2 `(2020-03-25)` - [x] Fix bug with `System.MissingMethodException` diff --git a/README.md b/README.md index 3d61423..07d6f64 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,9 @@ public void ConfigureServices(IServiceCollection services) // add descriptions from DescriptionAttribute or xml-comments to fix enums (add 'x-enumDescriptions' for schema extensions) for applied filters o.IncludeDescriptions = true; + // get descriptions from DescriptionAttribute then from xml-comments + o.DescriptionSource = DescriptionSources.DescriptionAttributesThenXmlComments; + // get descriptions from xml-file comments on the specified path // should use "options.IncludeXmlComments(xmlFilePath);" before o.IncludeXmlCommentsFrom(xmlFilePath); diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/EnumTypeExtensions.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/EnumTypeExtensions.cs index 219e942..ea8ec5d 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/EnumTypeExtensions.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/EnumTypeExtensions.cs @@ -11,6 +11,27 @@ namespace Unchase.Swashbuckle.AspNetCore.Extensions.Extensions { + /// + /// Description sources. + /// + public enum DescriptionSources + { + /// + /// . + /// + DescriptionAttributes = 0, + + /// + /// Xml comments. + /// + XmlComments = 1, + + /// + /// then xml comments. + /// + DescriptionAttributesThenXmlComments = 2 + } + internal static class EnumTypeExtensions { private static string GetDescriptionFromEnumOption(Type enumOptionType, object enumOption) @@ -29,18 +50,43 @@ private static string GetFieldAttributeDescription(this Type enumType, object en return string.Empty; } - internal static List GetEnumValuesDescription(Type enumType, IEnumerable xmlNavigators = null) + internal static List GetEnumValuesDescription(Type enumType, DescriptionSources descriptionSource, IEnumerable xmlNavigators = null) { var enumsDescriptions = new List(); foreach (var enumValue in Enum.GetValues(enumType)) { - var enumDescription = GetDescriptionFromEnumOption(enumType, enumValue); - if (string.IsNullOrWhiteSpace(enumDescription)) + var enumDescription = string.Empty; + try { - var memberInfo = enumType.GetMembers().FirstOrDefault(m => m.Name.Equals(enumValue.ToString(), StringComparison.InvariantCultureIgnoreCase)); - enumDescription = TryGetMemberComments(memberInfo, xmlNavigators); + switch (descriptionSource) + { + case DescriptionSources.DescriptionAttributes: + enumDescription = GetDescriptionFromEnumOption(enumType, enumValue); + break; + case DescriptionSources.XmlComments: + var memberInfo = enumType.GetMembers().FirstOrDefault(m => + m.Name.Equals(enumValue.ToString(), StringComparison.InvariantCultureIgnoreCase)); + enumDescription = TryGetMemberComments(memberInfo, xmlNavigators); + break; + case DescriptionSources.DescriptionAttributesThenXmlComments: + enumDescription = GetDescriptionFromEnumOption(enumType, enumValue); + if (string.IsNullOrWhiteSpace(enumDescription)) + { + var memberInfo2 = enumType.GetMembers().FirstOrDefault(m => + m.Name.Equals(enumValue.ToString(), StringComparison.InvariantCultureIgnoreCase)); + enumDescription = TryGetMemberComments(memberInfo2, xmlNavigators); + } + break; + } + } + catch + { + + } + finally + { + enumsDescriptions.Add(new OpenApiString(enumDescription)); } - enumsDescriptions.Add(new OpenApiString(enumDescription)); } return enumsDescriptions; } @@ -50,20 +96,13 @@ private static string TryGetMemberComments(MemberInfo memberInfo, IEnumerable _xmlNavigators = new HashSet(); @@ -35,6 +36,7 @@ public XEnumNamesParameterFilter(IOptions options, Action _xmlNavigators = new HashSet(); @@ -36,6 +37,7 @@ public XEnumNamesSchemaFilter(IOptions options, Action public bool IncludeDescriptions { get; set; } = false; + /// + /// Source to get descriptions. Default value is . + /// + public DescriptionSources DescriptionSource { get; set; } = DescriptionSources.DescriptionAttributes; + /// /// Apply fix enum filter to OpenApi schema. Default value is true. /// diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj index 8497f4e..35e209a 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj @@ -14,9 +14,9 @@ 7.3 https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/assets/icon.png?raw=true - 2.3.2 - 2.3.2.0 - 2.3.2.0 + 2.3.3 + 2.3.3.0 + 2.3.3.0 false Unchase.Swashbuckle.AspNetCore.Extensions.xml diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml index 3b1ff2a..f7f42bb 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml @@ -4,6 +4,26 @@ Unchase.Swashbuckle.AspNetCore.Extensions + + + Description sources. + + + + + . + + + + + Xml comments. + + + + + then xml comments. + + Extension methods for . @@ -290,6 +310,11 @@ Include descriptions from or xml comments. Default value is false. + + + Source to get descriptions. Default value is . + + Apply fix enum filter to OpenApi schema. Default value is true. diff --git a/test/WebApi3.1-Swashbuckle/Startup.cs b/test/WebApi3.1-Swashbuckle/Startup.cs index f9add5c..48d5ac9 100644 --- a/test/WebApi3.1-Swashbuckle/Startup.cs +++ b/test/WebApi3.1-Swashbuckle/Startup.cs @@ -56,6 +56,9 @@ public void ConfigureServices(IServiceCollection services) // add descriptions from DescriptionAttribute or xml-comments to fix enums (add 'x-enumDescriptions' for schema extensions) for applied filters o.IncludeDescriptions = true; + // get descriptions from DescriptionAttribute then from xml-comments + o.DescriptionSource = DescriptionSources.DescriptionAttributesThenXmlComments; + // get descriptions from xml-file comments on the specified path // should use "options.IncludeXmlComments(xmlFilePath);" before o.IncludeXmlCommentsFrom(xmlFilePath);