| | 1 | | using Despesas.Application.Dtos.Abstractions; |
| | 2 | | using Despesas.Application.Dtos.Core; |
| | 3 | | using Microsoft.AspNetCore.Mvc; |
| | 4 | | using Microsoft.OpenApi.Models; |
| | 5 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 6 | | using System.Reflection; |
| | 7 | |
|
| | 8 | | namespace Despesas.Backend.CommonDependenceInject; |
| | 9 | |
|
| | 10 | | public 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 | |
|
| | 90 | | internal class HideDtosFilter : ISchemaFilter |
| | 91 | | { |
| | 92 | | public void Apply(OpenApiSchema schema, SchemaFilterContext context) |
| 0 | 93 | | { |
| 0 | 94 | | if (context.Type == typeof(AuthenticationDto)) |
| 0 | 95 | | { |
| 0 | 96 | | schema.Description = "DTO contendo informações de autenticação."; |
| 0 | 97 | | schema.Type = "object"; |
| 0 | 98 | | schema.Properties.Clear(); |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | if (context.Type == typeof(GoogleAuthenticationDto)) |
| 0 | 102 | | { |
| 0 | 103 | | schema.Description = "DTO contendo informações de autenticação google."; |
| 0 | 104 | | schema.Type = "object"; |
| 0 | 105 | | schema.Properties.Clear(); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | |
|
| 0 | 109 | | if (context.Type == typeof(ProblemDetails)) |
| 0 | 110 | | { |
| 0 | 111 | | schema.Description = "Informações de erro retornado pela API."; |
| 0 | 112 | | schema.Type = "object"; |
| 0 | 113 | | schema.Properties.Clear(); |
| 0 | 114 | | } |
| 0 | 115 | | } |
| | 116 | | } |