< Summary

Information
Class: TicTacToeApp.API.Filters.ErrorResponseOperationFilter
Assembly: TicTacToeApp.API
File(s): /home/runner/work/TicTacToe/TicTacToe/TicTacToeApp.API/Filters/ErrorResponseFilter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 53
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%4260%

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    {
 13        if (context.Type == typeof(ErrorResponse))
 14        {
 15            schema.Example = new OpenApiObject
 16            {
 17                ["statusCode"] = new OpenApiString("400"),
 18                ["message"] = new OpenApiString("Invalid request"),
 19                ["detail"] = new OpenApiString("The 'id' parameter must be a positive number")
 20            };
 21        }
 22    }
 23}
 24
 25
 26public class ErrorResponseOperationFilter : IOperationFilter
 27{
 28    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 29    {
 030        var statusCodes = new Dictionary<string, (string Message, string Detail)>
 031        {
 032            ["400"] = ("Invalid request", "The 'id' parameter must be a positive number"),
 033            ["404"] = ("Resource not found", "The requested resource was not found"),
 034            ["409"] = ("Conflict", "Поле занято"),
 035            ["412"] = ("Precondition failed", "Несогласованное состояние объекта Game"),
 036            ["500"] = ("Internal server error", "An unexpected error occurred")
 037        };
 38
 039        foreach (var (code, (message, detail)) in statusCodes)
 40        {
 041            if (operation.Responses.TryGetValue(code, out var response) &&
 042                response.Content.TryGetValue("application/json", out var mediaType))
 43            {
 044                mediaType.Example = new OpenApiObject
 045                {
 046                    ["statusCode"] = new OpenApiInteger(int.Parse(code)),
 047                    ["message"] = new OpenApiString(message),
 048                    ["detail"] = new OpenApiString(detail)
 049                };
 50            }
 51        }
 052    }
 53}