forked from AcaMate/AcaMate_API
92 lines
3.4 KiB
C#
92 lines
3.4 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.OpenApi.Models;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
|
public class CustomOperationAttribute : Attribute
|
|
{
|
|
public string Summary { get; }
|
|
public string Description { get; }
|
|
public string[] Tags { get; }
|
|
|
|
public CustomOperationAttribute(string summary, string description, params string[] tags)
|
|
{
|
|
Summary = summary;
|
|
Description = description;
|
|
Tags = tags;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public class CustomSwaggerOperationFilter : IOperationFilter
|
|
{
|
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
|
{
|
|
var customSwaggerAttribute = context.MethodInfo.GetCustomAttributes(typeof(CustomOperationAttribute), false)
|
|
.FirstOrDefault() as CustomOperationAttribute;
|
|
|
|
if (customSwaggerAttribute != null)
|
|
{
|
|
operation.Summary = customSwaggerAttribute.Summary;
|
|
operation.Description = customSwaggerAttribute.Description;
|
|
operation.Tags = customSwaggerAttribute.Tags
|
|
.Select(tag => new OpenApiTag { Name = tag })
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class SwaggerConfigure
|
|
{
|
|
private static OpenApiInfo DocName(string title, string version)
|
|
{
|
|
return new OpenApiInfo
|
|
{
|
|
Title = title,
|
|
Version = version
|
|
};
|
|
}
|
|
public static void AddCustomSwagger(this IServiceCollection services)
|
|
{
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
options.EnableAnnotations();
|
|
options.OperationFilter<CustomSwaggerOperationFilter>();
|
|
options.SwaggerDoc("전체", DocName("AcaMate 전체 API","1.0.0"));
|
|
options.SwaggerDoc("공통",DocName("공통 API", "1.0.0"));
|
|
options.SwaggerDoc("사업자 정보", DocName("사업자 정보 API", "1.0.0"));
|
|
options.SwaggerDoc("사용자", DocName("사용자 API", "1.0.0"));
|
|
|
|
options.DocInclusionPredicate((docName, apiDesc) =>
|
|
{
|
|
if (docName == "전체") return true; // 전체 문서에 모든 API 포함
|
|
if (docName == "공통" && apiDesc.GroupName == "공통") return true;
|
|
if (docName == "사업자 정보" && apiDesc.GroupName == "사업자 정보") return true;
|
|
if (docName == "사용자" && apiDesc.GroupName == "사용자") return true;
|
|
return false;
|
|
});
|
|
|
|
options.TagActionsBy(apiDesc => new[] { apiDesc.GroupName ?? "기타" });
|
|
|
|
|
|
// options.TagActionsBy(apiDesc => apiDesc.ActionDescriptor.EndpointMetadata
|
|
// .OfType<SwaggerOperationAttribute>()
|
|
// .FirstOrDefault()?.Tags ?? new[] { "기타" });
|
|
});
|
|
}
|
|
|
|
public static void UseCustomSwaggerUI(this IApplicationBuilder app)
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/전체/swagger.json", "전체 API");
|
|
options.SwaggerEndpoint("/swagger/공통/swagger.json", "공통 API");
|
|
options.SwaggerEndpoint("/swagger/사용자/swagger.json", "사용자 API");
|
|
options.SwaggerEndpoint("/swagger/사업자 정보/swagger.json", "사업자 정보 API");
|
|
});
|
|
}
|
|
} |