< Summary

Information
Class: Despesas.Backend.CommonDependenceInject.SwaggerApiVersioningDependenceInject
Assembly: Despesas.Backend
File(s): /src/Despesas.Backend/CommonDependenceInject/SwaggerApiVersioningDependenceInject.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 71
Coverable lines: 71
Total lines: 116
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
AddSwaggerApiVersioning(...)0%7280%
AddSwaggerUIApiVersioning(...)0%620%

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{
 012    private readonly static string appName = "API Balanço Positivo";
 013    private readonly static string currentVersion = "v3.0.0";
 014    private readonly static string appDescription = @$"
 015           A API Balanço Positivo fornece serviços para o gerenciamento de finanças pessoais de forma simples e objetiva
 016       gastos e receitas, consultar relatórios, obter gráficos consolidados e organizar informações financeiras de manei
 017           Essa API foi projetada para permitir que aplicações clientes — como aplicativos móveis ou sistemas web — aces
 018       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)
 021    {
 022        services.AddEndpointsApiExplorer();
 23
 024        services.AddSwaggerGen(c =>
 025        {
 026
 027            c.AddSecurityRequirement(new OpenApiSecurityRequirement
 028            {
 029                {
 030                    new OpenApiSecurityScheme
 031                    {
 032                        Scheme = "Bearer",
 033                        Reference = new OpenApiReference
 034                        {
 035                            Type = ReferenceType.SecurityScheme,
 036                            Id = "Bearer"
 037                        }
 038                    },
 039                    Array.Empty<string>()
 040                }
 041            });
 042
 043            c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
 044            {
 045                In = ParameterLocation.Header,
 046                Name = "Authorization",
 047                Type = SecuritySchemeType.ApiKey,
 048                Scheme = "Bearer",
 049                Description = "Adicione o token JWT (ex: Bearer {seu token})"
 050            });
 051            c.SchemaFilter<HideDtosFilter>();
 052
 053
 054            c.SwaggerDoc(currentVersion, new OpenApiInfo
 055            {
 056                Title = appName,
 057                Version = currentVersion,
 058                Description = appDescription,
 059                Contact = new OpenApiContact
 060                {
 061                    Name = "Projeto Balanço Pessoal  - HONEY TI",
 062                    Url = new Uri("https://github.com/alexribeirofaria/prj-despesas-pessoais")
 063                },
 064            });
 065
 066            c.DocInclusionPredicate((docName, apiDesc) =>
 067            {
 068                if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;
 069                var controllerNamespace = methodInfo?.DeclaringType?.Namespace;
 070
 071                if (docName == currentVersion)
 072                    return true;
 073
 074                return false;
 075            });
 076        });
 077    }
 78
 79    public static void AddSwaggerUIApiVersioning(this WebApplication app)
 080    {
 081        app.UseSwagger();
 082        app.UseSwaggerUI(c =>
 083        {
 084            string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
 085            c.SwaggerEndpoint(@$"{swaggerJsonBasePath}/swagger/{currentVersion}/swagger.json", $"{appName} ");
 086        });
 087    }
 88}
 89
 90internal class HideDtosFilter : ISchemaFilter
 91{
 92    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
 93    {
 94        if (context.Type == typeof(AuthenticationDto))
 95        {
 96            schema.Description = "DTO contendo informações de autenticação.";
 97            schema.Type = "object";
 98            schema.Properties.Clear();
 99        }
 100
 101        if (context.Type == typeof(GoogleAuthenticationDto))
 102        {
 103            schema.Description = "DTO contendo informações de autenticação google.";
 104            schema.Type = "object";
 105            schema.Properties.Clear();
 106        }
 107
 108
 109        if (context.Type == typeof(ProblemDetails))
 110        {
 111            schema.Description = "Informações de erro retornado pela API.";
 112            schema.Type = "object";
 113            schema.Properties.Clear();
 114        }
 115    }
 116}