3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . IO ;
6
7
using System . Linq ;
7
8
using System . Text ;
8
9
using System . Threading . Tasks ;
@@ -16,7 +17,9 @@ namespace Microsoft.OpenApi.Services
16
17
/// </summary>
17
18
public class OpenApiWorkspace
18
19
{
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 > ( ) ;
20
23
21
24
/// <summary>
22
25
/// A list of OpenApiDocuments contained in the workspace
@@ -30,44 +33,52 @@ public IEnumerable<OpenApiDocument> Documents {
30
33
/// <summary>
31
34
/// A list of document fragments that are contained in the workspace
32
35
/// </summary>
33
- public IEnumerable < IOpenApiFragment > Fragments { get ; }
36
+ public IEnumerable < IOpenApiElement > Fragments { get ; }
34
37
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 ; }
35
47
36
48
public bool Contains ( string location )
37
49
{
38
- return _documents . ContainsKey ( location ) ;
50
+ return _documents . ContainsKey ( ToLocationUrl ( location ) ) ;
39
51
}
40
52
41
- public void AddDocument ( string location , OpenApiDocument document )
53
+ public void AddDocument ( string location , OpenApiDocument document )
42
54
{
43
55
document . Workspace = this ;
44
- _documents . Add ( location , document ) ;
56
+ _documents . Add ( ToLocationUrl ( location ) , document ) ;
45
57
}
46
58
47
- public void AddFragment ( string location , IOpenApiFragment fragment )
59
+ public void AddFragment ( string location , IOpenApiElement fragment )
48
60
{
49
-
61
+ _fragments . Add ( ToLocationUrl ( location ) , fragment ) ;
50
62
}
51
63
52
- public void AddArtifact < T > ( string location , T artifact )
64
+ public void AddArtifact ( string location , Stream artifact )
53
65
{
54
-
66
+ _artifacts . Add ( ToLocationUrl ( location ) , artifact ) ;
55
67
}
56
68
57
69
public IOpenApiReferenceable ResolveReference ( OpenApiReference reference )
58
70
{
59
- if ( ! _documents . TryGetValue ( reference . ExternalResource , out var doc ) )
71
+ if ( ! _documents . TryGetValue ( new Uri ( reference . ExternalResource , UriKind . RelativeOrAbsolute ) , out var doc ) )
60
72
{
61
73
return null ;
62
74
}
63
75
64
76
return doc . ResolveReference ( reference , true ) ;
65
77
}
66
78
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
+ }
72
83
}
73
84
}
0 commit comments