< Summary

Information
Class: Syki.Back.Configs.DocsConfigs
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Configs/DocsConfigs.cs
Tag: 22_11348620282
Line coverage
100%
Covered lines: 69
Uncovered lines: 0
Coverable lines: 69
Total lines: 92
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddDocsConfigs(...)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 Microsoft.OpenApi.Any;
 3using Microsoft.OpenApi.Models;
 4using Microsoft.OpenApi.Interfaces;
 5
 6namespace Syki.Back.Configs;
 7
 8public static class DocsConfigs
 9{
 10    public static void AddDocsConfigs(this IServiceCollection services)
 11    {
 112        services.AddSwaggerGen(options =>
 113        {
 114            options.SwaggerDoc("v1", new OpenApiInfo
 115            {
 116                Description = ReadResource("api-intro.md"),
 117                Extensions = new Dictionary<string, IOpenApiExtension>
 118                {
 119                    { "x-logo", new OpenApiObject
 120                    {
 121                        { "url", new OpenApiString("/syki-logo.png") },
 122                    }}
 123                },
 124            });
 125
 126            options.ExampleFilters();
 127
 128            options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
 129            {
 130                In = ParameterLocation.Header,
 131                Description = "Please enter a valid token",
 132                Name = "Authorization",
 133                Type = SecuritySchemeType.Http,
 134                BearerFormat = "JWT",
 135                Scheme = "Bearer"
 136            });
 137            options.AddSecurityRequirement(new OpenApiSecurityRequirement
 138            {
 139                {
 140                    new OpenApiSecurityScheme
 141                    {
 142                        Reference = new OpenApiReference
 143                        {
 144                            Type = ReferenceType.SecurityScheme,
 145                            Id = "Bearer"
 146                        }
 147                    },
 148                    new string[] {}
 149                }
 150            });
 151
 152            options.DescribeAllParametersInCamelCase();
 153
 154            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
 155            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
 156            options.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
 257        });
 58
 159        services.AddSwaggerExamplesFromAssemblyOf(typeof(Program));
 160    }
 61
 62    public static void UseDocs(this IApplicationBuilder app)
 63    {
 164        app.UseStaticFiles();
 65
 166        app.UseSwagger();
 167        app.UseSwaggerUI(options =>
 168        {
 169            options.DocumentTitle = "Syki API";
 170            options.SwaggerEndpoint("/swagger/v1/swagger.json", "Syki 1.0");
 171            options.DefaultModelsExpandDepth(-1);
 272        });
 73
 174        app.UseReDoc(c =>
 175        {
 176            c.RoutePrefix = "docs";
 177            c.DocumentTitle = "Syki API";
 178            c.SpecUrl = "/swagger/v1/swagger.json";
 279        });
 180    }
 81
 82    public static string ReadResource(string name)
 83    {
 184        var assembly = Assembly.GetExecutingAssembly();
 285        var resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(name));
 86
 187        using Stream stream = assembly.GetManifestResourceStream(resourcePath)!;
 188        using StreamReader reader = new(stream);
 89
 190        return reader.ReadToEnd();
 191    }
 92}