< Summary

Information
Class: Despesas.Backend.CommonDependenceInject.HideDtosFilter
Assembly: Despesas.Backend
File(s): /src/Despesas.Backend/CommonDependenceInject/SwaggerApiVersioningDependenceInject.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 116
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)

/src/Despesas.Backend/CommonDependenceInject/SwaggerApiVersioningDependenceInject.cs

#LineLine coverage
 1using Despesas.Application.Dtos.Abstractions;
 2using Despesas.Application.Dtos.Core;
 3using Microsoft.AspNetCore.Mvc;
 4using Microsoft.OpenApi.Models;
 5using Swashbuckle.AspNetCore.SwaggerGen;
 6using System.Reflection;
 7
 8namespace Despesas.Backend.CommonDependenceInject;
 9
 10public static class SwaggerApiVersioningDependenceInject
 11{
 12    private readonly static string appName = "API Balanço Positivo";
 13    private readonly static string currentVersion = "v3.0.0";
 14    private readonly static string appDescription = @$"
 15           A API Balanço Positivo fornece serviços para o gerenciamento de finanças pessoais de forma simples e objetiva
 16       gastos e receitas, consultar relatórios, obter gráficos consolidados e organizar informações financeiras de manei
 17           Essa API foi projetada para permitir que aplicações clientes — como aplicativos móveis ou sistemas web — aces
 18       e estruturada, auxiliando usuários a tomarem decisões mais conscientes e alcançarem a estabilidade financeira com
 19
 20    public static void AddSwaggerApiVersioning(this IServiceCollection services)
 21    {
 22        services.AddEndpointsApiExplorer();
 23
 24        services.AddSwaggerGen(c =>
 25        {
 26
 27            c.AddSecurityRequirement(new OpenApiSecurityRequirement
 28            {
 29                {
 30                    new OpenApiSecurityScheme
 31                    {
 32                        Scheme = "Bearer",
 33                        Reference = new OpenApiReference
 34                        {
 35                            Type = ReferenceType.SecurityScheme,
 36                            Id = "Bearer"
 37                        }
 38                    },
 39                    Array.Empty<string>()
 40                }
 41            });
 42
 43            c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
 44            {
 45                In = ParameterLocation.Header,
 46                Name = "Authorization",
 47                Type = SecuritySchemeType.ApiKey,
 48                Scheme = "Bearer",
 49                Description = "Adicione o token JWT (ex: Bearer {seu token})"
 50            });
 51            c.SchemaFilter<HideDtosFilter>();
 52
 53
 54            c.SwaggerDoc(currentVersion, new OpenApiInfo
 55            {
 56                Title = appName,
 57                Version = currentVersion,
 58                Description = appDescription,
 59                Contact = new OpenApiContact
 60                {
 61                    Name = "Projeto Balanço Pessoal  - HONEY TI",
 62                    Url = new Uri("https://github.com/alexribeirofaria/prj-despesas-pessoais")
 63                },
 64            });
 65
 66            c.DocInclusionPredicate((docName, apiDesc) =>
 67            {
 68                if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;
 69                var controllerNamespace = methodInfo?.DeclaringType?.Namespace;
 70
 71                if (docName == currentVersion)
 72                    return true;
 73
 74                return false;
 75            });
 76        });
 77    }
 78
 79    public static void AddSwaggerUIApiVersioning(this WebApplication app)
 80    {
 81        app.UseSwagger();
 82        app.UseSwaggerUI(c =>
 83        {
 84            string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
 85            c.SwaggerEndpoint(@$"{swaggerJsonBasePath}/swagger/{currentVersion}/swagger.json", $"{appName} ");
 86        });
 87    }
 88}
 89
 90internal class HideDtosFilter : ISchemaFilter
 91{
 92    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
 093    {
 094        if (context.Type == typeof(AuthenticationDto))
 095        {
 096            schema.Description = "DTO contendo informações de autenticação.";
 097            schema.Type = "object";
 098            schema.Properties.Clear();
 099        }
 100
 0101        if (context.Type == typeof(GoogleAuthenticationDto))
 0102        {
 0103            schema.Description = "DTO contendo informações de autenticação google.";
 0104            schema.Type = "object";
 0105            schema.Properties.Clear();
 0106        }
 107
 108
 0109        if (context.Type == typeof(ProblemDetails))
 0110        {
 0111            schema.Description = "Informações de erro retornado pela API.";
 0112            schema.Type = "object";
 0113            schema.Properties.Clear();
 0114        }
 0115    }
 116}