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做代理实 ...
随机推荐
- VSCode自动保存文件设置
很多时候敲了一大堆代码,结果手贱或者电脑没电或者电脑突然崩溃,如果没有保存,只能说GG.好在VSCode有自动保存代码的功能,而且有好几种自动保存的模式选择,设置方法如下: 进入">文 ...
- jmeter针对websocket协议的压测
之前一直没有接触过websocket协议,所以一直对websocket的压测存在疑惑,在网上参考文章并不断尝试之后,终于有所得:第一次用jmeter的websoket插件,用的ws非加密协议,请求都能 ...
- 第一篇 -- XML基础
一.XML简介 XML是一种标记语言,用于描述数据,它提供一种标准化的方式来来表示文本数据.XML文档以.xml为后缀.需要彻底注意的是XML是区分大小写的. 先从一个简单的XML例子来了解下xml基 ...
- mocker-api 原理
项目网址:https://github.com/jaywcjlove/mocker-api 作用有2个: 运行dev命令后,访问本地开启服务接口,模拟数据: 访问本机接口时,代理到其它服务器,即调用其 ...
- 利用 subst.exe 可以将任意文件夹映射成盘符:
subst命令:将路径与驱动器号关联,即将一个目录当做一个磁盘驱动器来看: 假设:将E:\下的baidu文件夹设置成虚拟盘,虚拟盘的盘符为M. 1.点开始按钮,在运行框里输入 subst m: E:\ ...
- 小胖守皇宫(VIJOS P1144 )题解
题目描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步一哨,每 ...
- nginx 访问控制之 request_uri
$request_uri比$docuemnt_uri多了请求的参数. 主要用来针对请求的uri中的参数进行控制. 示例: if ($request_uri ~ "gid=\d{9,12}&q ...
- vue使用案例,vue初始化,vue初始化方法,vue条件语句,vue在js里面添加元素调用vue方法
<div id="main" > <button id='but1' type="button" v-on:click="save ...
- java基础之 内部类 & 嵌套类
参考文档: 内部类的应用场景 http://blog.csdn.net/hivon/article/details/606312 http://wwty.iteye.com/blog/338628 定 ...
- 关于conda和jupyter使用
conda建立虚拟环境 常用指令 conda env list # 查看环境 conda create -n env_name python=3.6.8 # 安装对应python的虚拟环境 conda ...