Skip to content

Commit 23c8417

Browse files
Update implementation of OpenApiWorkspace.ResolveReference to support referencing document fragments
1 parent c86fd89 commit 23c8417

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text;
99
using System.Threading.Tasks;
10+
using Microsoft.OpenApi.Extensions;
1011
using Microsoft.OpenApi.Interfaces;
1112
using Microsoft.OpenApi.Models;
1213

@@ -18,7 +19,7 @@ namespace Microsoft.OpenApi.Services
1819
public class OpenApiWorkspace
1920
{
2021
private Dictionary<Uri, OpenApiDocument> _documents = new Dictionary<Uri, OpenApiDocument>();
21-
private Dictionary<Uri, IOpenApiElement> _fragments = new Dictionary<Uri, IOpenApiElement>();
22+
private Dictionary<Uri, IOpenApiReferenceable> _fragments = new Dictionary<Uri, IOpenApiReferenceable>();
2223
private Dictionary<Uri, Stream> _artifacts = new Dictionary<Uri, Stream>();
2324

2425
/// <summary>
@@ -92,7 +93,7 @@ public void AddDocument(string location, OpenApiDocument document)
9293
/// <remarks>Not sure how this is going to work. Does the reference just point to the fragment as a whole, or do we need to
9394
/// to be able to point into the fragment. Keeping it private until we figure it out.
9495
/// </remarks>
95-
private void AddFragment(string location, IOpenApiElement fragment)
96+
public void AddFragment(string location, IOpenApiReferenceable fragment)
9697
{
9798
_fragments.Add(ToLocationUrl(location), fragment);
9899
}
@@ -114,21 +115,14 @@ public void AddArtifact(string location, Stream artifact)
114115
/// <returns></returns>
115116
public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
116117
{
117-
if (_documents.TryGetValue(new Uri(BaseUrl,reference.ExternalResource),out var doc))
118+
if (_documents.TryGetValue(new Uri(BaseUrl, reference.ExternalResource), out var doc))
118119
{
119-
return doc.ResolveReference(reference, true);
120+
return doc.ResolveReference(reference, true);
120121
}
121122
else if (_fragments.TryGetValue(new Uri(BaseUrl, reference.ExternalResource), out var fragment))
122123
{
123-
var frag = fragment as IOpenApiReferenceable;
124-
if (frag != null)
125-
{
126-
return null; // frag.ResolveReference(reference, true); // IOpenApiElement needs to implement ResolveReference
127-
}
128-
else
129-
{
130-
return null;
131-
}
124+
var jsonPointer = $"/{reference.Id ?? string.Empty}";
125+
return fragment.ResolveReference(jsonPointer);
132126
}
133127
return null;
134128
}

test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,50 @@ public void OpenApiWorkspacesShouldLoadDocumentFragments()
155155
Assert.True(false);
156156
}
157157

158+
[Fact]
159+
public void OpenApiWorkspacesCanResolveReferencesToDocumentFragments()
160+
{
161+
// Arrange
162+
var workspace = new OpenApiWorkspace();
163+
var schemaFragment = new OpenApiSchema { Type = "string", Description = "Schema from a fragment" };
164+
workspace.AddFragment("fragment", schemaFragment);
165+
166+
// Act
167+
var schema = workspace.ResolveReference(new OpenApiReference()
168+
{
169+
ExternalResource = "fragment"
170+
}) as OpenApiSchema;
171+
172+
// Assert
173+
Assert.NotNull(schema);
174+
Assert.Equal("Schema from a fragment", schema.Description);
175+
}
176+
177+
[Fact]
178+
public void OpenApiWorkspacesCanResolveReferencesToDocumentFragmentsWithJsonPointers()
179+
{
180+
// Arrange
181+
var workspace = new OpenApiWorkspace();
182+
var responseFragment = new OpenApiResponse()
183+
{
184+
Headers = new Dictionary<string, OpenApiHeader>
185+
{
186+
{ "header1", new OpenApiHeader() }
187+
}
188+
};
189+
workspace.AddFragment("fragment", responseFragment);
190+
191+
// Act
192+
var resolvedElement = workspace.ResolveReference(new OpenApiReference()
193+
{
194+
Id = "headers/header1",
195+
ExternalResource = "fragment"
196+
});
197+
198+
// Assert
199+
Assert.Same(responseFragment.Headers["header1"], resolvedElement);
200+
}
201+
158202

159203
// Test artifacts
160204

0 commit comments

Comments
 (0)