using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace JiShe.CollectBus.Host.Swaggers
{
    /// <summary>
    /// swagger 枚举映射，
    /// 原因：前端代理生成枚举是数字
    /// </summary>
    public class EnumSchemaFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (!context.Type.IsEnum)
                return;
            OpenApiArray openApiArray = new OpenApiArray();
            openApiArray.AddRange((IEnumerable<IOpenApiAny>)Enum.GetNames(context.Type).Select(n => new OpenApiString(n)));
            schema.Extensions.Add("x-enumNames", openApiArray);
            schema.Extensions.Add("x-enum-varnames", openApiArray);
        }
    }
}
