| | | 1 | | using System.Collections; |
| | | 2 | | using System.Management.Automation; |
| | | 3 | | using System.Reflection; |
| | | 4 | | using System.Text.Json.Nodes; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.OpenApi; |
| | | 7 | | /// <summary> |
| | | 8 | | /// Helpers to create System.Text.Json.Nodes from .NET objects for OpenAPI representation. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class OpenApiJsonNodeFactory |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Create a JsonNode from a .NET object. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="value">The .NET object to convert.</param> |
| | | 16 | | /// <returns>A JsonNode representation of the object.</returns> |
| | | 17 | | public static JsonNode? ToNode(object? value) |
| | | 18 | | { |
| | 102 | 19 | | value = Unwrap(value); |
| | 116 | 20 | | if (value is null) { return null; } |
| | | 21 | | |
| | | 22 | | // Handle various common types |
| | 88 | 23 | | return value switch |
| | 88 | 24 | | { |
| | 88 | 25 | | // primitives |
| | 1 | 26 | | bool b => JsonValue.Create(b), |
| | 50 | 27 | | string s => JsonValue.Create(s), |
| | 88 | 28 | | |
| | 88 | 29 | | // integers (preserve range; avoid ulong->long overflow) |
| | 0 | 30 | | sbyte sb => JsonValue.Create((long)sb), |
| | 0 | 31 | | byte by => JsonValue.Create((long)by), |
| | 0 | 32 | | short sh => JsonValue.Create((long)sh), |
| | 0 | 33 | | ushort ush => JsonValue.Create((long)ush), |
| | 13 | 34 | | int i => JsonValue.Create((long)i), |
| | 0 | 35 | | uint ui => JsonValue.Create((ulong)ui <= long.MaxValue ? ui : (decimal)ui), |
| | 0 | 36 | | long l => JsonValue.Create(l), |
| | 0 | 37 | | ulong ul => ul <= long.MaxValue ? JsonValue.Create((long)ul) : JsonValue.Create((decimal)ul), |
| | 88 | 38 | | |
| | 88 | 39 | | // floating/decimal |
| | 0 | 40 | | float f => JsonValue.Create((double)f), |
| | 0 | 41 | | double d => JsonValue.Create(d), |
| | 0 | 42 | | decimal m => JsonValue.Create(m), |
| | 88 | 43 | | |
| | 88 | 44 | | // common .NET types |
| | 1 | 45 | | DateTime dt => JsonValue.Create(dt.ToString("o")), |
| | 0 | 46 | | DateTimeOffset dto => JsonValue.Create(dto.ToString("o")), |
| | 0 | 47 | | TimeSpan ts => JsonValue.Create(ts.ToString("c")), |
| | 0 | 48 | | Guid g => JsonValue.Create(g.ToString()), |
| | 0 | 49 | | Uri uri => JsonValue.Create(uri.ToString()), |
| | 88 | 50 | | |
| | 88 | 51 | | // enums -> string (usually nicer for OpenAPI-ish metadata) |
| | 0 | 52 | | Enum e => JsonValue.Create(e.ToString()), |
| | 88 | 53 | | |
| | 88 | 54 | | // dictionaries / lists |
| | 3 | 55 | | IDictionary dict => ToJsonObject(dict), |
| | 16 | 56 | | IEnumerable en when value is not string => ToJsonArray(en), |
| | 88 | 57 | | |
| | 88 | 58 | | // fallback |
| | 12 | 59 | | _ => FromPocoOrString(value), |
| | 88 | 60 | | }; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Unwraps common wrapper types to get the underlying value. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="value">The object to unwrap.</param> |
| | | 67 | | /// <returns>The unwrapped object, or the original if no unwrapping was needed.</returns> |
| | | 68 | | private static object? Unwrap(object? value) |
| | | 69 | | { |
| | 102 | 70 | | if (value is null) |
| | | 71 | | { |
| | 14 | 72 | | return null; |
| | | 73 | | } |
| | | 74 | | // PowerShell wraps lots of values in PSObject |
| | 88 | 75 | | if (value is PSObject pso) |
| | | 76 | | { |
| | | 77 | | // If it's a PSCustomObject / has note properties, keep PSObject itself |
| | | 78 | | // so we can serialize its Properties cleanly. |
| | | 79 | | // Otherwise unwrap to BaseObject. |
| | 0 | 80 | | return pso.BaseObject is not null && pso.BaseObject.GetType() != typeof(PSCustomObject) ? |
| | 0 | 81 | | pso.BaseObject : pso; |
| | | 82 | | } |
| | | 83 | | |
| | 88 | 84 | | return value; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Converts an IDictionary to a JsonObject. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="dict">The dictionary to convert.</param> |
| | | 91 | | /// <returns>A JsonObject representing the dictionary.</returns> |
| | | 92 | | private static JsonObject ToJsonObject(IDictionary dict) |
| | | 93 | | { |
| | 3 | 94 | | var obj = new JsonObject(); |
| | | 95 | | |
| | 18 | 96 | | foreach (DictionaryEntry de in dict) |
| | | 97 | | { |
| | 6 | 98 | | if (de.Key is null) |
| | | 99 | | { |
| | | 100 | | continue; |
| | | 101 | | } |
| | | 102 | | |
| | 6 | 103 | | var k = de.Key.ToString(); |
| | 6 | 104 | | if (string.IsNullOrWhiteSpace(k)) |
| | | 105 | | { |
| | | 106 | | continue; |
| | | 107 | | } |
| | | 108 | | |
| | 6 | 109 | | obj[k] = ToNode(de.Value); |
| | | 110 | | } |
| | | 111 | | |
| | 3 | 112 | | return obj; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Converts an IEnumerable to a JsonArray. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="en">The enumerable to convert.</param> |
| | | 119 | | /// <returns>A JsonArray representing the enumerable.</returns> |
| | | 120 | | private static JsonArray ToJsonArray(IEnumerable en) |
| | | 121 | | { |
| | 8 | 122 | | var arr = new JsonArray(); |
| | 16 | 123 | | foreach (var item in en) |
| | | 124 | | { |
| | 0 | 125 | | arr.Add(ToNode(item)); |
| | | 126 | | } |
| | 8 | 127 | | return arr; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Converts a POCO or other object to a JsonNode by reflecting its public properties. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="value">The object to convert.</param> |
| | | 134 | | /// <returns>A JsonNode representing the object.</returns> |
| | | 135 | | private static JsonNode FromPocoOrString(object value) |
| | | 136 | | { |
| | 12 | 137 | | if (TryConvertPsObjectProperties(value, out var psObject)) |
| | | 138 | | { |
| | 0 | 139 | | return psObject; |
| | | 140 | | } |
| | | 141 | | |
| | 12 | 142 | | var type = value.GetType(); |
| | | 143 | | // Skip null property values to avoid serializing them in the OpenAPI document. |
| | | 144 | | // Avoid reflecting on common framework types |
| | 12 | 145 | | if (ShouldFallbackToString(type)) |
| | | 146 | | { |
| | 0 | 147 | | return JsonValue.Create(value.ToString() ?? string.Empty); |
| | | 148 | | } |
| | | 149 | | |
| | 12 | 150 | | if (TryConvertPublicProperties(value, type, out var poco)) |
| | | 151 | | { |
| | 9 | 152 | | return poco; |
| | | 153 | | } |
| | | 154 | | // Fallback to string representation |
| | 3 | 155 | | return JsonValue.Create(value.ToString() ?? string.Empty); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Attempts to serialize a PowerShell object using its dynamic properties. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="value">The input value to inspect.</param> |
| | | 162 | | /// <param name="node">A JsonObject containing serialized PowerShell properties when successful.</param> |
| | | 163 | | /// <returns>True when properties were found and serialized; otherwise false.</returns> |
| | | 164 | | private static bool TryConvertPsObjectProperties(object value, out JsonNode node) |
| | | 165 | | { |
| | 12 | 166 | | node = null!; |
| | | 167 | | |
| | | 168 | | // If it's a PowerShell object with properties, serialize those rather than reflection on the proxy type. |
| | 12 | 169 | | if (value is not PSObject pso) |
| | | 170 | | { |
| | 12 | 171 | | return false; |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | var obj = new JsonObject(); |
| | 0 | 175 | | foreach (var prop in pso.Properties) |
| | | 176 | | { |
| | 0 | 177 | | var v = prop.Value; |
| | 0 | 178 | | if (v is null) |
| | | 179 | | { |
| | | 180 | | continue; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | obj[prop.Name] = ToNode(v); |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | if (obj.Count == 0) |
| | | 187 | | { |
| | 0 | 188 | | return false; |
| | | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | node = obj; |
| | 0 | 192 | | return true; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Determines whether an object of the specified type should be represented as a string |
| | | 197 | | /// instead of reflecting public properties. |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="type">The type to inspect.</param> |
| | | 200 | | /// <returns>True when the type should be treated as a scalar/string; otherwise false.</returns> |
| | | 201 | | private static bool ShouldFallbackToString(Type type) => |
| | 12 | 202 | | type.IsPrimitive || type == typeof(string) || typeof(IEnumerable).IsAssignableFrom(type); |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Attempts to serialize a POCO by reflecting its public, readable, non-indexer instance properties. |
| | | 206 | | /// </summary> |
| | | 207 | | /// <param name="value">The object instance to read properties from.</param> |
| | | 208 | | /// <param name="type">The runtime type of <paramref name="value"/>.</param> |
| | | 209 | | /// <param name="node">A JsonObject when at least one property was serialized.</param> |
| | | 210 | | /// <returns>True when properties were found and serialized; otherwise false.</returns> |
| | | 211 | | private static bool TryConvertPublicProperties(object value, Type type, out JsonNode node) |
| | | 212 | | { |
| | 12 | 213 | | node = null!; |
| | | 214 | | |
| | 12 | 215 | | var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| | 12 | 216 | | if (props.Length == 0) |
| | | 217 | | { |
| | 3 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | 9 | 221 | | var obj = new JsonObject(); |
| | 46 | 222 | | foreach (var p in props) |
| | | 223 | | { |
| | 14 | 224 | | if (!IsReadableNonIndexer(p)) |
| | | 225 | | { |
| | | 226 | | continue; |
| | | 227 | | } |
| | | 228 | | |
| | 14 | 229 | | if (!TryGetPropertyValue(p, value, out var v) || v is null) |
| | | 230 | | { |
| | | 231 | | continue; |
| | | 232 | | } |
| | | 233 | | |
| | 14 | 234 | | obj[p.Name] = ToNode(v); |
| | | 235 | | } |
| | | 236 | | |
| | 9 | 237 | | if (obj.Count == 0) |
| | | 238 | | { |
| | 0 | 239 | | return false; |
| | | 240 | | } |
| | | 241 | | |
| | 9 | 242 | | node = obj; |
| | 9 | 243 | | return true; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// Checks whether a property is readable and not an indexer. |
| | | 248 | | /// </summary> |
| | | 249 | | /// <param name="property">The property to check.</param> |
| | | 250 | | /// <returns>True when the property can be read and has no index parameters.</returns> |
| | | 251 | | private static bool IsReadableNonIndexer(PropertyInfo property) => |
| | 14 | 252 | | property.CanRead && property.GetIndexParameters().Length == 0; |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Attempts to read a property value and safely handles reflection exceptions. |
| | | 256 | | /// </summary> |
| | | 257 | | /// <param name="property">The property to read.</param> |
| | | 258 | | /// <param name="instance">The instance to read the value from.</param> |
| | | 259 | | /// <param name="value">The retrieved value, or null if not available.</param> |
| | | 260 | | /// <returns>True when the value was retrieved successfully; otherwise false.</returns> |
| | | 261 | | private static bool TryGetPropertyValue(PropertyInfo property, object instance, out object? value) |
| | | 262 | | { |
| | 14 | 263 | | value = null; |
| | | 264 | | |
| | | 265 | | try |
| | | 266 | | { |
| | 14 | 267 | | value = property.GetValue(instance); |
| | 14 | 268 | | return true; |
| | | 269 | | } |
| | 0 | 270 | | catch |
| | | 271 | | { |
| | 0 | 272 | | return false; |
| | | 273 | | } |
| | 14 | 274 | | } |
| | | 275 | | } |