SwaggerUI 不显示枚举摘要描述,C# .net 核心?

Moh*_*yan 9 c# enums swagger-ui .net-core asp.net-core-webapi

我使用https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.1&tabs=visual-studio#xml-comments来显示我的课程摘要说明在 SwaggerUI 中,可以但不显示enum摘要描述!
我的startup.cs

services.AddSwaggerGen(c =>
{   
    c.SwaggerDoc("v1", new Info
    {
        Version = "v1",
        Title = "My App-Service",
        Description = "My Description",
    });
    c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));  
    c.DescribeAllEnumsAsStrings();
});
Run Code Online (Sandbox Code Playgroud)

我的enum

public enum GenderEnum
{
    /// <summary>
    /// Man Description
    /// </summary>
    Man = 1,

    /// <summary>
    /// Woman Description
    /// </summary>
    Woman = 2
}
Run Code Online (Sandbox Code Playgroud)

它显示如下内容:
Swagger UI 枚举

我想像 这样在 SwaggerUI 中显示Man Description和显示:Woman Description

Man = 1, Man Description
Woman = 2,  Woman Description
Run Code Online (Sandbox Code Playgroud)


我正在使用Swashbuckle.AspNetCore v4.0.1

Pau*_*her 9

该解决方案允许

  • 显示潜在价值以及名称/描述
  • 处理多个 xml 文档文件,但仅处理一次文档。
  • 无需更改代码即可自定义布局

这是班级...

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;

using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace YourNamespace
{
    /// <summary>
    /// Swagger schema filter to modify description of enum types so they
    /// show the XML docs attached to each member of the enum.
    /// </summary>
    public class DescribeEnumMembers : ISchemaFilter
    {
        private readonly XDocument xmlComments;
        private readonly string assemblyName;

        /// <summary>
        /// Initialize schema filter.
        /// </summary>
        /// <param name="xmlComments">Document containing XML docs for enum members.</param>
        public DescribeEnumMembers(XDocument xmlComments)
        {
            this.xmlComments = xmlComments;
            this.assemblyName = DetermineAssembly(xmlComments);
        }

        /// <summary>
        /// Pre-amble to use before the enum items
        /// </summary>
        public static string Prefix { get; set; } = "<p>Possible values:</p>";

        /// <summary>
        /// Format to use, 0 : value, 1: Name, 2: Description
        /// </summary>
        public static string Format { get; set; } = "<b>{0} - {1}</b>: {2}";

        /// <summary>
        /// Apply this schema filter.
        /// </summary>
        /// <param name="schema">Target schema object.</param>
        /// <param name="context">Schema filter context.</param>
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            var type = context.Type;

            // Only process enums and...
            if (!type.IsEnum)
            {
                return;
            }

            // ...only the comments defined in their origin assembly
            if (type.Assembly.GetName().Name != assemblyName)
            {
                return;
            }
            var sb = new StringBuilder(schema.Description);

            if (!string.IsNullOrEmpty(Prefix))
            {
                sb.AppendLine(Prefix);
            }

            sb.AppendLine("<ul>");

            // TODO: Handle flags better e.g. Hex formatting
            foreach (var name in Enum.GetValues(type))
            {
                // Allows for large enums
                var value = Convert.ToInt64(name);
                var fullName = $"F:{type.FullName}.{name}";

                var description = xmlComments.XPathEvaluate(
                    $"normalize-space(//member[@name = '{fullName}']/summary/text())"
                ) as string;

                sb.AppendLine(string.Format("<li>" + Format + "</li>", value, name, description));
            }

            sb.AppendLine("</ul>");

            schema.Description = sb.ToString();
        }

        private string DetermineAssembly(XDocument doc)
        {
            var name = ((IEnumerable)doc.XPathEvaluate("/doc/assembly")).Cast<XElement>().ToList().FirstOrDefault();
            
            return name?.Value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和利用...

services.AddSwaggerGen(c =>
{
    ...
    
    // See https://github.com/domaindrivendev/Swashbuckle/issues/86
    var dir = new DirectoryInfo(AppContext.BaseDirectory);
    
    foreach (var fi in dir.EnumerateFiles("*.xml"))
    {
        var doc = XDocument.Load(fi.FullName);
        c.IncludeXmlComments(() => new XPathDocument(doc.CreateReader()), true);
        c.SchemaFilter<DescribeEnumMembers>(doc);
    }
});
Run Code Online (Sandbox Code Playgroud)

然后报告为

在此输入图像描述


Eri*_*tta 8

截至 2021 年 6 月,OpenApi 现在支持此功能,您可以扩展 Swagger 以显示详细信息。这是我在 .NET 5.0 上的 C# 代码。

首先在文件中定义架构过滤器(调用它DescribeEnumMembers.cs并确保更改YourNamespace为您的命名空间的名称):

using System;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;

using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace YourNamespace
{
  /// <summary>
  /// Swagger schema filter to modify description of enum types so they
  /// show the XML docs attached to each member of the enum.
  /// </summary>
  public class DescribeEnumMembers: ISchemaFilter
  {
    private readonly XDocument mXmlComments;

    /// <summary>
    /// Initialize schema filter.
    /// </summary>
    /// <param name="argXmlComments">Document containing XML docs for enum members.</param>
    public DescribeEnumMembers(XDocument argXmlComments)
      => mXmlComments = argXmlComments;

    /// <summary>
    /// Apply this schema filter.
    /// </summary>
    /// <param name="argSchema">Target schema object.</param>
    /// <param name="argContext">Schema filter context.</param>
    public void Apply(OpenApiSchema argSchema, SchemaFilterContext argContext) {
      var EnumType = argContext.Type;

      if(!EnumType.IsEnum) return;

      var sb = new StringBuilder(argSchema.Description);

      sb.AppendLine("<p>Possible values:</p>");
      sb.AppendLine("<ul>");

      foreach(var EnumMemberName in Enum.GetNames(EnumType)) {
        var FullEnumMemberName = $"F:{EnumType.FullName}.{EnumMemberName}";

        var EnumMemberDescription = mXmlComments.XPathEvaluate(
          $"normalize-space(//member[@name = '{FullEnumMemberName}']/summary/text())"
        ) as string;

        if(string.IsNullOrEmpty(EnumMemberDescription)) continue;

        sb.AppendLine($"<li><b>{EnumMemberName}</b>: {EnumMemberDescription}</li>");
      }

      sb.AppendLine("</ul>");

      argSchema.Description = sb.ToString();
    }
  }
}

Run Code Online (Sandbox Code Playgroud)

然后在 ASP.NETConfigureServices()方法中启用它。这是我剪掉与本练习无关的部分后的代码:

public void ConfigureServices(IServiceCollection argServices) {
  // ...<snip other code>

  argServices.AddSwaggerGen(SetSwaggerGenOptions);

  // ...<snip other code>

  return;

  // ...<snip other code>

  void SetSwaggerGenOptions(SwaggerGenOptions argOptions) {
    // ...<snip other code>

    AddXmlDocs();
    return;

    void AddXmlDocs() {
      // generate paths for the XML doc files in the assembly's directory.
      var XmlDocPaths = Directory.GetFiles(
        path: AppDomain.CurrentDomain.BaseDirectory,
        searchPattern: "YourAssemblyNameHere*.xml"
      );

      // load the XML docs for processing.
      var XmlDocs = (
        from DocPath in XmlDocPaths select XDocument.Load(DocPath)
      ).ToList();

      // ...<snip other code>

      // add pre-processed XML docs to Swagger.
      foreach(var doc in XmlDocs) {
        argOptions.IncludeXmlComments(() => new XPathDocument(doc.CreateReader()), true);

        // apply schema filter to add description of enum members.
        argOptions.SchemaFilter<DescribeEnumMembers>(doc);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请记住进行更改"YourAssemblyNameHere*.xml"以匹配您的程序集名称。启用架构过滤器的重要行是:

argOptions.SchemaFilter<DescribeEnumMembers>(doc);
Run Code Online (Sandbox Code Playgroud)

...必须在以下行之后调用:

argOptions.IncludeXmlComments(() => new XPathDocument(doc.CreateReader()), true);
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,如果您有一个像这样定义的枚举类型,例如:

  /// <summary>
  /// Setting to control when a no-match report is returned when searching.
  /// </summary>
  public enum NoMatchReportSetting
  {
    /// <summary>
    /// Return no-match report only if the search query has no match.
    /// </summary>
    IfNoMatch = 0,
    /// <summary>
    /// Always return no-match report even if the search query has a match.
    /// </summary>
    Always = 1,
    /// <summary>
    /// Never return no-match report even if search query has no match.
    /// </summary>
    No = 99
  }
Run Code Online (Sandbox Code Playgroud)

Swagger 文档最终将显示每个枚举成员的描述,作为枚举类型本身描述的一部分:

招摇文档截图


Tar*_*ncu -6

我使用描述属性解决了这个问题。这是一个用法示例:

public enum GenderEnum
{
    [Description("Man Description")]
    Man = 1,

    [Description("Woman Description")]
    Woman = 2
}
Run Code Online (Sandbox Code Playgroud)

  • 添加这些注释后,UI 中发生了哪些变化? (2认同)