| | | 1 | | using Anichron.API.Endpoints; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Microsoft.AspNetCore.OpenApi; |
| | | 4 | | using Microsoft.OpenApi; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | |
| | | 7 | | namespace Anichron.API.Infrastructure; |
| | | 8 | | |
| | | 9 | | /// <summary>Describes the DTO properties that send an enum's name, and the work items' query binds, by that enum's name |
| | | 10 | | internal sealed class EnumDocumentTransformer : IOpenApiDocumentTransformer |
| | | 11 | | { |
| | | 12 | | public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken ca |
| | 1 | 13 | | { |
| | 1 | 14 | | Name<WorkKind>(document, nameof(WorkItemDto), "kind"); |
| | 1 | 15 | | Name<WorkItemStatus>(document, nameof(WorkItemDto), "status"); |
| | 1 | 16 | | Name<PublishedMediaType>(document, nameof(AssetDto), "mediaType"); |
| | 1 | 17 | | return Task.CompletedTask; |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | private static void Name<TEnum>(OpenApiDocument document, string dto, string property) |
| | | 21 | | where TEnum : struct, Enum |
| | 3 | 22 | | { |
| | 3 | 23 | | var schemas = document.Components!.Schemas!; |
| | 3 | 24 | | var name = typeof(TEnum).Name; |
| | 3 | 25 | | schemas[name] = new OpenApiSchema |
| | 3 | 26 | | { |
| | 3 | 27 | | Type = JsonSchemaType.String, |
| | 3 | 28 | | Enum = [.. Enum.GetNames<TEnum>().Select(value => (JsonNode)value)], |
| | 3 | 29 | | }; |
| | 3 | 30 | | schemas[dto].Properties![property] = new OpenApiSchemaReference(name, document); |
| | 3 | 31 | | } |
| | | 32 | | } |