dotnet core 3.0 swagger 显示枚举描述
上一篇net core 2.2 swagger的枚举描述,core 3.0 需要升级swagger到5.0rc版,配置需要做些修改,swaager启用了OpenApi标准,之前的枚举描述方法也失效了。
这里通过了反射程序集,获取所有枚举定义放入字典,然后通过OpenApiSchema.Key来查表找到对应的枚举类型,通过Microsoft.OpenApi.Any.OpenApiInteger.Value强转会枚举,再补写枚举对应的描述。
/// <summary>
/// 基本状态
/// </summary> public enum BaseStatusEnum
{ /// <summary>
/// 逻辑删除状态
/// </summary>
[Description("逻辑删除状态")]
LogicDelete = -1, /// <summary>
/// 默认,未处理
/// </summary>
[Description("默认,未处理")]
Normal = 0, /// <summary>
/// 已处理
/// </summary>
[Description("已处理")]
Processed = 1
}
完整的Filter如下:
/// <summary>
/// Add enum value descriptions to Swagger
/// </summary>
public class SwaggerEnumFilter : IDocumentFilter
{
public void Apply(Microsoft.OpenApi.Models.OpenApiDocument swaggerDoc, DocumentFilterContext context)
//public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
Dictionary<string, Type> dict = GetAllEnum(); foreach (var item in swaggerDoc.Components.Schemas)
//foreach (var item in swaggerDoc.Definitions)
{
var property = item.Value;
var typeName = item.Key;
Type itemType = null;
if (property.Enum != null && property.Enum.Count > 0)
{
if (dict.ContainsKey(typeName))
{
itemType = dict[typeName];
}
else
{
itemType = null;
}
List<OpenApiInteger> list = new List<OpenApiInteger>();
foreach (var val in property.Enum)
{
list.Add((OpenApiInteger)val);
}
property.Description += DescribeEnum(itemType, list);
}
}
}
private static Dictionary<string, Type> GetAllEnum()
{
Assembly ass = Assembly.Load("Test.Model");
Type[] types = ass.GetTypes();
Dictionary<string, Type> dict = new Dictionary<string, Type>(); foreach (Type item in types)
{
if (item.IsEnum)
{
dict.Add(item.Name, item);
}
}
return dict;
} private static string DescribeEnum(Type type, List<OpenApiInteger> enums)
{
var enumDescriptions = new List<string>();
foreach (var item in enums)
{
if (type == null) continue;
var value = Enum.Parse(type, item.Value.ToString());
var desc = GetDescription(type, value); if (string.IsNullOrEmpty(desc))
enumDescriptions.Add($"{item.Value.ToString()}:{Enum.GetName(type, value)}; ");
else
enumDescriptions.Add($"{item.Value.ToString()}:{Enum.GetName(type, value)},{desc}; "); }
return $"<br/>{Environment.NewLine}{string.Join("<br/>" + Environment.NewLine, enumDescriptions)}";
} private static string GetDescription(Type t, object value)
{
foreach (MemberInfo mInfo in t.GetMembers())
{
if (mInfo.Name == t.GetEnumName(value))
{
foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
{
if (attr.GetType() == typeof(DescriptionAttribute))
{
return ((DescriptionAttribute)attr).Description;
}
}
}
}
return string.Empty;
}
}
dotnet core 3.0 swagger 显示枚举描述的更多相关文章
- dotnet core swagger filter 隐藏接口和显示枚举描述
dotnet core 2.2开发项目中,常会使用Swagger UI来生成在线Api文档. 某些接口不想放到Swagger中可以这样写Filter: /// <summary> /// ...
- DotNet Core 1.0 集成 CentOS 开发与运行环境部署
一. DotNet Core 1.0 开发环境部署 操作系统安装 我们使用CentOS 7.2.1511版本. 安装libunwind库 执行:sudo yum install libunwi ...
- 北京时间28号0点以后Scott Hanselman同志台宣布dotnet core 1.0 rtm
今日占住微信号头条的好消息<终于来了!微软.Net Core 1.0下载放出>.本人立马跑到官网http://dot.net看了一下,仍然是.net core 1.0 Preview 1版 ...
- centos 7 && dotnet core 2.0 && nginx && supervisor
前提 系统:centos 7 目录:/home/wwwroot/www.wuball.com dotnet core 2.0 官方指引 sudo rpm --import https://packag ...
- dotnet core 3.0 linux 部署小贴士
dotnet core 3.0 目前还是测试版,在linux下安装 sdk 需要有一些注意事项 1.下载url https://dotnet.microsoft.com/download/thank- ...
- DotNet Core 2.0使用MySql实现Code First
本教程使用vs2017 + dotnet core2.0 + MySql5.7.19 1.打开vs2017,文件>新建>项目,选择Asp.Net Core Web应用程序. 2.项目名称可 ...
- dotnet Core 2.0学习笔记(一)
一:Dotnet Core Windows运行环境,标红部分要注意 https://docs.microsoft.com/en-us/dotnet/core/windows-prerequisites ...
- dotnet core 2.0在ubuntu下安装失败
在ubuntu下安装.net core2.0失败了,不知道是什么原因.按照微软官方的步骤.似乎走不通.偶然翻到debian的安装方法,发现debian系统居然是直接下载包安装的.没经过apt.尝试一把 ...
- DotNet Core 2.0部署后外网IP访问
将DotNet Core2.0项目部署在Ubuntu上并且运行后,可以用localhost:5000来访问. 但是如果这时候用外网来访问就不行了. 这时候就有两种解决方案,第一种是用Nginx做代理实 ...
随机推荐
- Discuz! X3 数据字典
pre_common_admincp_cmenu 后台菜单收藏表 字段名 数据类型 默认值 允许非空 自动递增 备注 id smallint(6) unsigned NO 是 title v ...
- url 组成
- MANIFEST.MF文件对Import-Package/Export-Package重排列
众所周知,MANIFEST.MF文件中的空格开头的行是相当于拼接在上一行末尾的.很多又长又乱的Import-Package或者Export-Package,有时候想要搜索某个package却可能被换行 ...
- react-native 在iOS上使用http链接的图片地址不显示| iOS9 & iOS10 HTTP 不能正常使用的解决办法
https://segmentfault.com/a/1190000002933776 今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not ...
- NSSting NSData 与字符集合
NSString 是为了人类阅读而存在的,必须进行编码,以使得bytes对人类有意义 String Objects An NSString object encodes a Unicode-compl ...
- 关于springboot项目的jar和war两种打包方式部署的区别
关于springboot项目的jar和war两种打包方式部署的区别 关于springboot项目的jar和war两种打包方式部署的区别? https://bbs.csdn.net/topics/392 ...
- Mac 上 brew 安装Tomcat
首先保证brew命令能够正常使用: 搜索tomcat是否存在:brew search tomcat 安装tomcat:brew install tomcat 检查是否安装成功:catalina -h ...
- OpenCV 学习笔记(8)彩色图像RGB通道的分离、合并与显示
https://blog.csdn.net/ZYTTAE/article/details/42234989 由于算法的需要,需要把彩色图像的R.G.B值分离出来,OpenCV中正好有split() 和 ...
- 第六次作业--static关键字、对象
##题目一 ##Computer package Train.Method.TeachDemo.Thread.Fuction; /** * 求n的阶乘算法 * @author 喵 * @date 20 ...
- 使用google autoservice 自动生成java spi 描述文件
spi 是一种服务发现的标准,对于开发中我们通常需要编写 META-INF/services 文件夹中定义的类. google auto 中的autoservice 可以帮助我们生成对应的配置,很方便 ...