Skip to content

Commit aa40a85

Browse files
committed
Add a visitor that walks through an OpenApi document and sets the host document value for proxy reference objects
1 parent 5955a51 commit aa40a85

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public OpenApiDocument Read(JsonNode input, out OpenApiDiagnostic diagnostic)
5959
{
6060
throw new InvalidOperationException("Cannot load external refs using the synchronous Read, use ReadAsync instead.");
6161
}
62+
63+
SetHostDocument(document);
6264
}
6365
catch (OpenApiException ex)
6466
{
@@ -107,6 +109,8 @@ public async Task<ReadResult> ReadAsync(JsonNode input, CancellationToken cancel
107109
diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings);
108110
}
109111
}
112+
113+
SetHostDocument(document);
110114
}
111115
catch (OpenApiException ex)
112116
{
@@ -145,6 +149,11 @@ private Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, Cance
145149
return workspaceLoader.LoadAsync(new() { ExternalResource = "/" }, document, null, cancellationToken);
146150
}
147151

152+
private void SetHostDocument(OpenApiDocument document)
153+
{
154+
document.SetHostDocument();
155+
}
156+
148157
/// <summary>
149158
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
150159
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ public IEnumerable<OpenApiError> ResolveReferences()
452452
return resolver.Errors;
453453
}
454454

455+
/// <summary>
456+
/// Walks the OpenApiDocument and sets the host document for all referenceable objects
457+
/// </summary>
458+
public void SetHostDocument()
459+
{
460+
var resolver = new HostDocumentResolver(this);
461+
var walker = new OpenApiWalker(resolver);
462+
walker.Walk(this);
463+
}
464+
455465
/// <summary>
456466
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
457467
/// </summary>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.OpenApi.Interfaces;
5+
using Microsoft.OpenApi.Models;
6+
7+
namespace Microsoft.OpenApi.Services
8+
{
9+
internal class HostDocumentResolver : OpenApiVisitorBase
10+
{
11+
private OpenApiDocument _currentDocument;
12+
13+
public HostDocumentResolver(OpenApiDocument currentDocument)
14+
{
15+
_currentDocument = currentDocument;
16+
}
17+
18+
/// <summary>
19+
/// Visits the referenceable element in the host document
20+
/// </summary>
21+
/// <param name="referenceable">The referenceable element in the doc.</param>
22+
public override void Visit(IOpenApiReferenceable referenceable)
23+
{
24+
if (referenceable.Reference != null)
25+
{
26+
referenceable.Reference.HostDocument = _currentDocument;
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)