Skip to content

Commit

Permalink
Add params Type[] parameters to IncludeXmlCommentsWithRemarks opt…
Browse files Browse the repository at this point in the history
…ion to exclude remarks for concrete types.
  • Loading branch information
unchase committed Oct 13, 2020
1 parent 736e7a7 commit bbc9f05
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

These are the changes to each version that has been released on the [nuget](https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions/).

## v2.4.1 `2020-10-13`

- [x] Add `params Type[]` parameters to `IncludeXmlCommentsWithRemarks` option to exclude remarks for concrete types

## v2.4.0 `2020-10-11`

- [x] Add `IncludeXmlCommentsWithRemarks` option to add xml comments from summary and remarks into the swagger documentation
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ public void ConfigureServices(IServiceCollection services)
// use it if you want to hide Paths and Definitions from OpenApi documentation correctly
options.UseAllOfToExtendReferenceSchemas();

// if you want to add xml comments (with remarks) into the swagger documentation, first of all add:
// if you want to add xml comments from summary and remarks into the swagger documentation, first of all add:
// you can exclude remarks for concrete types
var xmlFilePath = Path.Combine(AppContext.BaseDirectory, "WebApi3.1-Swashbuckle.xml");
options.IncludeXmlCommentsWithRemarks(xmlFilePath);
options.IncludeXmlCommentsWithRemarks(xmlFilePath, false,
typeof(ComplicatedClass),
typeof(InnerEnum));

// or add without remarks
//options.IncludeXmlComments(xmlFilePath);
Expand Down Expand Up @@ -336,6 +340,13 @@ public void ConfigureServices(IServiceCollection services)
// add xml comments from summary and remarks into the swagger documentation
options.IncludeXmlCommentsWithRemarks("<xml_File_Full_Path>");

// add xml comments from summary and remarks into the swagger documentation
// with excluding remarks for concrete types (since v2.4.1)
var xmlFilePath = Path.Combine(AppContext.BaseDirectory, "WebApi3.1-Swashbuckle.xml");
options.IncludeXmlCommentsWithRemarks(xmlFilePath, false,
typeof(ComplicatedClass),
typeof(InnerEnum));

...
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Net;
using System.Xml.XPath;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -95,20 +96,24 @@ void EmptyAction(FixEnumsOptions x) { }
/// Flag to indicate if controller XML comments (i.e. summary) should be used to assign Tag descriptions.
/// Don't set this flag if you're customizing the default tag for operations via TagActionsBy.
/// </param>
/// <param name="excludedTypes">Types for which remarks will be excluded.</param>
public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
this SwaggerGenOptions swaggerGenOptions,
Func<XPathDocument> xmlDocFactory,
bool includeControllerXmlComments = false)
bool includeControllerXmlComments = false,
params Type[] excludedTypes)
{
swaggerGenOptions.IncludeXmlComments(xmlDocFactory, includeControllerXmlComments);

var distinctExcludedTypes = excludedTypes.Distinct().ToList();

var xmlDoc = xmlDocFactory();
swaggerGenOptions.ParameterFilter<XmlCommentsWithRemarksParameterFilter>(xmlDoc);
swaggerGenOptions.RequestBodyFilter<XmlCommentsWithRemarksRequestBodyFilter>(xmlDoc);
swaggerGenOptions.SchemaFilter<XmlCommentsWithRemarksSchemaFilter>(xmlDoc);
swaggerGenOptions.ParameterFilter<XmlCommentsWithRemarksParameterFilter>(xmlDoc, distinctExcludedTypes);
swaggerGenOptions.RequestBodyFilter<XmlCommentsWithRemarksRequestBodyFilter>(xmlDoc, distinctExcludedTypes);
swaggerGenOptions.SchemaFilter<XmlCommentsWithRemarksSchemaFilter>(xmlDoc, distinctExcludedTypes);

if (includeControllerXmlComments)
swaggerGenOptions.DocumentFilter<XmlCommentsWithRemarksDocumentFilter>(xmlDoc);
swaggerGenOptions.DocumentFilter<XmlCommentsWithRemarksDocumentFilter>(xmlDoc, distinctExcludedTypes);

return swaggerGenOptions;
}
Expand All @@ -122,12 +127,14 @@ public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
/// Flag to indicate if controller XML comments (i.e. summary) should be used to assign Tag descriptions.
/// Don't set this flag if you're customizing the default tag for operations via TagActionsBy.
/// </param>
/// <param name="excludedTypes">Types for which remarks will be excluded.</param>
public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
this SwaggerGenOptions swaggerGenOptions,
string filePath,
bool includeControllerXmlComments = false)
bool includeControllerXmlComments = false,
params Type[] excludedTypes)
{
return swaggerGenOptions.IncludeXmlCommentsWithRemarks(() => new XPathDocument(filePath), includeControllerXmlComments);
return swaggerGenOptions.IncludeXmlCommentsWithRemarks(() => new XPathDocument(filePath), includeControllerXmlComments, excludedTypes);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class XmlCommentsWithRemarksDocumentFilter : IDocumentFilter
private const string MemberXPath = "/doc/members/member[@name='{0}']";
private const string SummaryTag = "summary";
private const string RemarksTag = "remarks";

private readonly XPathNavigator _xmlNavigator;
private readonly Type[] _excludedTypes;

#endregion

Expand All @@ -29,9 +29,11 @@ public class XmlCommentsWithRemarksDocumentFilter : IDocumentFilter
/// Constructor.
/// </summary>
/// <param name="xmlDoc"><see cref="XPathDocument"/></param>
public XmlCommentsWithRemarksDocumentFilter(XPathDocument xmlDoc)
/// <param name="excludedTypes">Excluded types.</param>
public XmlCommentsWithRemarksDocumentFilter(XPathDocument xmlDoc, params Type[] excludedTypes)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_excludedTypes = excludedTypes;
}

#endregion
Expand All @@ -54,6 +56,12 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)

foreach (var nameAndType in controllerNamesAndTypes)
{
if (_excludedTypes.ToList().Select(t => t.FullName)
.Contains(nameAndType.Value.FullName))
{
continue;
}

var memberName = XmlCommentsNodeNameHelper.GetMemberNameForType(nameAndType.Value);
var typeNode = _xmlNavigator.SelectSingleNode(string.Format(MemberXPath, memberName));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
using System;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand All @@ -15,6 +17,7 @@ public class XmlCommentsWithRemarksParameterFilter : IParameterFilter
private const string SummaryTag = "summary";
private const string RemarksTag = "remarks";
private readonly XPathNavigator _xmlNavigator;
private readonly Type[] _excludedTypes;

#endregion

Expand All @@ -24,9 +27,11 @@ public class XmlCommentsWithRemarksParameterFilter : IParameterFilter
/// Constructor.
/// </summary>
/// <param name="xmlDoc"><see cref="XPathDocument"/></param>
public XmlCommentsWithRemarksParameterFilter(XPathDocument xmlDoc)
/// <param name="excludedTypes">Excluded types.</param>
public XmlCommentsWithRemarksParameterFilter(XPathDocument xmlDoc, params Type[] excludedTypes)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_excludedTypes = excludedTypes;
}

#endregion
Expand All @@ -48,6 +53,12 @@ public void Apply(OpenApiParameter parameter, ParameterFilterContext context)

private void ApplyPropertyTags(OpenApiParameter parameter, PropertyInfo propertyInfo)
{
if (propertyInfo.DeclaringType != null && _excludedTypes.ToList().Select(t => t.FullName)
.Contains(propertyInfo.DeclaringType?.FullName))
{
return;
}

var propertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(propertyInfo);
var propertyNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{propertyMemberName}']");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
using System;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand All @@ -15,6 +17,7 @@ public class XmlCommentsWithRemarksRequestBodyFilter : IRequestBodyFilter
private const string SummaryTag = "summary";
private const string RemarksTag = "remarks";
private readonly XPathNavigator _xmlNavigator;
private readonly Type[] _excludedTypes;

#endregion

Expand All @@ -24,9 +27,11 @@ public class XmlCommentsWithRemarksRequestBodyFilter : IRequestBodyFilter
/// Constructor.
/// </summary>
/// <param name="xmlDoc"><see cref="XPathDocument"/></param>
public XmlCommentsWithRemarksRequestBodyFilter(XPathDocument xmlDoc)
/// <param name="excludedTypes">Excluded types.</param>
public XmlCommentsWithRemarksRequestBodyFilter(XPathDocument xmlDoc, params Type[] excludedTypes)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_excludedTypes = excludedTypes;
}

#endregion
Expand Down Expand Up @@ -54,6 +59,12 @@ public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext conte

private void ApplyPropertyTags(OpenApiRequestBody requestBody, PropertyInfo propertyInfo)
{
if (propertyInfo.DeclaringType != null && _excludedTypes.ToList().Select(t => t.FullName)
.Contains(propertyInfo.DeclaringType?.FullName))
{
return;
}

var propertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(propertyInfo);
var propertySummaryNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{propertyMemberName}']/{SummaryTag}");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;
Expand All @@ -16,6 +17,7 @@ public class XmlCommentsWithRemarksSchemaFilter : ISchemaFilter
private const string SummaryTag = "summary";
private const string RemarksTag = "remarks";
private readonly XPathNavigator _xmlNavigator;
private readonly Type[] _excludedTypes;

#endregion

Expand All @@ -25,9 +27,11 @@ public class XmlCommentsWithRemarksSchemaFilter : ISchemaFilter
/// Constructor.
/// </summary>
/// <param name="xmlDoc"><see cref="XPathDocument"/></param>
public XmlCommentsWithRemarksSchemaFilter(XPathDocument xmlDoc)
/// <param name="excludedTypes">Excluded types.</param>
public XmlCommentsWithRemarksSchemaFilter(XPathDocument xmlDoc, params Type[] excludedTypes)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_excludedTypes = excludedTypes;
}

#endregion
Expand Down Expand Up @@ -55,9 +59,14 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context)

private void ApplyTypeTags(OpenApiSchema schema, Type type)
{
if (_excludedTypes.ToList().Select(t => t.FullName)
.Contains(type.FullName))
{
return;
}

var typeMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(type);
var typeSummaryNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{typeMemberName}']/{SummaryTag}");

if (typeSummaryNode != null)
{
var typeRemarksNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{typeMemberName}']/{RemarksTag}");
Expand All @@ -71,9 +80,14 @@ private void ApplyTypeTags(OpenApiSchema schema, Type type)

private void ApplyFieldOrPropertyTags(OpenApiSchema schema, MemberInfo fieldOrPropertyInfo)
{
if (fieldOrPropertyInfo.DeclaringType != null && _excludedTypes.ToList().Select(t => t.FullName)
.Contains(fieldOrPropertyInfo.DeclaringType?.FullName))
{
return;
}

var fieldOrPropertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(fieldOrPropertyInfo);
var fieldOrPropertyNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{fieldOrPropertyMemberName}']");

var summaryNode = fieldOrPropertyNode?.SelectSingleNode(SummaryTag);
if (summaryNode != null)
{
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.4.0</Version>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
<Version>2.4.1</Version>
<AssemblyVersion>2.4.1.0</AssemblyVersion>
<FileVersion>2.4.1.0</FileVersion>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<DocumentationFile>Unchase.Swashbuckle.AspNetCore.Extensions.xml</DocumentationFile>
</PropertyGroup>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bbc9f05

Please sign in to comment.