Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
steven.darby committed Jun 19, 2022
1 parent 5f42645 commit 8ac7de3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;
Expand All @@ -7,11 +10,14 @@ namespace Swashbuckle.AspNetCore.SwaggerGen
{
public class XmlCommentsOperationFilter : IOperationFilter
{
private readonly XPathNavigator _xmlNavigator;
private readonly Dictionary<string, XPathNavigator> _docMembers;

public XmlCommentsOperationFilter(XPathDocument xmlDoc)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_docMembers = xmlDoc.CreateNavigator()
.Select("/doc/members/member")
.OfType<XPathNavigator>()
.ToDictionary(memberNode => memberNode.GetAttribute("name", ""));
}

public void Apply(OpenApiOperation operation, OperationFilterContext context)
Expand All @@ -32,7 +38,10 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
private void ApplyControllerTags(OpenApiOperation operation, Type controllerType)
{
var typeMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(controllerType);
var responseNodes = _xmlNavigator.Select($"/doc/members/member[@name='{typeMemberName}']/response");

if (!_docMembers.TryGetValue(typeMemberName, out var methodNode)) return;

var responseNodes = methodNode.Select("response");
ApplyResponseTags(operation, responseNodes);
}

Expand All @@ -41,7 +50,7 @@ private void ApplyMethodTags(OpenApiOperation operation, MethodInfo methodInfo)
var methodMemberName = XmlCommentsNodeNameHelper.GetMemberNameForMethod(methodInfo);
var methodNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{methodMemberName}']");

if (methodNode == null) return;
if (!_docMembers.TryGetValue(methodMemberName, out var methodNode)) return;

var summaryNode = methodNode.SelectSingleNode("summary");
if (summaryNode != null)
Expand All @@ -60,12 +69,14 @@ private void ApplyResponseTags(OpenApiOperation operation, XPathNodeIterator res
while (responseNodes.MoveNext())
{
var code = responseNodes.Current.GetAttribute("code", "");
var response = operation.Responses.ContainsKey(code)
? operation.Responses[code]
: operation.Responses[code] = new OpenApiResponse();
if (!operation.Responses.TryGetValue(code, out var response))
{
response = new OpenApiResponse();
operation.Responses[code] = response;
}

response.Description = XmlCommentsTextHelper.Humanize(responseNodes.Current.InnerXml);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
using System.Reflection;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;

namespace Swashbuckle.AspNetCore.SwaggerGen
{
public class XmlCommentsParameterFilter : IParameterFilter
{
private XPathNavigator _xmlNavigator;
private readonly Dictionary<string, XPathNavigator> _docMembers;

public XmlCommentsParameterFilter(XPathDocument xmlDoc)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_docMembers = xmlDoc.CreateNavigator()
.Select("/doc/members/member")
.OfType<XPathNavigator>()
.ToDictionary(memberNode => memberNode.GetAttribute("name", ""));
}

public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
Expand All @@ -30,7 +36,7 @@ private void ApplyPropertyTags(OpenApiParameter parameter, ParameterFilterContex
var propertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(context.PropertyInfo);
var propertyNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{propertyMemberName}']");

if (propertyNode == null) return;
if (!_docMembers.TryGetValue(propertyMemberName, out var propertyNode)) return;

var summaryNode = propertyNode.SelectSingleNode("summary");
if (summaryNode != null)
Expand Down Expand Up @@ -61,8 +67,10 @@ private void ApplyParamTags(OpenApiParameter parameter, ParameterFilterContext c
if (targetMethod == null) return;

var methodMemberName = XmlCommentsNodeNameHelper.GetMemberNameForMethod(targetMethod);
var paramNode = _xmlNavigator.SelectSingleNode(
$"/doc/members/member[@name='{methodMemberName}']/param[@name='{context.ParameterInfo.Name}']");

if (!_docMembers.TryGetValue(methodMemberName, out var propertyNode)) return;

var paramNode = propertyNode.SelectSingleNode($"param[@name='{context.ParameterInfo.Name}']");

if (paramNode != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
using System.Reflection;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;

namespace Swashbuckle.AspNetCore.SwaggerGen
{
public class XmlCommentsRequestBodyFilter : IRequestBodyFilter
{
private readonly XPathNavigator _xmlNavigator;
private readonly Dictionary<string, XPathNavigator> _docMembers;

public XmlCommentsRequestBodyFilter(XPathDocument xmlDoc)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_docMembers = xmlDoc.CreateNavigator()
.Select("/doc/members/member")
.OfType<XPathNavigator>()
.ToDictionary(memberNode => memberNode.GetAttribute("name", ""));
}

public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
Expand Down Expand Up @@ -39,7 +45,7 @@ private void ApplyPropertyTags(OpenApiRequestBody requestBody, RequestBodyFilter
var propertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(propertyInfo);
var propertyNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{propertyMemberName}']");

if (propertyNode == null) return;
if (!_docMembers.TryGetValue(propertyMemberName, out var propertyNode)) return;

var summaryNode = propertyNode.SelectSingleNode("summary");
if (summaryNode != null)
Expand Down Expand Up @@ -70,8 +76,10 @@ private void ApplyParamTags(OpenApiRequestBody requestBody, RequestBodyFilterCon
if (targetMethod == null) return;

var methodMemberName = XmlCommentsNodeNameHelper.GetMemberNameForMethod(targetMethod);
var paramNode = _xmlNavigator.SelectSingleNode(
$"/doc/members/member[@name='{methodMemberName}']/param[@name='{parameterInfo.Name}']");

if (!_docMembers.TryGetValue(methodMemberName, out var propertyNode)) return;

var paramNode = propertyNode.SelectSingleNode($"param[@name='{parameterInfo.Name}']");

if (paramNode != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;

Expand All @@ -10,7 +13,10 @@ public class XmlCommentsSchemaFilter : ISchemaFilter

public XmlCommentsSchemaFilter(XPathDocument xmlDoc)
{
_xmlNavigator = xmlDoc.CreateNavigator();
_docMembers = xmlDoc.CreateNavigator()
.Select("/doc/members/member")
.OfType<XPathNavigator>()
.ToDictionary(memberNode => memberNode.GetAttribute("name", ""));
}

public void Apply(OpenApiSchema schema, SchemaFilterContext context)
Expand All @@ -26,7 +32,10 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context)
private void ApplyTypeTags(OpenApiSchema schema, Type type)
{
var typeMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(type);
var typeSummaryNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{typeMemberName}']/summary");

if (!_docMembers.TryGetValue(typeMemberName, out var memberNode)) return;

var typeSummaryNode = memberNode.SelectSingleNode("summary");

if (typeSummaryNode != null)
{
Expand Down

0 comments on commit 8ac7de3

Please sign in to comment.