Skip to content

Commit 50649b3

Browse files
committed
ISSUE #1228: Resolve path where it is an external reference
1 parent 4fc2a75 commit 50649b3

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

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

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

59+
var pointer = mapNode.GetReferencePointer();
60+
if (pointer != null)
61+
{
62+
var refObject = mapNode.GetReferencedObject<OpenApiPathItem>(ReferenceType.Path, pointer);
63+
return refObject;
64+
}
65+
5966
var pathItem = new OpenApiPathItem();
6067

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ public OpenApiReference ConvertToOpenApiReference(
129129
}
130130
id = localSegments[3];
131131
}
132+
else if (id.StartsWith("/paths/"))
133+
{
134+
var localSegments = segments[1].Split('/');
135+
if (localSegments.Length == 3)
136+
{
137+
// The reference of a path may contain JSON escape character ~1 for the forward-slash character, replace this otherwise
138+
// the reference cannot be resolved.
139+
id = localSegments[2].Replace("~1", "/");
140+
}
141+
else
142+
{
143+
throw new OpenApiException("Referenced Path mismatch");
144+
}
145+
}
132146
else
133147
{
134148
openApiReference.IsFragrament = true;

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
504504
case ReferenceType.Callback:
505505
return this.Components.Callbacks[reference.Id];
506506

507+
case ReferenceType.Path:
508+
return this.Paths[reference.Id];
509+
507510
default:
508511
throw new OpenApiException(Properties.SRResource.InvalidReferenceType);
509512
}

src/Microsoft.OpenApi/Models/ReferenceType.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public enum ReferenceType
5858
/// <summary>
5959
/// Tags item.
6060
/// </summary>
61-
[Display("tags")] Tag
61+
[Display("tags")] Tag,
62+
63+
/// <summary>
64+
/// Paths item.
65+
/// </summary>
66+
[Display("paths")] Path
6267
}
6368
}

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 7 additions & 3 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;
@@ -449,8 +449,12 @@ internal void Walk(OpenApiPathItem pathItem)
449449

450450
if (pathItem != null)
451451
{
452-
Walk(OpenApiConstants.Parameters, () => Walk(pathItem.Parameters));
453-
Walk(pathItem.Operations);
452+
// The path may be a reference
453+
if (!ProcessAsReference(pathItem))
454+
{
455+
Walk(OpenApiConstants.Parameters, () => Walk(pathItem.Parameters));
456+
Walk(pathItem.Operations);
457+
}
454458
}
455459
_visitor.Visit(pathItem as IOpenApiExtensible);
456460

0 commit comments

Comments
 (0)