Skip to content

Commit 7e77070

Browse files
committed
Get the $ref pointer to a pathItem object and resolve the reference by returning the pathItem matching the reference Id in the components object
1 parent 093f761 commit 7e77070

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/Microsoft.OpenApi.Readers/V3/OpenApiPathItemDeserializer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public static OpenApiPathItem LoadPathItem(ParseNode node)
5656
{
5757
var mapNode = node.CheckMapNode("PathItem");
5858

59+
var pointer = mapNode.GetReferencePointer();
60+
61+
if (pointer != null)
62+
{
63+
return new OpenApiPathItem()
64+
{
65+
UnresolvedReference = true,
66+
Reference = node.Context.VersionService.ConvertToOpenApiReference(pointer, ReferenceType.PathItem)
67+
};
68+
}
69+
5970
var pathItem = new OpenApiPathItem();
6071

6172
ParseMap(mapNode, pathItem, _pathItemFixedFields, _pathItemPatternFields);

src/Microsoft.OpenApi.Readers/V3/OpenApiV3VersionService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ private OpenApiReference ParseLocalReference(string localReference)
165165
if (segments[1] == "components")
166166
{
167167
var referenceType = segments[2].GetEnumFromDisplayName<ReferenceType>();
168-
return new OpenApiReference { Type = referenceType, Id = segments[3] };
168+
var refId = segments[3];
169+
if (segments[2] == "pathItems")
170+
{
171+
refId = "/" + segments[3];
172+
};
173+
return new OpenApiReference { Type = referenceType, Id = refId };
169174
}
170175
}
171176

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,10 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
505505
{
506506
case ReferenceType.Schema:
507507
return this.Components.Schemas[reference.Id];
508-
508+
509+
case ReferenceType.PathItem:
510+
return this.Components.PathItems[reference.Id];
511+
509512
case ReferenceType.Response:
510513
return this.Components.Responses[reference.Id];
511514

src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -70,6 +70,7 @@ public override void Visit(OpenApiComponents components)
7070
ResolveMap(components.Callbacks);
7171
ResolveMap(components.Examples);
7272
ResolveMap(components.Schemas);
73+
ResolveMap(components.PathItems);
7374
ResolveMap(components.SecuritySchemes);
7475
ResolveMap(components.Headers);
7576
}
@@ -83,6 +84,15 @@ public override void Visit(IDictionary<string, OpenApiCallback> callbacks)
8384
ResolveMap(callbacks);
8485
}
8586

87+
/// <summary>
88+
/// Resolves all references used in webhooks
89+
/// </summary>
90+
/// <param name="webhooks"></param>
91+
public override void Visit(IDictionary<string, OpenApiPathItem> webhooks)
92+
{
93+
ResolveMap(webhooks);
94+
}
95+
8696
/// <summary>
8797
/// Resolve all references used in an operation
8898
/// </summary>
@@ -301,7 +311,7 @@ private void ResolveTags(IList<OpenApiTag> tags)
301311

302312
private bool IsUnresolvedReference(IOpenApiReferenceable possibleReference)
303313
{
304-
return (possibleReference != null && possibleReference.UnresolvedReference);
314+
return possibleReference != null && possibleReference.UnresolvedReference;
305315
}
306316
}
307317
}

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ internal void Walk(OpenApiComponents components)
129129
}
130130
});
131131

132+
Walk(OpenApiConstants.PathItems, () =>
133+
{
134+
if (components.PathItems != null)
135+
{
136+
foreach (var path in components.PathItems)
137+
{
138+
Walk(path.Key, () => Walk(path.Value, isComponent: true));
139+
}
140+
}
141+
});
142+
132143
Walk(OpenApiConstants.Parameters, () =>
133144
{
134145
if (components.Parameters != null)
@@ -233,15 +244,17 @@ internal void Walk(IDictionary<string, OpenApiPathItem> webhooks)
233244
}
234245

235246
_visitor.Visit(webhooks);
236-
247+
237248
// Visit Webhooks
238249
if (webhooks != null)
239250
{
240251
foreach (var pathItem in webhooks)
241252
{
242-
Walk(pathItem.Key, () => Walk(pathItem.Value));
253+
_visitor.CurrentKeys.Path = pathItem.Key;
254+
Walk(pathItem.Key, () => Walk(pathItem.Value));// JSON Pointer uses ~1 as an escape character for /
255+
_visitor.CurrentKeys.Path = null;
243256
}
244-
}
257+
};
245258
}
246259

247260
/// <summary>
@@ -441,9 +454,9 @@ internal void Walk(OpenApiServerVariable serverVariable)
441454
/// <summary>
442455
/// Visits <see cref="OpenApiPathItem"/> and child objects
443456
/// </summary>
444-
internal void Walk(OpenApiPathItem pathItem)
457+
internal void Walk(OpenApiPathItem pathItem, bool isComponent = false)
445458
{
446-
if (pathItem == null)
459+
if (pathItem == null || ProcessAsReference(pathItem, isComponent))
447460
{
448461
return;
449462
}

0 commit comments

Comments
 (0)