Skip to content

Commit 78e7eee

Browse files
committed
Work on baseURL for workspace
1 parent 806c067 commit 78e7eee

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
78
using System.Text;
89
using System.Threading.Tasks;
@@ -16,7 +17,9 @@ namespace Microsoft.OpenApi.Services
1617
/// </summary>
1718
public class OpenApiWorkspace
1819
{
19-
private Dictionary<string, OpenApiDocument> _documents = new Dictionary<string, OpenApiDocument>();
20+
private Dictionary<Uri, OpenApiDocument> _documents = new Dictionary<Uri, OpenApiDocument>();
21+
private Dictionary<Uri, IOpenApiElement> _fragments = new Dictionary<Uri, IOpenApiElement>();
22+
private Dictionary<Uri, Stream> _artifacts = new Dictionary<Uri, Stream>();
2023

2124
/// <summary>
2225
/// A list of OpenApiDocuments contained in the workspace
@@ -30,44 +33,52 @@ public IEnumerable<OpenApiDocument> Documents {
3033
/// <summary>
3134
/// A list of document fragments that are contained in the workspace
3235
/// </summary>
33-
public IEnumerable<IOpenApiFragment> Fragments { get; }
36+
public IEnumerable<IOpenApiElement> Fragments { get; }
3437

38+
/// <summary>
39+
/// The base location from where all relative references are resolved
40+
/// </summary>
41+
public Uri BaseUrl { get; }
42+
43+
/// <summary>
44+
/// A list of document fragments that are contained in the workspace
45+
/// </summary>
46+
public IEnumerable<Stream> Artifacts { get; }
3547

3648
public bool Contains(string location)
3749
{
38-
return _documents.ContainsKey(location);
50+
return _documents.ContainsKey(ToLocationUrl(location));
3951
}
4052

41-
public void AddDocument(string location, OpenApiDocument document)
53+
public void AddDocument(string location, OpenApiDocument document)
4254
{
4355
document.Workspace = this;
44-
_documents.Add(location, document);
56+
_documents.Add(ToLocationUrl(location), document);
4557
}
4658

47-
public void AddFragment(string location, IOpenApiFragment fragment)
59+
public void AddFragment(string location, IOpenApiElement fragment)
4860
{
49-
61+
_fragments.Add(ToLocationUrl(location), fragment);
5062
}
5163

52-
public void AddArtifact<T>(string location, T artifact)
64+
public void AddArtifact(string location, Stream artifact)
5365
{
54-
66+
_artifacts.Add(ToLocationUrl(location), artifact);
5567
}
5668

5769
public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
5870
{
59-
if (!_documents.TryGetValue(reference.ExternalResource,out var doc))
71+
if (!_documents.TryGetValue(new Uri(reference.ExternalResource,UriKind.RelativeOrAbsolute),out var doc))
6072
{
6173
return null;
6274
}
6375

6476
return doc.ResolveReference(reference,true);
6577
}
6678

67-
}
68-
69-
public interface IOpenApiFragment
70-
{
71-
IOpenApiReferenceable ResolveReference(OpenApiReference reference);
79+
private Uri ToLocationUrl(string location)
80+
{
81+
return new Uri(BaseUrl, location);
82+
}
7283
}
7384
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,30 @@ public void OpenApiWorkspacesAllowDocumentsToReferenceEachOther_short()
138138
[Fact]
139139
public void OpenApiWorkspacesShouldNormalizeDocumentLocations()
140140
{
141+
// what does normalize mean?
142+
// If we use Urls as locators then normalization happens automatically.
143+
144+
// How do we set a base location for a workspace?
145+
// A base could be a folder. Should we use file://
146+
// A base could be a root url
147+
// Are absolute locations allowed?
148+
// Can a base URI change once a workspace has been created?
149+
// What should be the default base URL?
150+
// Can we infer it from a root document?
151+
// Is the root document the first document loaded?
152+
// Can we load multiple APIs into a Workspace? Does root document make sense?
153+
// What data type should "location" really be? Is it a Uri?
154+
//
141155
Assert.True(false);
142156
}
143157

144158
// Enable Workspace to load from any reader, not just streams.
145159

146160
// Test fragments
161+
public void OpenApiWorkspacesShouldLoadDocumentFragments()
162+
{
163+
Assert.True(false);
164+
}
147165

148166
// Test artifacts
149167

0 commit comments

Comments
 (0)