dotnet core swagger filter 隐藏接口和显示枚举描述
dotnet core 2.2开发项目中,常会使用Swagger UI来生成在线Api文档。
某些接口不想放到Swagger中可以这样写Filter:
- /// <summary>
- /// 隐藏swagger接口特性标识
- /// </summary>
- [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)]
- public partial class HiddenApiAttribute : System.Attribute { }
- /// <summary>
- /// 隐藏接口,不生成到swagger文档展示
- /// </summary>
- public class HiddenApiFilter : IDocumentFilter
- {
- /// <summary>
- /// 过滤器
- /// </summary>
- /// <param name="swaggerDoc"></param>
- /// <param name="context"></param>
- public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
- {
- foreach (ApiDescription apiDescription in context.ApiDescriptions)
- {
- if (apiDescription.TryGetMethodInfo(out MethodInfo method))
- {
- if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute))
- || method.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute)))
- {
- string key = "/" + apiDescription.RelativePath;
- if (key.Contains("?"))
- {
- int idx = key.IndexOf("?", System.StringComparison.Ordinal);
- key = key.Substring(0, idx);
- }
- swaggerDoc.Paths.Remove(key);
- }
- }
- }
- }
- }
注意:他不能隐藏一个Controller中的某个Method,比如仅隐藏Post或Get方法,因为它是对路由Paths.Remove
Starts.cs中加入代码:
- c.DocumentFilter<HiddenApiFilter>();
Controller类或某个方法加入代码:
- [HiddenApi]
- public class ValuesController : ControllerBase
- {
- ...
- }
显示枚举描述
- /// <summary>
- /// Add enum value descriptions to Swagger
- /// </summary>
- public class EnumDocumentFilter : IDocumentFilter
- {
- /// <inheritdoc />
- public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
- {
- // add enum descriptions to result models
- foreach (var schemaDictionaryItem in swaggerDoc.Definitions)
- {
- var schema = schemaDictionaryItem.Value;
- foreach (var propertyDictionaryItem in schema.Properties)
- {
- var property = propertyDictionaryItem.Value;
- var propertyEnums = property.Enum;
- if (propertyEnums != null && propertyEnums.Count > 0)
- {
- property.Description += DescribeEnum(propertyEnums);
- }
- }
- }
- }
- private static string DescribeEnum(IEnumerable<object> enums)
- {
- var enumDescriptions = new List<string>();
- Type type = null;
- foreach (var enumOption in enums)
- {
- if (type == null) type = enumOption.GetType();
- enumDescriptions.Add($"{Convert.ChangeType(enumOption, type.GetEnumUnderlyingType())} = {Enum.GetName(type, enumOption)},{GetDescription(type, enumOption)}");
- }
- return $"{Environment.NewLine}{string.Join(Environment.NewLine, enumDescriptions)}";
- }
- public 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;
- }
- }
Starts.cs中加入代码:
- c.DocumentFilter<EnumDocumentFilter>();
以上。
dotnet core swagger filter 隐藏接口和显示枚举描述的更多相关文章
- .Net core 下Swagger如何隐藏接口的显示
Swagger是这个非常强大的api文档工具,通常可以用来测试接口,和查看接口,就像这样: 非常的好用和快捷,这是一个小小的demo,我们在完成系统时,发布后,外部依旧可以用/swagger访问到这个 ...
- dotnet core 3.0 swagger 显示枚举描述
上一篇net core 2.2 swagger的枚举描述,core 3.0 需要升级swagger到5.0rc版,配置需要做些修改,swaager启用了OpenApi标准,之前的枚举描述方法也失效了. ...
- dotnet Core 调用HTTP接口,系统大量CLOSE_WAIT连接问题的分析,尚未解决。
环境: dotnet core 1.0.1 CentOS 7.2 今天在服务器巡检的时候,发现一个服务大量抛出异常 异常信息为: LockStatusPushError&&Messag ...
- dotnet core 通过修改文件头的方式隐藏控制台窗口
原文:dotnet core 通过修改文件头的方式隐藏控制台窗口 在带界面的 dotnet core 程序运行的时候就会出现一个控制台窗口,本文告诉大家使用最简单方法去隐藏控制台窗口. 最近在使用 A ...
- .Net WebApi接口之Swagger UI 隐藏指定接口类或方法
swagger的一个最大的优点是能实时同步api与文档,但有些时候我们不想全部公开接口,而要隐藏或屏蔽一些接口类或方法,swagger也是支持的,只需要设置一下DocumentFilter方法. 第一 ...
- dotnet core 隐藏控制台
如果写一个控制台程序,需要隐藏这个控制台程序,可以使用本文的方法 如果是在 Windows 下运行, 可以使用一些系统提供的方法隐藏控制台.如果是 Linux 下,都是控制台,就不用隐藏了 复制下面的 ...
- 将app接口服务器改为dotnet core承载
昨天我的一个 app 的接口服务器挂掉了,国外的小鸡意外的翻车,连同程序和数据一起,猝不及防.我的服务端程序是 asp.net mvc ,小鸡是 256 M 的内存跑不了 windows 系统,装的 ...
- 【Core Swagger】.NET Core中使用swagger
一.入门 https://www.nuget.org/packages/Swashbuckle.AspNetCore.SwaggerGen/ 1.添加核心NUGET包 Swashbuckle.AspN ...
- swagger webapi控制器注释不显示
swagger是webapi文档描述及调试工具,要在asp.net mvc中使用swagger,需要安装Swashbuckle.Core这个包,安装好后会在app_start中生成SwaggerCon ...
随机推荐
- C#和PHP加密结果一致的DES加密解密算法。php实现和c#一致的DES加密解密
DES加密算法 des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密 ...
- JAVA之Socket通讯
Server.java: Client.java Server console:(先启动服务器,再启动客户端) 服务器读取了客户端发来的hello server: Client console:客户 ...
- httprunner学习11-辅助函数debugtalk.py
前言 在httprunner里面,每个 YAML / JSON 文件的脚本都是独立运行的,有时候我们希望能跨文件使用公用的参数. 比如登录生成一个token,后面的用例都可以去引用这个token值,或 ...
- 斐波那契数性质 gcd(F[n],F[m])=F[gcd(n,m)]
引理1 结论: \[F(n)=F(m)F(n-m+1)+F(m-1)F(n-m)\] 推导: \[ \begin{aligned} F(n) &= F(n-1)+F(n-2) \\ & ...
- Python基础知识笔记-作用域
Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的. 变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python的作用域一共有4种,分别是: ...
- new.target元属性 | 分别用es5、es6 判断一个函数是否使用new操作符
函数内部有两个方法 [[call]] 和 [[construct]] (箭头函数没有这个方法),当使用new 操作符时, 函数内部调用 [[construct]], 创建一个新实例,this指向这个实 ...
- Gym100676 H. Capital City
感觉题目都已经快把正解给说出来了...strongly connected的两个点的消耗为0,其实就是同一个边双连通分量里面的点消耗为0.然后缩一下点,再树形DP一下就完了.第一次写边双,但感觉挺简单 ...
- js字符串转换为JSON
1. json字符串 jsStr = “{"a":'xxx', "b":'yyy'}” JSON.parse(jsStr); //可以将json字符串转换成j ...
- Numpy | 01 简介
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...
- GoCN每日新闻(2019-10-27)
GoCN每日新闻(2019-10-27) 1. Golab(意大利GopherCon)2019见闻 http://fedepaol.github.io/blog/2019/10/23/golab-20 ...