Skip to content

Commit

Permalink
fix(NRE): Fix another small bug
Browse files Browse the repository at this point in the history
  • Loading branch information
unchase committed Aug 26, 2021
1 parent b67a264 commit 7d07e99
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<XmlCommentsWithRemarksParameterFilter>(xmlDoc, distinctExcludedTypes);
Expand Down Expand Up @@ -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<InheritDocParameterFilter>(documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
swaggerGenOptions.RequestBodyFilter<InheritDocRequestBodyFilter>(documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
swaggerGenOptions.SchemaFilter<InheritDocSchemaFilter>(documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,38 @@ internal static class XmlCommentsExtensions

internal static Type GetTargetRecursive(this Type type, Dictionary<string, string> 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))
Expand All @@ -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<string, string> 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;

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<NeutralLanguage></NeutralLanguage>
<LangVersion>7.3</LangVersion>
<PackageIconUrl>https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/assets/icon.png?raw=true</PackageIconUrl>
<Version>2.6.4</Version>
<AssemblyVersion>2.6.4.0</AssemblyVersion>
<FileVersion>2.6.4.0</FileVersion>
<Version>2.6.5</Version>
<AssemblyVersion>2.6.5.0</AssemblyVersion>
<FileVersion>2.6.5.0</FileVersion>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<DocumentationFile>Unchase.Swashbuckle.AspNetCore.Extensions.xml</DocumentationFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebApi3.1-Swashbuckle/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) });

Expand Down

0 comments on commit 7d07e99

Please sign in to comment.