< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.OpenApi.OpenApiJsonNodeFactory
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/OpenApi/OpenApiJsonNodeFactory.cs
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 88
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 64
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/36) Branch coverage: 0% (0/64) Total lines: 88 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd 12/12/2025 - 17:27:19 Line coverage: 0% (0/36) Branch coverage: 0% (0/64) Total lines: 88 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FromObject(...)0%1482380%
ToJsonObject(...)0%4260%
ToJsonArray(...)0%620%
FromPocoOrString(...)0%342180%

File(s)

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

#LineLine coverage
 1using System.Collections;
 2using System.Text.Json.Nodes;
 3
 4namespace Kestrun.OpenApi;
 5/// <summary>
 6/// Helpers to create System.Text.Json.Nodes from .NET objects for OpenAPI representation.
 7/// </summary>
 8public static class OpenApiJsonNodeFactory
 9{
 10    /// <summary>
 11    /// Create a JsonNode from a .NET object.
 12    /// </summary>
 13    /// <param name="value">The .NET object to convert.</param>
 14    /// <returns>A JsonNode representation of the object.</returns>
 15    public static JsonNode? FromObject(object? value)
 16    {
 017        return value is null
 018            ? null
 019            : value switch
 020            {
 021                bool b => JsonValue.Create(b),
 022                string s => JsonValue.Create(s),
 023                sbyte or byte or short or ushort or int or uint or long or ulong => JsonValue.Create(Convert.ToInt64(val
 024                float or double or decimal => JsonValue.Create(Convert.ToDouble(value)),
 025                DateTime dt => JsonValue.Create(dt.ToString("o")),
 026                Guid g => JsonValue.Create(g.ToString()),
 027                IDictionary dict => ToJsonObject(dict),
 028                IEnumerable en when value is not string => ToJsonArray(en),
 029                _ => FromPocoOrString(value),
 030            };
 31    }
 32
 33    private static JsonObject ToJsonObject(IDictionary dict)
 34    {
 035        var obj = new JsonObject();
 036        foreach (DictionaryEntry de in dict)
 37        {
 038            if (de.Key is null)
 39            {
 40                continue;
 41            }
 42
 043            var k = de.Key.ToString() ?? string.Empty;
 044            obj[k] = FromObject(de.Value);
 45        }
 046        return obj;
 47    }
 48
 49    private static JsonArray ToJsonArray(IEnumerable en)
 50    {
 051        var arr = new JsonArray();
 052        foreach (var item in en)
 53        {
 054            arr.Add(FromObject(item));
 55        }
 056        return arr;
 57    }
 58
 59    private static JsonNode FromPocoOrString(object value)
 60    {
 061        var t = value.GetType();
 062        if (!t.IsPrimitive && t != typeof(string) && !typeof(IEnumerable).IsAssignableFrom(t))
 63        {
 064            var props = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
 065            if (props.Length > 0)
 66            {
 067                var obj = new JsonObject();
 068                foreach (var p in props)
 69                {
 070                    if (!p.CanRead)
 71                    {
 72                        continue;
 73                    }
 74
 075                    var v = p.GetValue(value);
 076                    if (v is null)
 77                    {
 78                        continue;
 79                    }
 80
 081                    obj[p.Name] = FromObject(v);
 82                }
 083                return obj;
 84            }
 85        }
 086        return JsonValue.Create(value?.ToString() ?? string.Empty)!;
 87    }
 88}