新增SwaggerDemo

This commit is contained in:
2025-09-23 16:29:17 +08:00
parent dc42b56bf6
commit dad5b8fb89
27 changed files with 505 additions and 39 deletions

View 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;
}
});
}
}
}