| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Reflection; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | using Jellyfin.Data.Attributes; |
| | | 7 | | using Microsoft.OpenApi; |
| | | 8 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | | 9 | | |
| | | 10 | | namespace Jellyfin.Server.Filters; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Filter to remove ignored enum values. |
| | | 14 | | /// </summary> |
| | | 15 | | public class IgnoreEnumSchemaFilter : ISchemaFilter |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public void Apply(IOpenApiSchema schema, SchemaFilterContext context) |
| | | 19 | | { |
| | 5077 | 20 | | if (context.Type.IsEnum || (Nullable.GetUnderlyingType(context.Type)?.IsEnum ?? false)) |
| | | 21 | | { |
| | 356 | 22 | | var type = context.Type.IsEnum ? context.Type : Nullable.GetUnderlyingType(context.Type); |
| | 356 | 23 | | if (type is null) |
| | | 24 | | { |
| | 0 | 25 | | return; |
| | | 26 | | } |
| | | 27 | | |
| | 356 | 28 | | if (schema is not OpenApiSchema concreteSchema) |
| | | 29 | | { |
| | 0 | 30 | | return; |
| | | 31 | | } |
| | | 32 | | |
| | 356 | 33 | | var enumOpenApiNodes = new List<JsonNode>(); |
| | | 34 | | |
| | 8030 | 35 | | foreach (var enumName in Enum.GetNames(type)) |
| | | 36 | | { |
| | 3659 | 37 | | var member = type.GetMember(enumName)[0]; |
| | 3659 | 38 | | if (!member.GetCustomAttributes<OpenApiIgnoreEnumAttribute>().Any()) |
| | | 39 | | { |
| | 3599 | 40 | | enumOpenApiNodes.Add(JsonValue.Create(enumName)!); |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 356 | 44 | | concreteSchema.Enum = enumOpenApiNodes; |
| | | 45 | | } |
| | 5077 | 46 | | } |
| | | 47 | | } |