From 7d07e998ff3d964b99a20180f1bcd0864fdc6653 Mon Sep 17 00:00:00 2001 From: Chebotov Nikolay Date: Thu, 26 Aug 2021 15:45:58 +0300 Subject: [PATCH] fix(NRE): Fix another small bug --- .../Extensions/SwaggerGenOptionsExtensions.cs | 4 +- .../Extensions/XmlCommentsExtensions.cs | 70 +++++++++++++------ .../Filters/InheritDocSchemaFilter.cs | 5 ++ ...e.Swashbuckle.AspNetCore.Extensions.csproj | 6 +- test/WebApi3.1-Swashbuckle/Startup.cs | 2 +- 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/SwaggerGenOptionsExtensions.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/SwaggerGenOptionsExtensions.cs index 62c8d37..5355700 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/SwaggerGenOptionsExtensions.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/SwaggerGenOptionsExtensions.cs @@ -106,7 +106,7 @@ public static SwaggerGenOptions IncludeXmlCommentsWithRemarks( { swaggerGenOptions.IncludeXmlComments(xmlDocFactory, includeControllerXmlComments); - var distinctExcludedTypes = excludedTypes?.Distinct().ToArray(); + var distinctExcludedTypes = excludedTypes?.Distinct().ToArray() ?? new Type[] { }; var xmlDoc = xmlDocFactory(); swaggerGenOptions.ParameterFilter(xmlDoc, distinctExcludedTypes); @@ -214,7 +214,7 @@ public static SwaggerGenOptions IncludeXmlCommentsFromInheritDocs( }) .ToDictionary(x => x.Name, x => x.Cref); - var distinctExcludedTypes = excludedTypes?.Distinct().ToArray(); + var distinctExcludedTypes = excludedTypes?.Distinct().ToArray() ?? new Type[] { }; swaggerGenOptions.ParameterFilter(documents, inheritedDocs, includeRemarks, distinctExcludedTypes); swaggerGenOptions.RequestBodyFilter(documents, inheritedDocs, includeRemarks, distinctExcludedTypes); swaggerGenOptions.SchemaFilter(documents, inheritedDocs, includeRemarks, distinctExcludedTypes); diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/XmlCommentsExtensions.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/XmlCommentsExtensions.cs index 57a5a12..63ad45b 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/XmlCommentsExtensions.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/XmlCommentsExtensions.cs @@ -17,24 +17,38 @@ internal static class XmlCommentsExtensions internal static Type GetTargetRecursive(this Type type, Dictionary inheritedDocs, string cref) { - var target = GetTarget(type, cref); + var targets = GetTargets(type, cref); - if (target == null) + if (targets.Any() != true) { return null; } - string targetMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(target); - - if (inheritedDocs.ContainsKey(targetMemberName)) + foreach (var target in targets) { - return GetTarget(target, inheritedDocs[targetMemberName]); + if (target == null) + { + continue; + } + + string targetMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(target); + if (!string.IsNullOrWhiteSpace(targetMemberName)) + { + if (inheritedDocs.ContainsKey(targetMemberName)) + { + return GetTargetRecursive(target, inheritedDocs, inheritedDocs[targetMemberName]); + } + else + { + return target; + } + } } - return target; + return null; } - private static Type GetTarget(Type type, string cref) + private static Type[] GetTargets(Type type, string cref) { var targets = type.GetInterfaces(); if (type.BaseType != typeof(object)) @@ -49,34 +63,47 @@ private static Type GetTarget(Type type, string cref) if (crefTarget != null) { - return crefTarget; + return new[] { crefTarget }; } } - // We use the last since that will be our base class or the "nearest" implemented interface. - return targets.LastOrDefault(); + return targets.ToArray(); } internal static MemberInfo GetTargetRecursive(this MemberInfo memberInfo, Dictionary inheritedDocs, string cref) { - var target = GetTarget(memberInfo, cref); + var targets = GetTargets(memberInfo, cref); - if (target == null) + if (targets.Any() != true) { return null; } - string targetMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(target); - - if (inheritedDocs.ContainsKey(targetMemberName)) + foreach (var target in targets) { - return GetTarget(target, inheritedDocs[targetMemberName]); + if (target == null) + { + continue; + } + + string targetMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(target); + if (!string.IsNullOrWhiteSpace(targetMemberName)) + { + if (inheritedDocs.ContainsKey(targetMemberName)) + { + return GetTargetRecursive(target, inheritedDocs, inheritedDocs[targetMemberName]); + } + else + { + return target; + } + } } - return target; + return null; } - private static MemberInfo GetTarget(MemberInfo memberInfo, string cref) + private static MemberInfo[] GetTargets(MemberInfo memberInfo, string cref) { var type = memberInfo.DeclaringType ?? memberInfo.ReflectedType; @@ -103,12 +130,11 @@ private static MemberInfo GetTarget(MemberInfo memberInfo, string cref) if (crefTarget != null) { - return crefTarget; + return new[] { crefTarget }; } } - // We use the last since that will be our base class or the "nearest" implemented interface. - return targets.LastOrDefault(); + return targets.ToArray(); } internal static void ApplyPropertyComments( diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/InheritDocSchemaFilter.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/InheritDocSchemaFilter.cs index 585108e..4c1399a 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/InheritDocSchemaFilter.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/InheritDocSchemaFilter.cs @@ -78,6 +78,11 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context) string cref = _inheritedDocs[memberName]; var target = context.Type.GetTargetRecursive(_inheritedDocs, cref); + if (target == null) + { + return; + } + var targetXmlNode = XmlCommentsExtensions.GetMemberXmlNode(XmlCommentsNodeNameHelper.GetMemberNameForType(target), _documents); var summaryNode = targetXmlNode?.SelectSingleNode(SummaryTag); 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 cebae33..d1a50ac 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.6.4 - 2.6.4.0 - 2.6.4.0 + 2.6.5 + 2.6.5.0 + 2.6.5.0 false Unchase.Swashbuckle.AspNetCore.Extensions.xml diff --git a/test/WebApi3.1-Swashbuckle/Startup.cs b/test/WebApi3.1-Swashbuckle/Startup.cs index 93ae9fa..b0dde90 100644 --- a/test/WebApi3.1-Swashbuckle/Startup.cs +++ b/test/WebApi3.1-Swashbuckle/Startup.cs @@ -42,7 +42,7 @@ public void ConfigureServices(IServiceCollection services) // if you want to add xml comments from inheritdocs (from summary and remarks) into the swagger documentation, add: // you can exclude remarks for concrete types - options.IncludeXmlCommentsFromInheritDocs(includeRemarks: true, excludedTypes: typeof(string)); + options.IncludeXmlCommentsFromInheritDocs(includeRemarks: true); // options.IncludeXmlCommentsWithRemarks(filePath: xmlFilePath, includeControllerXmlComments: false, () => new[] { typeof(InnerEnum) });