|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.IO; |
| 7 | +using System.Linq; |
7 | 8 |
|
8 | 9 | namespace Microsoft.OpenApi |
9 | 10 | { |
@@ -326,6 +327,90 @@ public bool Contains(string location) |
326 | 327 | return default; |
327 | 328 | } |
328 | 329 |
|
| 330 | + /// <summary> |
| 331 | + /// Recursively resolves a schema from a URI fragment. |
| 332 | + /// </summary> |
| 333 | + /// <param name="location"></param> |
| 334 | + /// <returns></returns> |
| 335 | + internal IOpenApiSchema? ResolveJsonSchemaReference(string location) |
| 336 | + { |
| 337 | + /* Enables resolving references for nested subschemas |
| 338 | + * Examples: |
| 339 | + * #/components/schemas/person/properties/address" |
| 340 | + * #/components/schemas/human/allOf/0 |
| 341 | + */ |
| 342 | + |
| 343 | + if (string.IsNullOrEmpty(location)) return default; |
| 344 | + |
| 345 | + var uri = ToLocationUrl(location); |
| 346 | + string[] pathSegments; |
| 347 | + |
| 348 | + if (uri is not null) |
| 349 | + { |
| 350 | + pathSegments = uri.Fragment.Split(['/'], StringSplitOptions.RemoveEmptyEntries); |
| 351 | + |
| 352 | + // Build the base path for the root schema: "#/components/schemas/person" |
| 353 | + var fragment = OpenApiConstants.ComponentsSegment + ReferenceType.Schema.GetDisplayName() + ComponentSegmentSeparator + pathSegments[3]; |
| 354 | + var uriBuilder = new UriBuilder(uri) |
| 355 | + { |
| 356 | + Fragment = fragment |
| 357 | + }; // to avoid escaping the # character in the resulting Uri |
| 358 | + |
| 359 | + if (_IOpenApiReferenceableRegistry.TryGetValue(uriBuilder.Uri, out var schema) && schema is IOpenApiSchema targetSchema) |
| 360 | + { |
| 361 | + // traverse remaining segments after fetching the base schema |
| 362 | + var remainingSegments = pathSegments.Skip(4).ToArray(); |
| 363 | + return ResolveSubSchema(targetSchema, remainingSegments); |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + return default; |
| 368 | + } |
| 369 | + |
| 370 | + internal static IOpenApiSchema? ResolveSubSchema(IOpenApiSchema schema, string[] pathSegments) |
| 371 | + { |
| 372 | + // Traverse schema object to resolve subschemas |
| 373 | + if (pathSegments.Length == 0) |
| 374 | + { |
| 375 | + return schema; |
| 376 | + } |
| 377 | + var currentSegment = pathSegments[0]; |
| 378 | + pathSegments = [.. pathSegments.Skip(1)]; // skip one segment for the next recursive call |
| 379 | + |
| 380 | + switch (currentSegment) |
| 381 | + { |
| 382 | + case OpenApiConstants.Properties: |
| 383 | + var propName = pathSegments[0]; |
| 384 | + if (schema.Properties != null && schema.Properties.TryGetValue(propName, out var propSchema)) |
| 385 | + return ResolveSubSchema(propSchema, [.. pathSegments.Skip(1)]); |
| 386 | + break; |
| 387 | + case OpenApiConstants.Items: |
| 388 | + return schema.Items is OpenApiSchema itemsSchema ? ResolveSubSchema(itemsSchema, pathSegments) : null; |
| 389 | + |
| 390 | + case OpenApiConstants.AdditionalProperties: |
| 391 | + return schema.AdditionalProperties is OpenApiSchema additionalSchema ? ResolveSubSchema(additionalSchema, pathSegments) : null; |
| 392 | + case OpenApiConstants.AllOf: |
| 393 | + case OpenApiConstants.AnyOf: |
| 394 | + case OpenApiConstants.OneOf: |
| 395 | + if (!int.TryParse(pathSegments[0], out var index)) return null; |
| 396 | + |
| 397 | + var list = currentSegment switch |
| 398 | + { |
| 399 | + OpenApiConstants.AllOf => schema.AllOf, |
| 400 | + OpenApiConstants.AnyOf => schema.AnyOf, |
| 401 | + OpenApiConstants.OneOf => schema.OneOf, |
| 402 | + _ => null |
| 403 | + }; |
| 404 | + |
| 405 | + // recurse into the indexed subschema if valid |
| 406 | + if (list != null && index < list.Count) |
| 407 | + return ResolveSubSchema(list[index], [.. pathSegments.Skip(1)]); |
| 408 | + break; |
| 409 | + } |
| 410 | + |
| 411 | + return null; |
| 412 | + } |
| 413 | + |
329 | 414 | private Uri? ToLocationUrl(string location) |
330 | 415 | { |
331 | 416 | if (BaseUrl is not null) |
|
0 commit comments