< Summary

Information
Class: ClutterStock.Api.Filters.GlobalResponsesOperationFilter
Assembly: Api
File(s): /home/runner/work/ClutterStock/ClutterStock/backend/src/Api/Filters/GlobalResponsesOperationFilter.cs
Tag: 58_25416222083
Line coverage
0%
Covered lines: 0
Uncovered lines: 57
Coverable lines: 57
Total lines: 78
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%2040%

File(s)

/home/runner/work/ClutterStock/ClutterStock/backend/src/Api/Filters/GlobalResponsesOperationFilter.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Http;
 3using Microsoft.AspNetCore.Mvc;
 4using Microsoft.OpenApi;
 5using Swashbuckle.AspNetCore.SwaggerGen;
 6
 7namespace ClutterStock.Api.Filters;
 8
 9internal sealed class GlobalResponsesOperationFilter : IOperationFilter
 10{
 11    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 12    {
 013        if (operation.Responses is not { } responses)
 014            operation.Responses = responses = new OpenApiResponses();
 15
 016        var problemSchema = context.SchemaGenerator.GenerateSchema(typeof(ProblemDetails), context.SchemaRepository);
 017        var validationSchema = context.SchemaGenerator.GenerateSchema(typeof(HttpValidationProblemDetails), context.Sche
 18
 019        responses.TryAdd("400",
 020                         new OpenApiResponse
 021                         {
 022                             Description = "Bad Request",
 023                             Content = new Dictionary<string, OpenApiMediaType>
 024                             {
 025                                 ["application/problem+json"] = new()
 026                                 {
 027                                     Schema = validationSchema
 028                                 }
 029                             }
 030                         });
 31
 032        var allowAnonymous = context.ApiDescription.ActionDescriptor.EndpointMetadata
 033                                    .OfType<IAllowAnonymous>()
 034                                    .Any();
 35
 036        if (!allowAnonymous)
 37        {
 038            responses.TryAdd("401",
 039                             new OpenApiResponse
 040                             {
 041                                 Description = "Unauthorized",
 042                                 Content = new Dictionary<string, OpenApiMediaType>
 043                                 {
 044                                     ["application/problem+json"] = new()
 045                                     {
 046                                         Schema = problemSchema
 047                                     }
 048                                 }
 049                             });
 50
 051            responses.TryAdd("403",
 052                             new OpenApiResponse
 053                             {
 054                                 Description = "Forbidden",
 055                                 Content = new Dictionary<string, OpenApiMediaType>
 056                                 {
 057                                     ["application/problem+json"] = new()
 058                                     {
 059                                         Schema = problemSchema
 060                                     }
 061                                 }
 062                             });
 63        }
 64
 065        responses.TryAdd("500",
 066                         new OpenApiResponse
 067                         {
 068                             Description = "Server Error",
 069                             Content = new Dictionary<string, OpenApiMediaType>
 070                             {
 071                                 ["application/problem+json"] = new()
 072                                 {
 073                                     Schema = problemSchema
 074                                 }
 075                             }
 076                         });
 077    }
 78}