< Summary - Estud

Information
Class: Estud.Back.Configs.DocsConfigs
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Configs/DocsConfigs.cs
Tag: 114_29044117136
Line coverage
82%
Covered lines: 74
Uncovered lines: 16
Coverable lines: 90
Total lines: 116
Line coverage: 82.2%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddDocsConfigs(...)0%402875%
MapScalarDocs(...)100%11100%
UseDocs(...)100%11100%
ReadResource(...)100%11100%

File(s)

/home/runner/work/syki/syki/Back/Configs/DocsConfigs.cs

#LineLine coverage
 1using System.Reflection;
 2using Scalar.AspNetCore;
 3using Microsoft.OpenApi.Any;
 4using Estud.Back.Auth.Schemes;
 5using Microsoft.OpenApi.Models;
 6using Microsoft.OpenApi.Interfaces;
 7
 8namespace Estud.Back.Configs;
 9
 10public static class DocsConfigs
 11{
 12    public static void AddDocsConfigs(this WebApplicationBuilder builder)
 13    {
 214        builder.Services.AddSwaggerGen(options =>
 215        {
 216            options.SwaggerDoc("v1", new OpenApiInfo
 217            {
 218                Title = "Estud API Docs",
 219                Description = ReadResource("api-intro.md"),
 220                Extensions = new Dictionary<string, IOpenApiExtension>
 221                {
 222                    { "x-logo", new OpenApiObject
 223                    {
 224                        { "url", new OpenApiString("/estud-logo.png") },
 225                    }}
 226                },
 227            });
 228
 229            options.EnableAnnotations();
 230
 231            options.TagActionsBy(api =>
 232            {
 033                var group = api.RelativePath.Split("/")[0];
 034                if (group == "campi") return ["🏫 Campi"];
 035                if (group == "courses") return ["📚 Courses"];
 036                if (group == "course-curriculums") return ["📋 Curriculums"];
 037                if (group == "course-offerings") return ["🗓️ Offerings"];
 038                if (group == "disciplines") return ["📖 Disciplines"];
 039                if (group == "periods") return ["📅 Periods"];
 040                if (group == "students") return ["🎓 Students"];
 041                if (group == "teachers") return ["👨‍🏫 Teachers"];
 042                if (group == "identity") return ["🛡️ Identity"];
 043                if (group == "users") return ["👩🏻‍🎓 Users"];
 044                if (group == "webhooks") return ["🪝 Webhooks"];
 045                if (group == "classes") return ["👩🏻‍🏫 Classes"];
 046                if (group == "classrooms") return ["📈 Classrooms"];
 047                if (group == "notifications") return ["🔔 Notifications"];
 048                return ["🧱 Cross"];
 249            });
 250            options.DocInclusionPredicate((name, api) => true);
 251
 252            options.SchemaFilter<EnumSchemaFilter>();
 253            options.OperationFilter<AuthOperationsFilter>();
 254            options.OperationFilter<IdParameterExamplesFilter>();
 255            options.OperationFilter<ExamplesOperationsFilterFilter>();
 256            options.DocumentFilter<HttpMethodSorterDocumentFilter>();
 257
 258            options.ExampleFilters();
 259
 260            options.AddSecurityDefinition(
 261                JwtBearerScheme.Name,
 262                new OpenApiSecurityScheme
 263                {
 264                    In = ParameterLocation.Cookie,
 265                    Name = JwtBearerScheme.Cookie,
 266                    Type = SecuritySchemeType.ApiKey,
 267                    Description = "JWT enviado no cookie http only",
 268                });
 269
 270            options.DescribeAllParametersInCamelCase();
 271
 272            var xmlPath = Path.Combine(AppContext.BaseDirectory, "Back.xml");
 273            options.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
 474        });
 75
 276        builder.Services.AddSwaggerExamplesFromAssemblyOf(typeof(Program));
 277        builder.Services.AddOpenApi();
 278    }
 79
 80    public static void MapScalarDocs(this IEndpointRouteBuilder options)
 81    {
 282        options.MapScalarApiReference("/docs", options =>
 283        {
 284            options.WithModels(false);
 285            options.WithTitle("Estud API Docs");
 286            options.WithDocumentDownloadType(DocumentDownloadType.Json);
 287            options.WithOpenApiRoutePattern("/swagger/{documentName}/swagger.json");
 288            options
 289                .AddPreferredSecuritySchemes("Bearer")
 490                .AddHttpAuthentication("Bearer", x => x.Token = "your.bearer.token");
 291
 292            options.CustomCss = @"
 293                :root {
 294                    --scalar-sidebar-width: 300px;
 295                }
 296            ";
 497        });
 298    }
 99
 100    public static void UseDocs(this IApplicationBuilder app)
 101    {
 2102        app.UseStaticFiles();
 2103        app.UseSwagger();
 2104    }
 105
 106    private static string ReadResource(string name)
 107    {
 2108        var assembly = Assembly.GetExecutingAssembly();
 10109        var resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(name));
 110
 2111        using Stream stream = assembly.GetManifestResourceStream(resourcePath)!;
 2112        using StreamReader reader = new(stream);
 113
 2114        return reader.ReadToEnd();
 2115    }
 116}