using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json.Nodes;
using Jellyfin.Data.Attributes;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Jellyfin.Server.Filters;
///
/// Filter to remove ignored enum values.
///
public class IgnoreEnumSchemaFilter : ISchemaFilter
{
///
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsEnum || (Nullable.GetUnderlyingType(context.Type)?.IsEnum ?? false))
{
var type = context.Type.IsEnum ? context.Type : Nullable.GetUnderlyingType(context.Type);
if (type is null)
{
return;
}
if (schema is not OpenApiSchema concreteSchema)
{
return;
}
var enumOpenApiNodes = new List();
foreach (var enumName in Enum.GetNames(type))
{
var member = type.GetMember(enumName)[0];
if (!member.GetCustomAttributes().Any())
{
enumOpenApiNodes.Add(JsonValue.Create(enumName)!);
}
}
concreteSchema.Enum = enumOpenApiNodes;
}
}
}