| | | 1 | | using Microsoft.AspNetCore.Mvc; |
| | | 2 | | using Microsoft.AspNetCore.OpenApi; |
| | | 3 | | using Microsoft.OpenApi; |
| | | 4 | | using System.Text.Json.Nodes; |
| | | 5 | | |
| | | 6 | | namespace Anichron.API.Infrastructure; |
| | | 7 | | |
| | | 8 | | /// <summary>Describes the <c>code</c> extension that the API's own problems carry, and each field error, as an <c>Error |
| | | 9 | | internal sealed class ProblemCodeDocumentTransformer : IOpenApiDocumentTransformer |
| | | 10 | | { |
| | | 11 | | private const string errorCodeSchema = "ErrorCode"; |
| | | 12 | | private const string errorsProperty = "errors"; |
| | | 13 | | |
| | | 14 | | public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken ca |
| | 1 | 15 | | { |
| | 1 | 16 | | var schemas = document.Components!.Schemas!; |
| | 1 | 17 | | document.AddComponent(errorCodeSchema, ErrorCode()); |
| | 7 | 18 | | foreach (var name in new[] { nameof(ProblemDetails), nameof(HttpValidationProblemDetails) }) |
| | 2 | 19 | | schemas[name].Properties![ApiProblems.CodeExtension] = new OpenApiSchemaReference(errorCodeSchema, document) |
| | | 20 | | |
| | 1 | 21 | | schemas[nameof(HttpValidationProblemDetails)].Properties![errorsProperty] = new OpenApiSchema |
| | 1 | 22 | | { |
| | 1 | 23 | | Type = JsonSchemaType.Object, |
| | 1 | 24 | | AdditionalProperties = new OpenApiSchema |
| | 1 | 25 | | { |
| | 1 | 26 | | Type = JsonSchemaType.Array, |
| | 1 | 27 | | Items = new OpenApiSchemaReference(errorCodeSchema, document), |
| | 1 | 28 | | }, |
| | 1 | 29 | | }; |
| | | 30 | | |
| | 1 | 31 | | return Task.CompletedTask; |
| | 1 | 32 | | } |
| | | 33 | | |
| | | 34 | | private static OpenApiSchema ErrorCode() |
| | 1 | 35 | | => new() |
| | 1 | 36 | | { |
| | 1 | 37 | | Type = JsonSchemaType.String, |
| | 1 | 38 | | Enum = [.. ErrorCodes.All().Select(code => (JsonNode)code)], |
| | 1 | 39 | | }; |
| | | 40 | | } |