42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using Nancy.Swagger;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace 电子展板.Utility.Swagger
|
|
{
|
|
public static class SwaggerModelDataUtils
|
|
{
|
|
public static SwaggerModelData GetModelData<T>()
|
|
{
|
|
return SwaggerModelData.ForType<T>(with =>
|
|
{
|
|
Type type = typeof(T);
|
|
SwaggerClassCommentAttribute classComment = type.GetCustomAttribute<SwaggerClassCommentAttribute>();
|
|
with.Description(classComment != null ? classComment.Comment : type.Name);
|
|
PropertyInfo[] propertyInfos = typeof(T).GetProperties();
|
|
foreach (PropertyInfo propertyInfo in propertyInfos)
|
|
{
|
|
SwaggerModelPropertyData ss = with.Data.Properties.Where(x => x.Name == propertyInfo.Name).FirstOrDefault();
|
|
SwaggerCommentAttribute attribute = propertyInfo.GetCustomAttribute<SwaggerCommentAttribute>();
|
|
if (attribute == null)
|
|
{
|
|
continue;
|
|
}
|
|
if (!string.IsNullOrEmpty(attribute.Comment))
|
|
ss.Description = attribute.Comment;
|
|
if (attribute.IsRequired != null)
|
|
ss.Required = attribute.IsRequired.Value;
|
|
if (attribute.Min != null)
|
|
ss.Minimum = attribute.Min.Value;
|
|
if (attribute.Max != null)
|
|
ss.Maximum = attribute.Max.Value;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|