| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Text.Json.Nodes; |
| | | 3 | | using Microsoft.OpenApi; |
| | | 4 | | using Kestrun.Runtime; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.OpenApi; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Generates OpenAPI v2 (Swagger) documents from C# types decorated with OpenApiSchema attributes. |
| | | 10 | | /// </summary> |
| | | 11 | | public partial class OpenApiDocDescriptor |
| | | 12 | | { |
| | | 13 | | #region Schemas |
| | | 14 | | private static OpenApiPropertyAttribute? GetSchemaIdentity(Type t) |
| | | 15 | | { |
| | | 16 | | // inherit:true already climbs the chain until it finds the first one |
| | 0 | 17 | | var attrs = (OpenApiPropertyAttribute[])t.GetCustomAttributes(typeof(OpenApiPropertyAttribute), inherit: true); |
| | 0 | 18 | | return attrs.Length > 0 ? attrs[0] : null; |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Builds and returns the schema for a given type. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="t">Type to build schema for</param> |
| | | 25 | | /// <param name="built">Set of types already built to avoid recursion</param> |
| | | 26 | | /// <returns>OpenApiSchema representing the type</returns> |
| | | 27 | | private IOpenApiSchema BuildSchemaForType(Type t, HashSet<Type>? built = null) |
| | | 28 | | { |
| | 21 | 29 | | built ??= []; |
| | | 30 | | |
| | | 31 | | // Handle custom base type derivations first |
| | 21 | 32 | | if (t.BaseType is not null && t.BaseType != typeof(object)) |
| | | 33 | | { |
| | 6 | 34 | | var baseTypeSchema = BuildBaseTypeSchema(t); |
| | 6 | 35 | | if (baseTypeSchema is not null) |
| | | 36 | | { |
| | 6 | 37 | | return baseTypeSchema; |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | 15 | 41 | | var schema = new OpenApiSchema |
| | 15 | 42 | | { |
| | 15 | 43 | | Type = JsonSchemaType.Object, |
| | 15 | 44 | | Properties = new Dictionary<string, IOpenApiSchema>(StringComparer.Ordinal) |
| | 15 | 45 | | }; |
| | | 46 | | |
| | | 47 | | // Prevent infinite recursion |
| | 15 | 48 | | if (built.Contains(t)) |
| | | 49 | | { |
| | 1 | 50 | | return schema; |
| | | 51 | | } |
| | | 52 | | |
| | 14 | 53 | | _ = built.Add(t); |
| | | 54 | | |
| | | 55 | | // Handle enum types |
| | 14 | 56 | | if (t.IsEnum) |
| | | 57 | | { |
| | 0 | 58 | | RegisterEnumSchema(t); |
| | 0 | 59 | | return schema; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | // Early return for primitive types |
| | 14 | 63 | | if (IsPrimitiveLike(t)) |
| | | 64 | | { |
| | 2 | 65 | | return schema; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | // Apply type-level attributes |
| | 12 | 69 | | ApplyTypeAttributes(t, schema); |
| | | 70 | | |
| | | 71 | | // Process properties with default value capture |
| | 12 | 72 | | ProcessTypeProperties(t, schema, built); |
| | | 73 | | |
| | 12 | 74 | | return schema; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Builds schema for custom base type derivations. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="t">Type to build schema for</param> |
| | | 81 | | /// <returns>OpenApiSchema representing the base type derivation, or null if not applicable</returns> |
| | | 82 | | private IOpenApiSchema? BuildBaseTypeSchema(Type t) |
| | | 83 | | { |
| | | 84 | | // Determine if the base type is a known OpenAPI primitive type |
| | 6 | 85 | | OaSchemaType? baseTypeName = t.BaseType switch |
| | 6 | 86 | | { |
| | 6 | 87 | | Type bt when bt == typeof(OaString) => OaSchemaType.String, |
| | 6 | 88 | | Type bt when bt == typeof(OaInteger) => OaSchemaType.Integer, |
| | 6 | 89 | | Type bt when bt == typeof(OaNumber) => OaSchemaType.Number, |
| | 6 | 90 | | Type bt when bt == typeof(OaBoolean) => OaSchemaType.Boolean, |
| | 6 | 91 | | _ => null |
| | 6 | 92 | | }; |
| | | 93 | | |
| | 6 | 94 | | if (typeof(IOpenApiType).IsAssignableFrom(t)) |
| | | 95 | | { |
| | 0 | 96 | | return BuildOpenApiTypeSchema(t); |
| | | 97 | | } |
| | | 98 | | // Fallback to custom base type schema building |
| | 6 | 99 | | return BuildCustomBaseTypeSchema(t, baseTypeName); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Builds schema for types implementing IOpenApiType. |
| | | 104 | | /// </summary> |
| | | 105 | | private static OpenApiSchema? BuildOpenApiTypeSchema(Type t) |
| | | 106 | | { |
| | 0 | 107 | | var attr = GetSchemaIdentity(t); |
| | 0 | 108 | | return attr is not null |
| | 0 | 109 | | ? new OpenApiSchema |
| | 0 | 110 | | { |
| | 0 | 111 | | Type = attr.Type.ToJsonSchemaType(), |
| | 0 | 112 | | Format = attr.Format |
| | 0 | 113 | | } |
| | 0 | 114 | | : null; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Builds schema for types with custom base types. |
| | | 119 | | /// </summary> |
| | | 120 | | private IOpenApiSchema BuildCustomBaseTypeSchema(Type t, OaSchemaType? baseTypeName) |
| | | 121 | | { |
| | 6 | 122 | | IOpenApiSchema baseSchema = baseTypeName is not null |
| | 6 | 123 | | ? new OpenApiSchema { Type = baseTypeName?.ToJsonSchemaType() } |
| | 6 | 124 | | : new OpenApiSchemaReference(t.BaseType!.Name); |
| | | 125 | | |
| | 6 | 126 | | var schemaComps = t.GetCustomAttributes<OpenApiProperties>() |
| | 0 | 127 | | .Where(schemaComp => schemaComp is not null) |
| | 6 | 128 | | .Cast<OpenApiProperties>(); |
| | | 129 | | |
| | 12 | 130 | | foreach (var prop in schemaComps) |
| | | 131 | | { |
| | 0 | 132 | | return BuildPropertyFromAttribute(prop, baseSchema); |
| | | 133 | | } |
| | | 134 | | |
| | 6 | 135 | | return baseSchema; |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Builds a property schema from an OpenApiProperties attribute. |
| | | 140 | | /// </summary> |
| | | 141 | | private static IOpenApiSchema BuildPropertyFromAttribute(OpenApiProperties prop, IOpenApiSchema baseSchema) |
| | | 142 | | { |
| | 0 | 143 | | var schema = prop.Array |
| | 0 | 144 | | ? new OpenApiSchema { Type = JsonSchemaType.Array, Items = baseSchema } |
| | 0 | 145 | | : baseSchema; |
| | | 146 | | |
| | 0 | 147 | | ApplySchemaAttr(prop, schema); |
| | 0 | 148 | | return schema; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Registers an enum type schema in the document components. |
| | | 153 | | /// </summary> |
| | | 154 | | private void RegisterEnumSchema(Type enumType) |
| | | 155 | | { |
| | 0 | 156 | | if (Document.Components?.Schemas is not null) |
| | | 157 | | { |
| | 0 | 158 | | Document.Components.Schemas[enumType.Name] = new OpenApiSchema |
| | 0 | 159 | | { |
| | 0 | 160 | | Type = JsonSchemaType.String, |
| | 0 | 161 | | Enum = [.. enumType.GetEnumNames().Select(n => (JsonNode)n)] |
| | 0 | 162 | | }; |
| | | 163 | | } |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Applies type-level attributes to a schema. |
| | | 168 | | /// </summary> |
| | | 169 | | private static void ApplyTypeAttributes(Type t, OpenApiSchema schema) |
| | | 170 | | { |
| | 32 | 171 | | foreach (var attr in t.GetCustomAttributes(true) |
| | 38 | 172 | | .Where(a => a is OpenApiPropertyAttribute or OpenApiSchemaComponent)) |
| | | 173 | | { |
| | 4 | 174 | | ApplySchemaAttr(attr as OpenApiProperties, schema); |
| | | 175 | | |
| | 4 | 176 | | if (attr is OpenApiSchemaComponent schemaAttribute && schemaAttribute.Examples is not null) |
| | | 177 | | { |
| | 0 | 178 | | schema.Examples ??= []; |
| | 0 | 179 | | var node = ToNode(schemaAttribute.Examples); |
| | 0 | 180 | | if (node is not null) |
| | | 181 | | { |
| | 0 | 182 | | schema.Examples.Add(node); |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |
| | 12 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Processes all properties of a type and builds their schemas. |
| | | 190 | | /// </summary> |
| | | 191 | | private void ProcessTypeProperties(Type t, OpenApiSchema schema, HashSet<Type> built) |
| | | 192 | | { |
| | 12 | 193 | | var instance = TryCreateTypeInstance(t); |
| | | 194 | | |
| | 70 | 195 | | foreach (var prop in t.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| | | 196 | | { |
| | 23 | 197 | | var propSchema = BuildPropertySchema(prop, built); |
| | 23 | 198 | | CapturePropertyDefault(instance, prop, propSchema); |
| | | 199 | | |
| | 23 | 200 | | if (prop.GetCustomAttribute<OpenApiAdditionalPropertiesAttribute>() is not null) |
| | | 201 | | { |
| | 1 | 202 | | schema.AdditionalPropertiesAllowed = true; |
| | 1 | 203 | | schema.AdditionalProperties = propSchema; |
| | | 204 | | } |
| | | 205 | | else |
| | | 206 | | { |
| | 22 | 207 | | schema.Properties?.Add(prop.Name, propSchema); |
| | | 208 | | } |
| | | 209 | | } |
| | 12 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Attempts to create an instance of a type to capture default values. |
| | | 214 | | /// </summary> |
| | | 215 | | private static object? TryCreateTypeInstance(Type t) |
| | | 216 | | { |
| | | 217 | | try |
| | | 218 | | { |
| | 12 | 219 | | return Activator.CreateInstance(t); |
| | | 220 | | } |
| | 1 | 221 | | catch |
| | | 222 | | { |
| | 1 | 223 | | return null; |
| | | 224 | | } |
| | 12 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Captures the default value of a property if not already set. |
| | | 229 | | /// </summary> |
| | | 230 | | private static void CapturePropertyDefault(object? instance, PropertyInfo prop, IOpenApiSchema propSchema) |
| | | 231 | | { |
| | 23 | 232 | | if (instance is null || propSchema is not OpenApiSchema concrete || concrete.Default is not null) |
| | | 233 | | { |
| | 1 | 234 | | return; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | try |
| | | 238 | | { |
| | 22 | 239 | | var value = prop.GetValue(instance); |
| | 22 | 240 | | if (!IsIntrinsicDefault(value, prop.PropertyType)) |
| | | 241 | | { |
| | 20 | 242 | | concrete.Default = ToNode(value); |
| | | 243 | | } |
| | 22 | 244 | | } |
| | 0 | 245 | | catch |
| | | 246 | | { |
| | | 247 | | // Ignore failures when capturing defaults |
| | 0 | 248 | | } |
| | 22 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Determines if a value is the intrinsic default for its declared type. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <param name="value">The value to check.</param> |
| | | 255 | | /// <param name="declaredType">The declared type of the value.</param> |
| | | 256 | | /// <returns>True if the value is the intrinsic default for its declared type; otherwise, false.</returns> |
| | | 257 | | private static bool IsIntrinsicDefault(object? value, Type declaredType) |
| | | 258 | | { |
| | 22 | 259 | | if (value is null) |
| | | 260 | | { |
| | 1 | 261 | | return true; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | // Unwrap Nullable<T> |
| | 21 | 265 | | var t = Nullable.GetUnderlyingType(declaredType) ?? declaredType; |
| | | 266 | | |
| | | 267 | | // Reference types: null is the only intrinsic default |
| | 21 | 268 | | if (!t.IsValueType) |
| | | 269 | | { |
| | 12 | 270 | | return false; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | // Special-cases for common structs |
| | 9 | 274 | | if (t == typeof(Guid)) |
| | | 275 | | { |
| | 0 | 276 | | return value.Equals(Guid.Empty); |
| | | 277 | | } |
| | | 278 | | |
| | 9 | 279 | | if (t == typeof(TimeSpan)) |
| | | 280 | | { |
| | 0 | 281 | | return value.Equals(TimeSpan.Zero); |
| | | 282 | | } |
| | | 283 | | |
| | 9 | 284 | | if (t == typeof(DateTime)) |
| | | 285 | | { |
| | 1 | 286 | | return value.Equals(default(DateTime)); |
| | | 287 | | } |
| | | 288 | | |
| | 8 | 289 | | if (t == typeof(DateTimeOffset)) |
| | | 290 | | { |
| | 0 | 291 | | return value.Equals(default(DateTimeOffset)); |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | // Enums: 0 is intrinsic default |
| | 8 | 295 | | if (t.IsEnum) |
| | | 296 | | { |
| | 0 | 297 | | return Convert.ToInt64(value) == 0; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | // Primitive/value types: compare to default(T) |
| | 8 | 301 | | var def = Activator.CreateInstance(t); |
| | 8 | 302 | | return value.Equals(def); |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Merges multiple OpenApiPropertyAttribute instances into one. |
| | | 307 | | /// </summary> |
| | | 308 | | /// <param name="attrs">An array of OpenApiPropertyAttribute instances to merge.</param> |
| | | 309 | | /// <returns>A single OpenApiPropertyAttribute instance representing the merged attributes.</returns> |
| | | 310 | | private static OpenApiPropertyAttribute? MergeSchemaAttributes(OpenApiPropertyAttribute[] attrs) |
| | | 311 | | { |
| | 0 | 312 | | if (attrs == null || attrs.Length == 0) |
| | | 313 | | { |
| | 0 | 314 | | return null; |
| | | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | if (attrs.Length == 1) |
| | | 318 | | { |
| | 0 | 319 | | return attrs[0]; |
| | | 320 | | } |
| | | 321 | | |
| | 0 | 322 | | var m = new OpenApiPropertyAttribute(); |
| | | 323 | | |
| | 0 | 324 | | foreach (var a in attrs) |
| | | 325 | | { |
| | 0 | 326 | | MergeStringProperties(m, a); |
| | 0 | 327 | | MergeEnumAndCollections(m, a); |
| | 0 | 328 | | MergeNumericProperties(m, a); |
| | 0 | 329 | | MergeBooleanProperties(m, a); |
| | 0 | 330 | | MergeTypeAndRequired(m, a); |
| | 0 | 331 | | MergeCustomFields(m, a); |
| | | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | return m; |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | /// <summary> |
| | | 338 | | /// Merges string properties where the last non-empty value wins. |
| | | 339 | | /// </summary> |
| | | 340 | | /// <param name="merged">The merged OpenApiPropertyAttribute to update.</param> |
| | | 341 | | /// <param name="attr">The OpenApiPropertyAttribute to merge from.</param> |
| | | 342 | | private static void MergeStringProperties(OpenApiPropertyAttribute merged, OpenApiPropertyAttribute attr) |
| | | 343 | | { |
| | 0 | 344 | | if (!string.IsNullOrWhiteSpace(attr.Title)) |
| | | 345 | | { |
| | 0 | 346 | | merged.Title = attr.Title; |
| | | 347 | | } |
| | | 348 | | |
| | 0 | 349 | | if (!string.IsNullOrWhiteSpace(attr.Description)) |
| | | 350 | | { |
| | 0 | 351 | | merged.Description = attr.Description; |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | if (!string.IsNullOrWhiteSpace(attr.Format)) |
| | | 355 | | { |
| | 0 | 356 | | merged.Format = attr.Format; |
| | | 357 | | } |
| | | 358 | | |
| | 0 | 359 | | if (!string.IsNullOrWhiteSpace(attr.Pattern)) |
| | | 360 | | { |
| | 0 | 361 | | merged.Pattern = attr.Pattern; |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | if (!string.IsNullOrWhiteSpace(attr.Maximum)) |
| | | 365 | | { |
| | 0 | 366 | | merged.Maximum = attr.Maximum; |
| | | 367 | | } |
| | | 368 | | |
| | 0 | 369 | | if (!string.IsNullOrWhiteSpace(attr.Minimum)) |
| | | 370 | | { |
| | 0 | 371 | | merged.Minimum = attr.Minimum; |
| | | 372 | | } |
| | 0 | 373 | | } |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Merges enum and collection properties. |
| | | 377 | | /// </summary> |
| | | 378 | | /// <param name="merged">The merged OpenApiPropertyAttribute to update.</param> |
| | | 379 | | /// <param name="attr">The OpenApiPropertyAttribute to merge from.</param> |
| | | 380 | | private static void MergeEnumAndCollections(OpenApiPropertyAttribute merged, OpenApiPropertyAttribute attr) |
| | | 381 | | { |
| | 0 | 382 | | if (attr.Enum is { Length: > 0 }) |
| | | 383 | | { |
| | 0 | 384 | | merged.Enum = [.. merged.Enum ?? [], .. attr.Enum]; |
| | | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | if (attr.Default is not null) |
| | | 388 | | { |
| | 0 | 389 | | merged.Default = attr.Default; |
| | | 390 | | } |
| | | 391 | | |
| | 0 | 392 | | if (attr.Example is not null) |
| | | 393 | | { |
| | 0 | 394 | | merged.Example = attr.Example; |
| | | 395 | | } |
| | 0 | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Merges numeric properties where values >= 0 are considered explicitly set. |
| | | 400 | | /// </summary> |
| | | 401 | | /// <param name="merged">The merged OpenApiPropertyAttribute to update.</param> |
| | | 402 | | /// <param name="attr">The OpenApiPropertyAttribute to merge from.</param> |
| | | 403 | | private static void MergeNumericProperties(OpenApiPropertyAttribute merged, OpenApiPropertyAttribute attr) |
| | | 404 | | { |
| | 0 | 405 | | if (attr.MaxLength >= 0) |
| | | 406 | | { |
| | 0 | 407 | | merged.MaxLength = attr.MaxLength; |
| | | 408 | | } |
| | | 409 | | |
| | 0 | 410 | | if (attr.MinLength >= 0) |
| | | 411 | | { |
| | 0 | 412 | | merged.MinLength = attr.MinLength; |
| | | 413 | | } |
| | | 414 | | |
| | 0 | 415 | | if (attr.MaxItems >= 0) |
| | | 416 | | { |
| | 0 | 417 | | merged.MaxItems = attr.MaxItems; |
| | | 418 | | } |
| | | 419 | | |
| | 0 | 420 | | if (attr.MinItems >= 0) |
| | | 421 | | { |
| | 0 | 422 | | merged.MinItems = attr.MinItems; |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | if (attr.MultipleOf is not null) |
| | | 426 | | { |
| | 0 | 427 | | merged.MultipleOf = attr.MultipleOf; |
| | | 428 | | } |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Merges boolean properties using OR logic. |
| | | 433 | | /// </summary> |
| | | 434 | | /// <param name="merged">The merged OpenApiPropertyAttribute to update.</param> |
| | | 435 | | /// <param name="attr">The OpenApiPropertyAttribute to merge from.</param> |
| | | 436 | | private static void MergeBooleanProperties(OpenApiPropertyAttribute merged, OpenApiPropertyAttribute attr) |
| | | 437 | | { |
| | 0 | 438 | | merged.Nullable |= attr.Nullable; |
| | 0 | 439 | | merged.ReadOnly |= attr.ReadOnly; |
| | 0 | 440 | | merged.WriteOnly |= attr.WriteOnly; |
| | 0 | 441 | | merged.Deprecated |= attr.Deprecated; |
| | 0 | 442 | | merged.UniqueItems |= attr.UniqueItems; |
| | 0 | 443 | | merged.ExclusiveMaximum |= attr.ExclusiveMaximum; |
| | 0 | 444 | | merged.ExclusiveMinimum |= attr.ExclusiveMinimum; |
| | 0 | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Merges type and required properties. |
| | | 449 | | /// </summary> |
| | | 450 | | /// <param name="merged">The merged OpenApiPropertyAttribute to update.</param> |
| | | 451 | | /// <param name="attr">The OpenApiPropertyAttribute to merge from.</param> |
| | | 452 | | private static void MergeTypeAndRequired(OpenApiPropertyAttribute merged, OpenApiPropertyAttribute attr) |
| | | 453 | | { |
| | 0 | 454 | | if (attr.Type != OaSchemaType.None) |
| | | 455 | | { |
| | 0 | 456 | | merged.Type = attr.Type; |
| | | 457 | | } |
| | | 458 | | |
| | 0 | 459 | | if (attr.Required is { Length: > 0 }) |
| | | 460 | | { |
| | 0 | 461 | | merged.Required = [.. (merged.Required ?? []).Concat(attr.Required).Distinct()]; |
| | | 462 | | } |
| | 0 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Merges custom fields like XmlName. |
| | | 467 | | /// </summary> |
| | | 468 | | /// <param name="merged">The merged OpenApiPropertyAttribute to update.</param> |
| | | 469 | | /// <param name="attr">The OpenApiPropertyAttribute to merge from.</param> |
| | | 470 | | private static void MergeCustomFields(OpenApiPropertyAttribute merged, OpenApiPropertyAttribute attr) |
| | | 471 | | { |
| | 0 | 472 | | if (!string.IsNullOrWhiteSpace(attr.XmlName)) |
| | | 473 | | { |
| | 0 | 474 | | merged.XmlName = attr.XmlName; |
| | | 475 | | } |
| | 0 | 476 | | } |
| | | 477 | | |
| | | 478 | | /// <summary> |
| | | 479 | | /// Infers a primitive OpenApiSchema from a .NET type. |
| | | 480 | | /// </summary> |
| | | 481 | | /// <param name="type">The .NET type to infer from.</param> |
| | | 482 | | /// <param name="inline">Indicates if the schema should be inlined.</param> |
| | | 483 | | /// <returns>The inferred OpenApiSchema.</returns> |
| | | 484 | | private IOpenApiSchema InferPrimitiveSchema(Type type, bool inline = false) |
| | | 485 | | { |
| | | 486 | | // Direct type mappings |
| | 27 | 487 | | if (PrimitiveSchemaMap.TryGetValue(type, out var schemaFactory)) |
| | | 488 | | { |
| | 27 | 489 | | return schemaFactory(); |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | // Array type handling |
| | 0 | 493 | | if (type.Name.EndsWith("[]")) |
| | | 494 | | { |
| | 0 | 495 | | return InferArraySchema(type, inline); |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | // Special handling for PowerShell OpenAPI classes |
| | 0 | 499 | | if (PowerShellOpenApiClassExporter.ValidClassNames.Contains(type.Name)) |
| | | 500 | | { |
| | 0 | 501 | | return InferPowerShellClassSchema(type, inline); |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | // Fallback |
| | 0 | 505 | | return new OpenApiSchema { Type = JsonSchemaType.String }; |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | /// <summary> |
| | | 509 | | /// Infers an array OpenApiSchema from a .NET array type. |
| | | 510 | | /// </summary> |
| | | 511 | | /// <param name="type">The .NET array type to infer from.</param> |
| | | 512 | | /// <param name="inline">Indicates if the schema should be inlined.</param> |
| | | 513 | | /// <returns>The inferred OpenApiSchema.</returns> |
| | | 514 | | private OpenApiSchema InferArraySchema(Type type, bool inline) |
| | | 515 | | { |
| | 0 | 516 | | var typeName = type.Name[..^2]; |
| | 0 | 517 | | if (ComponentSchemasExists(typeName)) |
| | | 518 | | { |
| | 0 | 519 | | IOpenApiSchema? items = inline ? GetSchema(typeName).Clone() : new OpenApiSchemaReference(typeName); |
| | 0 | 520 | | return new OpenApiSchema { Type = JsonSchemaType.Array, Items = items }; |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | return new OpenApiSchema { Type = JsonSchemaType.Array, Items = InferPrimitiveSchema(type.GetElementType() ?? ty |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | /// <summary> |
| | | 527 | | /// Infers a PowerShell OpenAPI class schema. |
| | | 528 | | /// </summary> |
| | | 529 | | /// <param name="type">The .NET type representing the PowerShell OpenAPI class.</param> |
| | | 530 | | /// <param name="inline">Indicates if the schema should be inlined.</param> |
| | | 531 | | /// <returns>The inferred OpenApiSchema.</returns> |
| | | 532 | | private IOpenApiSchema InferPowerShellClassSchema(Type type, bool inline) |
| | | 533 | | { |
| | 0 | 534 | | var schema = GetSchema(type.Name); |
| | | 535 | | |
| | 0 | 536 | | if (inline) |
| | | 537 | | { |
| | 0 | 538 | | if (schema is OpenApiSchema concreteSchema) |
| | | 539 | | { |
| | 0 | 540 | | return concreteSchema.Clone(); |
| | | 541 | | } |
| | | 542 | | } |
| | | 543 | | else |
| | | 544 | | { |
| | 0 | 545 | | if (schema is not null) |
| | | 546 | | { |
| | 0 | 547 | | return new OpenApiSchemaReference(type.Name); |
| | | 548 | | } |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | Host.Logger.Warning("Schema for PowerShell OpenAPI class '{typeName}' not found. Defaulting to string schema.", |
| | 0 | 552 | | return new OpenApiSchema { Type = JsonSchemaType.String }; |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | /// <summary> |
| | | 556 | | /// Mapping of .NET primitive types to OpenAPI schema definitions. |
| | | 557 | | /// </summary> |
| | | 558 | | /// <remarks> |
| | | 559 | | /// This dictionary maps common .NET primitive types to their corresponding OpenAPI schema representations. |
| | | 560 | | /// Each entry consists of a .NET type as the key and a function that returns an OpenApiSchema as the value. |
| | | 561 | | /// </remarks> |
| | 1 | 562 | | private static readonly Dictionary<Type, Func<OpenApiSchema>> PrimitiveSchemaMap = new() |
| | 1 | 563 | | { |
| | 14 | 564 | | [typeof(string)] = () => new OpenApiSchema { Type = JsonSchemaType.String }, |
| | 1 | 565 | | [typeof(bool)] = () => new OpenApiSchema { Type = JsonSchemaType.Boolean }, |
| | 0 | 566 | | [typeof(long)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64" }, |
| | 1 | 567 | | [typeof(DateTime)] = () => new OpenApiSchema { Type = JsonSchemaType.String, Format = "date-time" }, |
| | 0 | 568 | | [typeof(DateTimeOffset)] = () => new OpenApiSchema { Type = JsonSchemaType.String, Format = "date-time" }, |
| | 0 | 569 | | [typeof(TimeSpan)] = () => new OpenApiSchema { Type = JsonSchemaType.String, Format = "duration" }, |
| | 0 | 570 | | [typeof(byte[])] = () => new OpenApiSchema { Type = JsonSchemaType.String, Format = "binary" }, |
| | 0 | 571 | | [typeof(Uri)] = () => new OpenApiSchema { Type = JsonSchemaType.String, Format = "uri" }, |
| | 0 | 572 | | [typeof(Guid)] = () => new OpenApiSchema { Type = JsonSchemaType.String, Format = "uuid" }, |
| | 4 | 573 | | [typeof(object)] = () => new OpenApiSchema { Type = JsonSchemaType.Object }, |
| | 0 | 574 | | [typeof(void)] = () => new OpenApiSchema { Type = JsonSchemaType.Null }, |
| | 0 | 575 | | [typeof(char)] = () => new OpenApiSchema { Type = JsonSchemaType.String, MaxLength = 1, MinLength = 1 }, |
| | 0 | 576 | | [typeof(sbyte)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" }, |
| | 0 | 577 | | [typeof(byte)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" }, |
| | 0 | 578 | | [typeof(short)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" }, |
| | 0 | 579 | | [typeof(ushort)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" }, |
| | 7 | 580 | | [typeof(int)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" }, |
| | 0 | 581 | | [typeof(uint)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" }, |
| | 0 | 582 | | [typeof(long)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64" }, |
| | 0 | 583 | | [typeof(ulong)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64" }, |
| | 0 | 584 | | [typeof(float)] = () => new OpenApiSchema { Type = JsonSchemaType.Number, Format = "float" }, |
| | 0 | 585 | | [typeof(double)] = () => new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double" }, |
| | 0 | 586 | | [typeof(decimal)] = () => new OpenApiSchema { Type = JsonSchemaType.Number, Format = "decimal" }, |
| | 0 | 587 | | [typeof(OaString)] = () => new OpenApiSchema { Type = JsonSchemaType.String }, |
| | 0 | 588 | | [typeof(OaInteger)] = () => new OpenApiSchema { Type = JsonSchemaType.Integer }, |
| | 0 | 589 | | [typeof(OaNumber)] = () => new OpenApiSchema { Type = JsonSchemaType.Number }, |
| | 0 | 590 | | [typeof(OaBoolean)] = () => new OpenApiSchema { Type = JsonSchemaType.Boolean } |
| | 1 | 591 | | }; |
| | | 592 | | |
| | | 593 | | /// <summary> |
| | | 594 | | /// Applies schema attributes to an OpenAPI schema. |
| | | 595 | | /// </summary> |
| | | 596 | | /// <param name="oaProperties">The OpenApiProperties containing attributes to apply.</param> |
| | | 597 | | /// <param name="ioaSchema">The OpenAPI schema to apply attributes to.</param> |
| | | 598 | | private static void ApplySchemaAttr(OpenApiProperties? oaProperties, IOpenApiSchema ioaSchema) |
| | | 599 | | { |
| | 31 | 600 | | if (oaProperties is null) |
| | | 601 | | { |
| | 25 | 602 | | return; |
| | | 603 | | } |
| | | 604 | | |
| | | 605 | | // Most models implement OpenApiSchema (concrete) OR OpenApiSchemaReference. |
| | | 606 | | // We set common metadata when possible (Description/Title apply only to concrete schema). |
| | 6 | 607 | | if (ioaSchema is OpenApiSchema concreteSchema) |
| | | 608 | | { |
| | 6 | 609 | | ApplyConcreteSchemaAttributes(oaProperties, concreteSchema); |
| | 6 | 610 | | return; |
| | | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | if (ioaSchema is OpenApiSchemaReference refSchema) |
| | | 614 | | { |
| | 0 | 615 | | ApplyReferenceSchemaAttributes(oaProperties, refSchema); |
| | | 616 | | } |
| | 0 | 617 | | } |
| | | 618 | | |
| | | 619 | | /// <summary> |
| | | 620 | | /// Applies concrete schema attributes to an OpenApiSchema. |
| | | 621 | | /// </summary> |
| | | 622 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 623 | | /// <param name="schema">The OpenApiSchema to apply attributes to.</param> |
| | | 624 | | private static void ApplyConcreteSchemaAttributes(OpenApiProperties properties, OpenApiSchema schema) |
| | | 625 | | { |
| | 6 | 626 | | ApplyTitleAndDescription(properties, schema); |
| | 6 | 627 | | ApplySchemaType(properties, schema); |
| | 6 | 628 | | ApplyFormatAndNumericBounds(properties, schema); |
| | 6 | 629 | | ApplyLengthAndPattern(properties, schema); |
| | 6 | 630 | | ApplyCollectionConstraints(properties, schema); |
| | 6 | 631 | | ApplyFlags(properties, schema); |
| | 6 | 632 | | ApplyExamplesAndDefaults(properties, schema); |
| | 6 | 633 | | } |
| | | 634 | | |
| | | 635 | | /// <summary> |
| | | 636 | | /// Applies title and description to an OpenApiSchema. |
| | | 637 | | /// </summary> |
| | | 638 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 639 | | /// <param name="schema">The OpenApiSchema to apply attributes to.</param> |
| | | 640 | | private static void ApplyTitleAndDescription(OpenApiProperties properties, OpenApiSchema schema) |
| | | 641 | | { |
| | 6 | 642 | | if (properties.Title is not null) |
| | | 643 | | { |
| | 4 | 644 | | schema.Title = properties.Title; |
| | | 645 | | } |
| | | 646 | | |
| | 6 | 647 | | if (properties.Description is not null) |
| | | 648 | | { |
| | 5 | 649 | | schema.Description = properties.Description; |
| | | 650 | | } |
| | 6 | 651 | | } |
| | | 652 | | |
| | | 653 | | /// <summary> |
| | | 654 | | /// Applies schema type and nullability to an OpenApiSchema. |
| | | 655 | | /// </summary> |
| | | 656 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 657 | | /// <param name="schema">The OpenApiSchema to apply attributes to.</param> |
| | | 658 | | private static void ApplySchemaType(OpenApiProperties properties, OpenApiSchema schema) |
| | | 659 | | { |
| | 6 | 660 | | if (properties.Type != OaSchemaType.None) |
| | | 661 | | { |
| | 0 | 662 | | schema.Type = properties.Type switch |
| | 0 | 663 | | { |
| | 0 | 664 | | OaSchemaType.String => JsonSchemaType.String, |
| | 0 | 665 | | OaSchemaType.Number => JsonSchemaType.Number, |
| | 0 | 666 | | OaSchemaType.Integer => JsonSchemaType.Integer, |
| | 0 | 667 | | OaSchemaType.Boolean => JsonSchemaType.Boolean, |
| | 0 | 668 | | OaSchemaType.Array => JsonSchemaType.Array, |
| | 0 | 669 | | OaSchemaType.Object => JsonSchemaType.Object, |
| | 0 | 670 | | OaSchemaType.Null => JsonSchemaType.Null, |
| | 0 | 671 | | _ => schema.Type |
| | 0 | 672 | | }; |
| | | 673 | | } |
| | | 674 | | |
| | 6 | 675 | | if (properties.Nullable) |
| | | 676 | | { |
| | 0 | 677 | | schema.Type |= JsonSchemaType.Null; |
| | | 678 | | } |
| | 6 | 679 | | } |
| | | 680 | | |
| | | 681 | | /// <summary> |
| | | 682 | | /// Applies format and numeric bounds to an OpenApiSchema. |
| | | 683 | | /// </summary> |
| | | 684 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 685 | | /// <param name="schema"></param> |
| | | 686 | | private static void ApplyFormatAndNumericBounds(OpenApiProperties properties, OpenApiSchema schema) |
| | | 687 | | { |
| | 6 | 688 | | if (!string.IsNullOrWhiteSpace(properties.Format)) |
| | | 689 | | { |
| | 0 | 690 | | schema.Format = properties.Format; |
| | | 691 | | } |
| | | 692 | | |
| | 6 | 693 | | if (properties.MultipleOf.HasValue) |
| | | 694 | | { |
| | 0 | 695 | | schema.MultipleOf = properties.MultipleOf; |
| | | 696 | | } |
| | | 697 | | |
| | 6 | 698 | | if (!string.IsNullOrWhiteSpace(properties.Maximum)) |
| | | 699 | | { |
| | 0 | 700 | | schema.Maximum = properties.Maximum; |
| | 0 | 701 | | if (properties.ExclusiveMaximum) |
| | | 702 | | { |
| | 0 | 703 | | schema.ExclusiveMaximum = properties.Maximum; |
| | | 704 | | } |
| | | 705 | | } |
| | | 706 | | |
| | 6 | 707 | | if (!string.IsNullOrWhiteSpace(properties.Minimum)) |
| | | 708 | | { |
| | 0 | 709 | | schema.Minimum = properties.Minimum; |
| | 0 | 710 | | if (properties.ExclusiveMinimum) |
| | | 711 | | { |
| | 0 | 712 | | schema.ExclusiveMinimum = properties.Minimum; |
| | | 713 | | } |
| | | 714 | | } |
| | 6 | 715 | | } |
| | | 716 | | |
| | | 717 | | /// <summary> |
| | | 718 | | /// Applies length and pattern constraints to an OpenApiSchema. |
| | | 719 | | /// </summary> |
| | | 720 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 721 | | /// <param name="schema"></param> |
| | | 722 | | private static void ApplyLengthAndPattern(OpenApiProperties properties, OpenApiSchema schema) |
| | | 723 | | { |
| | 6 | 724 | | if (properties.MaxLength >= 0) |
| | | 725 | | { |
| | 0 | 726 | | schema.MaxLength = properties.MaxLength; |
| | | 727 | | } |
| | | 728 | | |
| | 6 | 729 | | if (properties.MinLength >= 0) |
| | | 730 | | { |
| | 0 | 731 | | schema.MinLength = properties.MinLength; |
| | | 732 | | } |
| | | 733 | | |
| | 6 | 734 | | if (!string.IsNullOrWhiteSpace(properties.Pattern)) |
| | | 735 | | { |
| | 0 | 736 | | schema.Pattern = properties.Pattern; |
| | | 737 | | } |
| | 6 | 738 | | } |
| | | 739 | | |
| | | 740 | | /// <summary> |
| | | 741 | | /// Applies collection constraints to an OpenApiSchema. |
| | | 742 | | /// </summary> |
| | | 743 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 744 | | /// <param name="schema">The OpenApiSchema to apply attributes to.</param> |
| | | 745 | | private static void ApplyCollectionConstraints(OpenApiProperties properties, OpenApiSchema schema) |
| | | 746 | | { |
| | 6 | 747 | | if (properties.MaxItems >= 0) |
| | | 748 | | { |
| | 0 | 749 | | schema.MaxItems = properties.MaxItems; |
| | | 750 | | } |
| | | 751 | | |
| | 6 | 752 | | if (properties.MinItems >= 0) |
| | | 753 | | { |
| | 0 | 754 | | schema.MinItems = properties.MinItems; |
| | | 755 | | } |
| | | 756 | | |
| | 6 | 757 | | if (properties.UniqueItems) |
| | | 758 | | { |
| | 0 | 759 | | schema.UniqueItems = true; |
| | | 760 | | } |
| | | 761 | | |
| | 6 | 762 | | if (properties.MaxProperties >= 0) |
| | | 763 | | { |
| | 0 | 764 | | schema.MaxProperties = properties.MaxProperties; |
| | | 765 | | } |
| | | 766 | | |
| | 6 | 767 | | if (properties.MinProperties >= 0) |
| | | 768 | | { |
| | 0 | 769 | | schema.MinProperties = properties.MinProperties; |
| | | 770 | | } |
| | 6 | 771 | | } |
| | | 772 | | |
| | | 773 | | private static void ApplyFlags(OpenApiProperties properties, OpenApiSchema schema) |
| | | 774 | | { |
| | 6 | 775 | | schema.ReadOnly = properties.ReadOnly; |
| | 6 | 776 | | schema.WriteOnly = properties.WriteOnly; |
| | 6 | 777 | | schema.Deprecated = properties.Deprecated; |
| | 6 | 778 | | schema.AdditionalPropertiesAllowed = properties.AdditionalPropertiesAllowed; |
| | 6 | 779 | | schema.UnevaluatedProperties = properties.UnevaluatedProperties; |
| | 6 | 780 | | } |
| | | 781 | | |
| | | 782 | | private static void ApplyExamplesAndDefaults(OpenApiProperties properties, OpenApiSchema schema) |
| | | 783 | | { |
| | 6 | 784 | | if (properties.Default is not null) |
| | | 785 | | { |
| | 0 | 786 | | schema.Default = ToNode(properties.Default); |
| | | 787 | | } |
| | | 788 | | |
| | 6 | 789 | | if (properties.Example is not null) |
| | | 790 | | { |
| | 0 | 791 | | schema.Example = ToNode(properties.Example); |
| | | 792 | | } |
| | | 793 | | |
| | 6 | 794 | | if (properties.Enum is { Length: > 0 }) |
| | | 795 | | { |
| | 0 | 796 | | schema.Enum = [.. properties.Enum.Select(ToNode).OfType<JsonNode>()]; |
| | | 797 | | } |
| | | 798 | | |
| | 6 | 799 | | if (properties.Required is { Length: > 0 }) |
| | | 800 | | { |
| | 0 | 801 | | schema.Required ??= new HashSet<string>(StringComparer.Ordinal); |
| | 0 | 802 | | foreach (var r in properties.Required) |
| | | 803 | | { |
| | 0 | 804 | | _ = schema.Required.Add(r); |
| | | 805 | | } |
| | | 806 | | } |
| | 6 | 807 | | } |
| | | 808 | | |
| | | 809 | | /// <summary> |
| | | 810 | | /// Applies reference schema attributes to an OpenApiSchemaReference. |
| | | 811 | | /// </summary> |
| | | 812 | | /// <param name="properties">The OpenApiProperties containing attributes to apply.</param> |
| | | 813 | | /// <param name="reference">The OpenApiSchemaReference to apply attributes to.</param> |
| | | 814 | | private static void ApplyReferenceSchemaAttributes(OpenApiProperties properties, OpenApiSchemaReference reference) |
| | | 815 | | { |
| | | 816 | | // Description/Title can live on a reference proxy in v2 (and serialize alongside $ref) |
| | 0 | 817 | | if (!string.IsNullOrWhiteSpace(properties.Description)) |
| | | 818 | | { |
| | 0 | 819 | | reference.Description = properties.Description; |
| | | 820 | | } |
| | | 821 | | |
| | 0 | 822 | | if (!string.IsNullOrWhiteSpace(properties.Title)) |
| | | 823 | | { |
| | 0 | 824 | | reference.Title = properties.Title; |
| | | 825 | | } |
| | | 826 | | |
| | | 827 | | // Example/Default/Enum aren’t typically set on the ref node itself; |
| | | 828 | | // attach such metadata to the component target instead if you need it. |
| | 0 | 829 | | } |
| | | 830 | | |
| | | 831 | | /// <summary> |
| | | 832 | | /// Determines if a type is considered primitive-like for schema generation. |
| | | 833 | | /// </summary> |
| | | 834 | | /// <param name="t">The type to check.</param> |
| | | 835 | | /// <returns>True if the type is considered primitive-like; otherwise, false.</returns> |
| | | 836 | | private static bool IsPrimitiveLike(Type t) |
| | 41 | 837 | | => t.IsPrimitive || t == typeof(string) || t == typeof(decimal) || t == typeof(DateTime) || |
| | 41 | 838 | | t == typeof(Guid) || t == typeof(object) || t == typeof(OaString) || t == typeof(OaInteger) || |
| | 41 | 839 | | t == typeof(OaNumber) || t == typeof(OaBoolean); |
| | | 840 | | |
| | | 841 | | #endregion |
| | | 842 | | |
| | | 843 | | /// <summary> |
| | | 844 | | /// Converts a .NET object to a JsonNode representation. |
| | | 845 | | /// </summary> |
| | | 846 | | /// <param name="value">The .NET object to convert.</param> |
| | | 847 | | /// <returns>A JsonNode representing the object, or null if the object is null.</returns> |
| | | 848 | | internal static JsonNode? ToNode(object? value) |
| | | 849 | | { |
| | 31 | 850 | | if (value is null) |
| | | 851 | | { |
| | 0 | 852 | | return null; |
| | | 853 | | } |
| | | 854 | | |
| | | 855 | | // handle common types |
| | 31 | 856 | | return value switch |
| | 31 | 857 | | { |
| | 1 | 858 | | bool b => JsonValue.Create(b), |
| | 19 | 859 | | string s => JsonValue.Create(s), |
| | 6 | 860 | | sbyte or byte or short or ushort or int or uint or long or ulong => JsonValue.Create(Convert.ToInt64(value)) |
| | 0 | 861 | | float or double or decimal => JsonValue.Create(Convert.ToDouble(value)), |
| | 1 | 862 | | DateTime dt => JsonValue.Create(dt.ToString("o")), |
| | 0 | 863 | | Guid g => JsonValue.Create(g.ToString()), |
| | 31 | 864 | | // Hashtable/IDictionary -> JsonObject |
| | 0 | 865 | | System.Collections.IDictionary dict => ToJsonObject(dict), |
| | 31 | 866 | | // Generic enumerable -> JsonArray |
| | 0 | 867 | | IEnumerable<object?> seq => new JsonArray([.. seq.Select(ToNode)]), |
| | 31 | 868 | | // Non-generic enumerable -> JsonArray |
| | 0 | 869 | | System.Collections.IEnumerable en when value is not string => ToJsonArray(en), |
| | 4 | 870 | | _ => ToNodeFromPocoOrString(value) |
| | 31 | 871 | | }; |
| | | 872 | | } |
| | | 873 | | |
| | | 874 | | private static JsonObject ToJsonObject(System.Collections.IDictionary dict) |
| | | 875 | | { |
| | 0 | 876 | | var obj = new JsonObject(); |
| | 0 | 877 | | foreach (System.Collections.DictionaryEntry de in dict) |
| | | 878 | | { |
| | 0 | 879 | | if (de.Key is null) { continue; } |
| | 0 | 880 | | var k = de.Key.ToString() ?? string.Empty; |
| | 0 | 881 | | obj[k] = ToNode(de.Value); |
| | | 882 | | } |
| | 0 | 883 | | return obj; |
| | | 884 | | } |
| | | 885 | | |
| | | 886 | | private static JsonArray ToJsonArray(System.Collections.IEnumerable en) |
| | | 887 | | { |
| | 0 | 888 | | var arr = new JsonArray(); |
| | 0 | 889 | | foreach (var item in en) |
| | | 890 | | { |
| | 0 | 891 | | arr.Add(ToNode(item)); |
| | | 892 | | } |
| | 0 | 893 | | return arr; |
| | | 894 | | } |
| | | 895 | | |
| | | 896 | | private static JsonNode ToNodeFromPocoOrString(object value) |
| | | 897 | | { |
| | | 898 | | // Try POCO reflection |
| | 4 | 899 | | var t = value.GetType(); |
| | | 900 | | // Ignore types that are clearly not POCOs |
| | 4 | 901 | | if (!t.IsPrimitive && t != typeof(string) && !typeof(System.Collections.IEnumerable).IsAssignableFrom(t)) |
| | | 902 | | { |
| | 4 | 903 | | var props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| | 4 | 904 | | if (props.Length > 0) |
| | | 905 | | { |
| | 4 | 906 | | var obj = new JsonObject(); |
| | 16 | 907 | | foreach (var p in props) |
| | | 908 | | { |
| | 4 | 909 | | if (!p.CanRead) { continue; } |
| | 4 | 910 | | var v = p.GetValue(value); |
| | 4 | 911 | | if (v is null) { continue; } |
| | 4 | 912 | | obj[p.Name] = ToNode(v); |
| | | 913 | | } |
| | 4 | 914 | | return obj; |
| | | 915 | | } |
| | | 916 | | } |
| | | 917 | | // Fallback |
| | 0 | 918 | | return JsonValue.Create(value?.ToString() ?? string.Empty); |
| | | 919 | | } |
| | | 920 | | |
| | | 921 | | /// <summary> |
| | | 922 | | /// Ensures that a schema component exists for a complex .NET type. |
| | | 923 | | /// </summary> |
| | | 924 | | /// <param name="complexType">The complex .NET type.</param> |
| | | 925 | | private void EnsureSchemaComponent(Type complexType) |
| | | 926 | | { |
| | 0 | 927 | | if (Document.Components?.Schemas != null && Document.Components.Schemas.ContainsKey(complexType.Name)) |
| | | 928 | | { |
| | 0 | 929 | | return; |
| | | 930 | | } |
| | | 931 | | |
| | 0 | 932 | | var temp = new HashSet<Type>(); |
| | 0 | 933 | | BuildSchema(complexType, temp); |
| | 0 | 934 | | } |
| | | 935 | | } |