Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public void ConfigureServices(IServiceCollection services)
options.ChangeAllResponsesByHttpStatusCode(
httpStatusCode: HttpStatusCode.BadRequest,
responseDescription: "400 status code description",
responseExampleOption: ResponseExampleOptions.Clear, // claer response examples
responseExampleOption: ResponseExampleOptions.Clear, // clear response examples
responseExample: new ComplicatedClass()); // some class for response examples

// change responses for specific HTTP status code ("201" (StatusCodes.Status201Created))
Expand Down
19 changes: 13 additions & 6 deletions Unchase.Swashbuckle.AspNetCore.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi3.1-Swashbuckle", "te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi5-Swashbuckle", "test\WebApi5-Swashbuckle\WebApi5-Swashbuckle.csproj", "{C9251364-9941-4776-A9A3-841173DB58C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi7-Swashbuckle", "test\WebApi7-Swashbuckle\WebApi7-Swashbuckle.csproj", "{4C6ADEFF-E66A-4E7D-B8B7-24FCDB3F6A89}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi8-Swashbuckle", "test\WebApi8-Swashbuckle\WebApi8-Swashbuckle.csproj", "{BD6CDEB9-F093-4BD3-AA28-2B74B8A985B9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi7-Swashbuckle", "test\WebApi7-Swashbuckle\WebApi7-Swashbuckle.csproj", "{7868CB09-04B0-418F-A26E-316B4F73F865}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -60,10 +62,14 @@ Global
{C9251364-9941-4776-A9A3-841173DB58C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9251364-9941-4776-A9A3-841173DB58C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9251364-9941-4776-A9A3-841173DB58C4}.Release|Any CPU.Build.0 = Release|Any CPU
{4C6ADEFF-E66A-4E7D-B8B7-24FCDB3F6A89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C6ADEFF-E66A-4E7D-B8B7-24FCDB3F6A89}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C6ADEFF-E66A-4E7D-B8B7-24FCDB3F6A89}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C6ADEFF-E66A-4E7D-B8B7-24FCDB3F6A89}.Release|Any CPU.Build.0 = Release|Any CPU
{BD6CDEB9-F093-4BD3-AA28-2B74B8A985B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD6CDEB9-F093-4BD3-AA28-2B74B8A985B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD6CDEB9-F093-4BD3-AA28-2B74B8A985B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD6CDEB9-F093-4BD3-AA28-2B74B8A985B9}.Release|Any CPU.Build.0 = Release|Any CPU
{7868CB09-04B0-418F-A26E-316B4F73F865}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7868CB09-04B0-418F-A26E-316B4F73F865}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7868CB09-04B0-418F-A26E-316B4F73F865}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7868CB09-04B0-418F-A26E-316B4F73F865}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -73,7 +79,8 @@ Global
{ADFE3232-3F94-4582-BE4E-CD93603EAE18} = {41C53B82-ED58-4927-B597-CCCAEC8E3BF8}
{1AD5C40F-8AF7-4DAA-9C72-40D1269304C0} = {41C53B82-ED58-4927-B597-CCCAEC8E3BF8}
{C9251364-9941-4776-A9A3-841173DB58C4} = {41C53B82-ED58-4927-B597-CCCAEC8E3BF8}
{4C6ADEFF-E66A-4E7D-B8B7-24FCDB3F6A89} = {41C53B82-ED58-4927-B597-CCCAEC8E3BF8}
{BD6CDEB9-F093-4BD3-AA28-2B74B8A985B9} = {41C53B82-ED58-4927-B597-CCCAEC8E3BF8}
{7868CB09-04B0-418F-A26E-316B4F73F865} = {41C53B82-ED58-4927-B597-CCCAEC8E3BF8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4830AE48-C5E4-460E-B23E-B2D7D0578670}
Expand Down
4 changes: 4 additions & 0 deletions Unchase.Swashbuckle.AspNetCore.Extensions.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=inheritdocs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unchase/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=varnames/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Text;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.XPath;

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Unchase.Swashbuckle.AspNetCore.Extensions.Extensions
{
/// <summary>
Expand Down Expand Up @@ -41,8 +40,7 @@ private static string GetDescriptionFromEnumOption(
Type enumOptionType,
object enumOption)
{
return enumOptionType
.GetFieldAttributeDescription(enumOption, 0);
return GetFieldAttributeDescription(enumOptionType, enumOption, 0);
}

private static string GetFieldAttributeDescription(
Expand Down Expand Up @@ -85,6 +83,7 @@ private static string GetFieldAttributeDescription(
case DescriptionSources.XmlComments:
var memberInfo = enumType.GetMembers().FirstOrDefault(m =>
m.Name.Equals(enumValue.ToString(), StringComparison.InvariantCultureIgnoreCase));
// ReSharper disable once PossibleMultipleEnumeration
enumDescription = TryGetMemberComments(memberInfo, xmlNavigators, includeRemarks);
break;
case DescriptionSources.DescriptionAttributesThenXmlComments:
Expand All @@ -93,12 +92,14 @@ private static string GetFieldAttributeDescription(
{
var memberInfo2 = enumType.GetMembers().FirstOrDefault(m =>
m.Name.Equals(enumValue.ToString(), StringComparison.InvariantCultureIgnoreCase));
// ReSharper disable once PossibleMultipleEnumeration
enumDescription = TryGetMemberComments(memberInfo2, xmlNavigators, includeRemarks);
}

break;
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
Expand Down Expand Up @@ -140,6 +141,7 @@ private static string TryGetMemberComments(
commentsBuilder.Append(XmlCommentsTextHelper.Humanize(xpathSummaryNavigator.InnerXml));
if (includeRemarks)
{
// ReSharper disable once ConstantConditionalAccessQualifier
var xpathRemarksNavigator = xpathMemberNavigator?.SelectSingleNode("remarks");
if (xpathRemarksNavigator != null && !string.IsNullOrWhiteSpace(xpathRemarksNavigator.InnerXml))
{
Expand All @@ -158,6 +160,7 @@ private static string GetNodeNameForMember(
MemberInfo memberInfo)
{
var stringBuilder = new StringBuilder((memberInfo.MemberType & MemberTypes.Field) != 0 ? "F:" : "P:");
// ReSharper disable once RedundantArgumentDefaultValue
stringBuilder.Append(QualifiedNameFor(memberInfo.DeclaringType, false));
stringBuilder.Append("." + memberInfo.Name);
return stringBuilder.ToString();
Expand Down Expand Up @@ -187,8 +190,7 @@ private static string QualifiedNameFor(
{
var str = type.Name.Split('`').First();
stringBuilder.Append(str);
var values = type.GetGenericArguments()
.Select(t => !t.IsGenericParameter ? QualifiedNameFor(t, true) : string.Format("`{0}", t.GenericParameterPosition));
var values = type.GetGenericArguments().Select(t => !t.IsGenericParameter ? QualifiedNameFor(t, true) : string.Format("`{0}", t.GenericParameterPosition));
stringBuilder.Append("{" + string.Join(",", values) + "}");
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using Unchase.Swashbuckle.AspNetCore.Extensions.Factories;
using Unchase.Swashbuckle.AspNetCore.Extensions.Filters;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml.XPath;

using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.SwaggerGen;
using Unchase.Swashbuckle.AspNetCore.Extensions.Filters;
using Unchase.Swashbuckle.AspNetCore.Extensions.Options;

Expand Down Expand Up @@ -37,7 +35,7 @@ public static SwaggerGenOptions ChangeAllResponsesByHttpStatusCode<T>(
ResponseExampleOptions responseExampleOption = ResponseExampleOptions.None,
T responseExample = default) where T : class
{
swaggerGenOptions.DocumentFilter<ChangeResponseByHttpStatusCodeDocumentFilter<T>>(httpStatusCode, responseDescription, responseExampleOption, responseExample);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.DocumentFilter<ChangeResponseByHttpStatusCodeDocumentFilter<T>>(swaggerGenOptions, httpStatusCode, responseDescription, responseExampleOption, responseExample);
return swaggerGenOptions;
}

Expand All @@ -60,7 +58,7 @@ public static SwaggerGenOptions ChangeAllResponsesByHttpStatusCode<T>(
ResponseExampleOptions responseExampleOption = ResponseExampleOptions.None,
T responseExample = default) where T : class
{
return swaggerGenOptions.ChangeAllResponsesByHttpStatusCode((int)httpStatusCode, responseDescription, responseExampleOption, responseExample);
return ChangeAllResponsesByHttpStatusCode(swaggerGenOptions, (int)httpStatusCode, responseDescription, responseExampleOption, responseExample);
}

/// <summary>
Expand All @@ -75,9 +73,9 @@ public static SwaggerGenOptions AddEnumsWithValuesFixFilters(
// local function
void EmptyAction(FixEnumsOptions x) { }

swaggerGenOptions.SchemaFilter<XEnumNamesSchemaFilter>(configureOptions ?? EmptyAction);
swaggerGenOptions.ParameterFilter<XEnumNamesParameterFilter>(configureOptions ?? EmptyAction);
swaggerGenOptions.DocumentFilter<DisplayEnumsWithValuesDocumentFilter>();
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.SchemaFilter<XEnumNamesSchemaFilter>(swaggerGenOptions, configureOptions ?? EmptyAction);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.ParameterFilter<XEnumNamesParameterFilter>(swaggerGenOptions, configureOptions ?? EmptyAction);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.DocumentFilter<DisplayEnumsWithValuesDocumentFilter>(swaggerGenOptions);
return swaggerGenOptions;
}

Expand All @@ -97,18 +95,18 @@ public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
bool includeControllerXmlComments = false,
params Type[] excludedTypes)
{
swaggerGenOptions.IncludeXmlComments(xmlDocFactory, includeControllerXmlComments);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.IncludeXmlComments(swaggerGenOptions, xmlDocFactory, includeControllerXmlComments);

var distinctExcludedTypes = excludedTypes?.Distinct().ToArray() ?? new Type[] { };

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

if (includeControllerXmlComments)
{
swaggerGenOptions.DocumentFilter<XmlCommentsWithRemarksDocumentFilter>(xmlDoc, distinctExcludedTypes);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.DocumentFilter<XmlCommentsWithRemarksDocumentFilter>(swaggerGenOptions, xmlDoc, distinctExcludedTypes);
}

return swaggerGenOptions;
Expand All @@ -130,7 +128,7 @@ public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
bool includeControllerXmlComments = false,
params Type[] excludedTypes)
{
return swaggerGenOptions.IncludeXmlCommentsWithRemarks(() => new XPathDocument(filePath), includeControllerXmlComments, excludedTypes);
return IncludeXmlCommentsWithRemarks(swaggerGenOptions, () => new XPathDocument(filePath), includeControllerXmlComments, excludedTypes);
}

/// <summary>
Expand All @@ -149,7 +147,7 @@ public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
bool includeControllerXmlComments = false,
Func<Type[]> excludedTypesFunc = default)
{
return swaggerGenOptions.IncludeXmlCommentsWithRemarks(xmlDocFactory, includeControllerXmlComments, excludedTypesFunc?.Invoke());
return IncludeXmlCommentsWithRemarks(swaggerGenOptions, xmlDocFactory, includeControllerXmlComments, excludedTypesFunc?.Invoke());
}

/// <summary>
Expand All @@ -168,7 +166,7 @@ public static SwaggerGenOptions IncludeXmlCommentsWithRemarks(
bool includeControllerXmlComments = false,
Func<Type[]> excludedTypesFunc = default)
{
return swaggerGenOptions.IncludeXmlCommentsWithRemarks(() => new XPathDocument(filePath), includeControllerXmlComments, excludedTypesFunc?.Invoke());
return IncludeXmlCommentsWithRemarks(swaggerGenOptions, () => new XPathDocument(filePath), includeControllerXmlComments, excludedTypesFunc?.Invoke());
}

/// <summary>
Expand All @@ -184,41 +182,35 @@ public static SwaggerGenOptions IncludeXmlCommentsFromInheritDocs(
bool includeRemarks = false,
params Type[] excludedTypes)
{
var documents = swaggerGenOptions.SchemaFilterDescriptors.Where(x => x.Type == typeof(XmlCommentsSchemaFilter))
.Select(x => x.Arguments.Single())
.Cast<XPathDocument>()
.ToList();
var documents = swaggerGenOptions.SchemaFilterDescriptors.Where(x => x.Type == typeof(XmlCommentsSchemaFilter)).Select(x => x.Arguments.Single()).Cast<XPathDocument>().ToList();

var inheritedDocs = documents.SelectMany(
doc =>
var inheritedDocs = documents.SelectMany(doc =>
{
var inheritedElements = new List<(string Name, string Cref, string Path)>();
foreach (XPathNavigator member in doc.CreateNavigator().Select("doc/members/member/inheritdoc"))
{
string cref = member.GetAttribute("cref", string.Empty);
string path = member.GetAttribute("path", string.Empty);
member.MoveToParent();
string parentCref = member.GetAttribute("cref", string.Empty);
string parentPath = member.GetAttribute("path", string.Empty);
if (!string.IsNullOrWhiteSpace(parentCref))
{
var inheritedElements = new List<(string Name, string Cref, string Path)>();
foreach (XPathNavigator member in doc.CreateNavigator().Select("doc/members/member/inheritdoc"))
{
string cref = member.GetAttribute("cref", string.Empty);
string path = member.GetAttribute("path", string.Empty);
member.MoveToParent();
string parentCref = member.GetAttribute("cref", string.Empty);
string parentPath = member.GetAttribute("path", string.Empty);
if (!string.IsNullOrWhiteSpace(parentCref))
{
cref = parentCref;
path = parentPath;
}

inheritedElements.Add((member.GetAttribute("name", string.Empty), cref, path));
}

return inheritedElements;
})
.GroupBy(x => x.Name)
.ToDictionary(x => x.Key, x => (x.First().Cref, x.First().Path));
cref = parentCref;
path = parentPath;
}

inheritedElements.Add((member.GetAttribute("name", string.Empty), cref, path));
}

return inheritedElements;
}).GroupBy(x => x.Name).ToDictionary(x => x.Key, x => (x.First().Cref, x.First().Path));

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);
swaggerGenOptions.OperationFilter<InheritDocOperationFilter>(documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.ParameterFilter<InheritDocParameterFilter>(swaggerGenOptions, documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.RequestBodyFilter<InheritDocRequestBodyFilter>(swaggerGenOptions, documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.SchemaFilter<InheritDocSchemaFilter>(swaggerGenOptions, documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.OperationFilter<InheritDocOperationFilter>(swaggerGenOptions, documents, inheritedDocs, includeRemarks, distinctExcludedTypes);
return swaggerGenOptions;
}

Expand Down
Loading
Loading