| | |
| | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel.DataAnnotations; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | |
| | | |
| | | // 设置最终的 OperationId |
| | | operation.OperationId = $"{controllerName}-{actionName}"; |
| | | |
| | | // 获取接口方法的返回类型(可能是 Task<ApiResult<T>> 或 ApiResult<T>) |
| | | var returnType = context.MethodInfo.ReturnType; |
| | | |
| | | // 步骤 1:如果是 Task 或 Task<ApiResult<T>>,提取内部类型 |
| | | if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>)) |
| | | { |
| | | returnType = returnType.GetGenericArguments()[0]; // 提取 Task<T> 中的 T |
| | | } |
| | | |
| | | // 步骤 3:替换 Swagger 响应类型为 Data 的类型 |
| | | operation.Responses.Clear(); |
| | | operation.Responses.Add("200", new OpenApiResponse |
| | | { |
| | | Description = "操作成功", |
| | | Content = new Dictionary<string, OpenApiMediaType> |
| | | { |
| | | ["application/json"] = new OpenApiMediaType |
| | | { |
| | | Schema = context.SchemaGenerator.GenerateSchema(returnType, context.SchemaRepository) |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | } |