< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.OpenApi.OpenApiHelper
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/OpenApi/OpenApiHelper.cs
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 109
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 12/12/2025 - 17:27:19 Line coverage: 0% (0/27) Branch coverage: 0% (0/12) Total lines: 109 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd 12/12/2025 - 17:27:19 Line coverage: 0% (0/27) Branch coverage: 0% (0/12) Total lines: 109 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddSecurityRequirementObject(...)100%210%
AddExplicitScheme(...)0%620%
MapPoliciesToSchemes(...)0%7280%
GetOrCreateScopeList(...)0%620%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/OpenApi/OpenApiHelper.cs

#LineLine coverage
 1using Kestrun.Hosting;
 2
 3namespace Kestrun.OpenApi;
 4
 5/// <summary>
 6/// Helper methods for OpenAPI integration.
 7/// </summary>
 8public static class OpenApiHelper
 9{
 10    /// <summary>
 11    /// Adds a security requirement object to the OpenAPI metadata based on the specified scheme and policies.
 12    /// </summary>
 13    /// <param name="host"> The Kestrun host instance.</param>
 14    /// <param name="scheme">The security scheme name.</param>
 15    /// <param name="policyList">List of security policies.</param>
 16    /// <param name="securitySchemes">The list of security schemes to which the security requirement will be added.</par
 17    /// <returns>A list of all security schemes involved in the requirement.</returns>
 18    internal static List<string> AddSecurityRequirementObject(this KestrunHost host,
 19        string? scheme, List<string> policyList,
 20        List<Dictionary<string, List<string>>> securitySchemes)
 21    {
 022        ArgumentNullException.ThrowIfNull(host);
 023        ArgumentNullException.ThrowIfNull(policyList);
 024        ArgumentNullException.ThrowIfNull(securitySchemes);
 25
 026        var scopesByScheme = new Dictionary<string, List<string>>(StringComparer.Ordinal);
 027        var allSchemes = new HashSet<string>(StringComparer.Ordinal);
 28
 029        AddExplicitScheme(scheme, scopesByScheme, allSchemes);
 030        MapPoliciesToSchemes(host, policyList, scopesByScheme, allSchemes);
 31
 032        securitySchemes.Add(scopesByScheme);
 33
 034        return [.. allSchemes];
 35    }
 36
 37    /// <summary>
 38    /// Adds an explicit security scheme to the scopes dictionary and all schemes set.
 39    /// </summary>
 40    /// <param name="scheme">The security scheme name.</param>
 41    /// <param name="scopesByScheme">The dictionary mapping schemes to their scopes.</param>
 42    /// <param name="allSchemes">The set of all security schemes.</param>
 43    private static void AddExplicitScheme(
 44        string? scheme,
 45        Dictionary<string, List<string>> scopesByScheme,
 46        HashSet<string> allSchemes)
 47    {
 048        if (string.IsNullOrWhiteSpace(scheme))
 49        {
 050            return;
 51        }
 52
 053        _ = GetOrCreateScopeList(scopesByScheme, scheme);
 054        _ = allSchemes.Add(scheme);
 055    }
 56    /// <summary>
 57    /// Maps security policies to their corresponding security schemes.
 58    /// </summary>
 59    /// <param name="host">The Kestrun host instance.</param>
 60    /// <param name="policyList">List of security policies.</param>
 61    /// <param name="scopesByScheme">The dictionary mapping schemes to their scopes.</param>
 62    /// <param name="allSchemes">The set of all security schemes.</param>
 63    private static void MapPoliciesToSchemes(
 64        KestrunHost host,
 65        IEnumerable<string> policyList,
 66        Dictionary<string, List<string>> scopesByScheme,
 67        HashSet<string> allSchemes)
 68    {
 069        foreach (var policy in policyList)
 70        {
 071            var schemesForPolicy = host.RegisteredAuthentications.GetSchemesByPolicy(policy);
 072            if (schemesForPolicy is null)
 73            {
 74                continue;
 75            }
 76
 077            foreach (var schemeName in schemesForPolicy)
 78            {
 079                var scopeList = GetOrCreateScopeList(scopesByScheme, schemeName);
 80
 081                if (!scopeList.Contains(policy))
 82                {
 083                    scopeList.Add(policy);
 84                }
 85
 086                _ = allSchemes.Add(schemeName);
 87            }
 88        }
 089    }
 90
 91    /// <summary>
 92    /// Retrieves or creates the scope list for a given security scheme.
 93    /// </summary>
 94    /// <param name="scopesByScheme">The dictionary mapping schemes to their scopes.</param>
 95    /// <param name="schemeName">The security scheme name.</param>
 96    /// <returns>The list of scopes associated with the security scheme.</returns>
 97    private static List<string> GetOrCreateScopeList(
 98        Dictionary<string, List<string>> scopesByScheme,
 99        string schemeName)
 100    {
 0101        if (!scopesByScheme.TryGetValue(schemeName, out var scopeList))
 102        {
 0103            scopeList = [];
 0104            scopesByScheme[schemeName] = scopeList;
 105        }
 106
 0107        return scopeList;
 108    }
 109}