aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Filters/IgnoreEnumSchemaFilter.cs
blob: eb9ad03c2121d2733edbe635e8fe360a01276cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Jellyfin.Data.Attributes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Jellyfin.Server.Filters;

/// <summary>
/// Filter to remove ignored enum values.
/// </summary>
public class IgnoreEnumSchemaFilter : ISchemaFilter
{
    /// <inheritdoc />
    public void Apply(OpenApiSchema 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;
            }

            var enumOpenApiStrings = new List<IOpenApiAny>();

            foreach (var enumName in Enum.GetNames(type))
            {
                var member = type.GetMember(enumName)[0];
                if (!member.GetCustomAttributes<OpenApiIgnoreEnumAttribute>().Any())
                {
                    enumOpenApiStrings.Add(new OpenApiString(enumName));
                }
            }

            schema.Enum = enumOpenApiStrings;
        }
    }
}