dotnet core 2.2开发项目中,常会使用Swagger UI来生成在线Api文档。
某些接口不想放到Swagger中可以这样写Filter:
/// /// 隐藏swagger接口特性标识
///
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)]
public partial class HiddenApiAttribute : System.Attribute { }
/// /// 隐藏接口,不生成到swagger文档展示
///
public class HiddenApiFilter : IDocumentFilter
{
/// /// 过滤器
///
///
///
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();
Controller类或某个方法加入代码:
[HiddenApi]
public class ValuesController : ControllerBase
{
...
}
显示枚举描述
/// /// Add enum value descriptions to Swagger
///
public class EnumDocumentFilter : IDocumentFilter
{
///
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(IEnumerableenums)
{
var enumDescriptions = new List();
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();
以上。