diff options
| author | Cody Robibero <cody@robibe.ro> | 2023-11-09 14:00:29 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-09 14:00:29 -0700 |
| commit | 906f701fa81c7cde1a9a01b066f3f29ff98552a5 (patch) | |
| tree | efc92ef0ecf4e535a519de4f8e950a77bb2ad5c3 /Jellyfin.Server/Filters | |
| parent | c7a94d48ae019f41d5f06340bca7efe0788ad5ad (diff) | |
Convert CollectionType, SpecialFolderType to enum (#9764)
* 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>
Diffstat (limited to 'Jellyfin.Server/Filters')
| -rw-r--r-- | Jellyfin.Server/Filters/IgnoreEnumSchemaFilter.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Jellyfin.Server/Filters/IgnoreEnumSchemaFilter.cs b/Jellyfin.Server/Filters/IgnoreEnumSchemaFilter.cs new file mode 100644 index 000000000..eb9ad03c2 --- /dev/null +++ b/Jellyfin.Server/Filters/IgnoreEnumSchemaFilter.cs @@ -0,0 +1,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; + } + } +} |
