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做代理实 ...
随机推荐
- 使用Python音频双通道分离
某些音频是双方对话,有可能需要对音频作通道的分离. 示例代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- """ 音 ...
- 从Seq2seq到Attention模型到Self Attention
Seq2seq Seq2seq全名是Sequence-to-sequence,也就是从序列到序列的过程,是近年当红的模型之一.Seq2seq被广泛应用在机器翻译.聊天机器人甚至是图像生成文字等情境. ...
- CentOS 7.2搭建FastDFS 分布式文件系统,实现高可用集群
分布式集群搭建结构 双Tracker 2组Group 轮询存储策略 Keepalived+Nginx高可用 Nginx缓存 4个存储节点 一. 集群规划清单 1.安装清单 软件名称 版本 百度云盘存放 ...
- HelloPlatform
#include <stdio.h> int main() { #if _PLATFORM_ == _PLATFORM_TRU64 printf("Hello _PLATFORM ...
- 瀑布流插件|jquery.masonry|使用demo
Maonsry+Infinite-Scroll实现滚动式分页,网上有很多,这里只说: 瀑布流插件的一个基本使用,附上基本功能的demo <html> <head> <me ...
- 什么叫pure function(纯函数)
(来自:http://en.wikipedia.org/wiki/Pure_function) 在计算机编程中,假如满足下面这两个句子的约束,一个函数可能被描述为一个纯函数: 给出同样的参数值,该函数 ...
- Arduino通过串口监视器收发数据
在串口监视器中发送数据,板子收到数据并打印出来. 不需要额外电路,但是板子必须连接电脑,Arduino IDE的串口监视器也需要被打开. 代码 /* 串口事件 当新的串口数据到来时,我们会将它添加到一 ...
- NYOJ469 - 擅长排列的小明 II - (dp)
题目描述: 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个正整数n,序列1,2,3,4,5......n满足以下情况的排列: 1.第一个数必须是1 2.相邻两个数之差不 ...
- 2.Vue.js 是什么
Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架. 与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用. Vue 的核心库只关注视图层,不仅易于上手,还便 ...
- 文本编辑器vim——三种模式、显示行号、插入命令、行快速定位、行内定位
1.vim的三种工作模式: (1)利用vim命令新建文件: 点击entre键执行命令后,开始向文本中输入想要写入的内容: (2)命令行模式(ESC): 不管用户处于何种模式,只要单击Esc键,即可进入 ...