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>();
以上。