mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 13:57:14 -04:00
* Convert CollectionType, SpecialFolderType to enum * Hide internal enum CollectionType values * Apply suggestions from code review Co-authored-by: Shadowghost <Shadowghost@users.noreply.github.com> * Fix recent change * Update Jellyfin.Data/Attributes/OpenApiIgnoreEnumAttribute.cs Co-authored-by: Patrick Barron <barronpm@gmail.com> --------- Co-authored-by: Shadowghost <Shadowghost@users.noreply.github.com> Co-authored-by: Patrick Barron <barronpm@gmail.com>
43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|