新增SwaggerDemo
This commit is contained in:
41
电子展板/Utility/Swagger/SwaggerModelDataUtils.cs
Normal file
41
电子展板/Utility/Swagger/SwaggerModelDataUtils.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user