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做代理实 ...
随机推荐
- 一些坑 Java 执行命令行命令 Spring Boot 打包为jar ResourceUtils.getFile 等出现的问题
Java 执行命令行命令 这个没技术含量的东西耗费了我半个多小时 String command = ....; Process process = Runtime.getRuntime().exec( ...
- Angular pagination分页模块 只提供分页参数处理 不处理分页记录数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- npm install 命令。默认会找到当前路径下的package.json。然后安装其中的依赖
npm install 命令.默认会找到当前路径下的package.json.然后安装其中的依赖 By default, npm install will install all modules li ...
- mysql 执行存储过程报错Prepared statement needs to be re-prepared
今日思语:不喜欢再见 说再见,因为有时明知道下一次再见已是遥遥无期或是不再见 错误如下: ERROR 1615 (HY000) at line 406 in file: 'update-mysql.s ...
- Codechef July Challenge 2019 Hit the Coconuts
假设现在有一堆数,我想要保证能取出一个,至少需要敲 (数的个数)*(这些数里的最小值)那么把这些数从大到小排序,$dp[i][j]$ 表示前 $i$ 个里面保证能取出 $j$ 个需要敲的次数.$dp[ ...
- python--requests模块初识
requests,发送http请求(用python模拟浏览器浏览网页)requests.get("http://www.baidu.com") 示例: import request ...
- 内存模型学习-- Container Executor task之间的关系
(分割线前的都是废话) java8内存模型: http://www.cnblogs.com/paddix/p/5309550.html http://www.cnblogs.com/dingyings ...
- 014_matlab读取ecxel(直接导入)
视频教程:https://v.qq.com/x/page/c3039b5htwx.html 资料下载:https://download.csdn.net/download/xiaoguoge11/12 ...
- eclipse隐藏的列编辑
作为开发人员,应该大部分都懂列编辑模式,最早接触使用列编辑模式是用UE,后来用了notepad++,列编辑模式也很顺手. 以前用eclipse编辑代码想用列编辑时还以为它不支持就打开notepad++ ...
- VMware Tools安装后设置自动挂载解决共享文件夹无法显示的问题
一. 确保成功安装了VMware Tools 二. 使用如下命令 1.apt-get install open-vm-tools 2.vmhgfs-fuse .host:/ /mnt/hgfs ...