< Summary

Information
Class: TicTacToeApp.API.Filters.ErrorResponseSchemaFilter
Assembly: TicTacToeApp.API
File(s): /home/runner/work/TicTacToe/TicTacToe/TicTacToeApp.API/Filters/ErrorResponseFilter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 8
Coverable lines: 8
Total lines: 53
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Apply(...)0%620%

File(s)

/home/runner/work/TicTacToe/TicTacToe/TicTacToeApp.API/Filters/ErrorResponseFilter.cs

#LineLine coverage
 1using Microsoft.OpenApi.Any;
 2using Microsoft.OpenApi.Models;
 3using Swashbuckle.AspNetCore.SwaggerGen;
 4using TicTacToeApp.API.Response;
 5
 6namespace TicTacToeApp.API.Filters;
 7
 8// Фильтр для добавления примеров ErrorResponse
 9public class ErrorResponseSchemaFilter : ISchemaFilter
 10{
 11    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
 12    {
 013        if (context.Type == typeof(ErrorResponse))
 14        {
 015            schema.Example = new OpenApiObject
 016            {
 017                ["statusCode"] = new OpenApiString("400"),
 018                ["message"] = new OpenApiString("Invalid request"),
 019                ["detail"] = new OpenApiString("The 'id' parameter must be a positive number")
 020            };
 21        }
 022    }
 23}
 24
 25
 26public class ErrorResponseOperationFilter : IOperationFilter
 27{
 28    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 29    {
 30        var statusCodes = new Dictionary<string, (string Message, string Detail)>
 31        {
 32            ["400"] = ("Invalid request", "The 'id' parameter must be a positive number"),
 33            ["404"] = ("Resource not found", "The requested resource was not found"),
 34            ["409"] = ("Conflict", "Поле занято"),
 35            ["412"] = ("Precondition failed", "Несогласованное состояние объекта Game"),
 36            ["500"] = ("Internal server error", "An unexpected error occurred")
 37        };
 38
 39        foreach (var (code, (message, detail)) in statusCodes)
 40        {
 41            if (operation.Responses.TryGetValue(code, out var response) &&
 42                response.Content.TryGetValue("application/json", out var mediaType))
 43            {
 44                mediaType.Example = new OpenApiObject
 45                {
 46                    ["statusCode"] = new OpenApiInteger(int.Parse(code)),
 47                    ["message"] = new OpenApiString(message),
 48                    ["detail"] = new OpenApiString(detail)
 49                };
 50            }
 51        }
 52    }
 53}