@@ -45,35 +45,102 @@ public IEnumerable<OpenApiDocument> Documents {
45
45
/// </summary>
46
46
public IEnumerable < Stream > Artifacts { get ; }
47
47
48
+ /// <summary>
49
+ /// Initialize workspace pointing to a base URL to allow resolving relative document locations. Use a file:// url to point to a folder
50
+ /// </summary>
51
+ /// <param name="baseUrl"></param>
52
+ public OpenApiWorkspace ( Uri baseUrl )
53
+ {
54
+ BaseUrl = baseUrl ;
55
+ }
56
+
57
+ /// <summary>
58
+ /// Initialize workspace using current directory as the default location.
59
+ /// </summary>
60
+ public OpenApiWorkspace ( )
61
+ {
62
+ BaseUrl = new Uri ( "file://" + Environment . CurrentDirectory + "\\ " ) ;
63
+ }
64
+
65
+ /// <summary>
66
+ /// Verify if workspace contains a document based on its URL.
67
+ /// </summary>
68
+ /// <param name="location">A relative or absolute URL of the file. Use file:// for folder locations.</param>
69
+ /// <returns>Returns true if a matching document is found.</returns>
48
70
public bool Contains ( string location )
49
71
{
50
- return _documents . ContainsKey ( ToLocationUrl ( location ) ) ;
72
+ Uri key = ToLocationUrl ( location ) ;
73
+ return _documents . ContainsKey ( key ) || _fragments . ContainsKey ( key ) || _artifacts . ContainsKey ( key ) ;
51
74
}
52
75
76
+ /// <summary>
77
+ /// Add an OpenApiDocument to the workspace.
78
+ /// </summary>
79
+ /// <param name="location"></param>
80
+ /// <param name="document"></param>
53
81
public void AddDocument ( string location , OpenApiDocument document )
54
82
{
55
83
document . Workspace = this ;
56
84
_documents . Add ( ToLocationUrl ( location ) , document ) ;
57
85
}
58
86
59
- public void AddFragment ( string location , IOpenApiElement fragment )
87
+ /// <summary>
88
+ /// Adds a fragment of an OpenApiDocument to the workspace.
89
+ /// </summary>
90
+ /// <param name="location"></param>
91
+ /// <param name="fragment"></param>
92
+ /// <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
93
+ /// to be able to point into the fragment. Keeping it private until we figure it out.
94
+ /// </remarks>
95
+ private void AddFragment ( string location , IOpenApiElement fragment )
60
96
{
61
97
_fragments . Add ( ToLocationUrl ( location ) , fragment ) ;
62
98
}
63
99
100
+ /// <summary>
101
+ /// Add a stream based artificat to the workspace. Useful for images, examples, alternative schemas.
102
+ /// </summary>
103
+ /// <param name="location"></param>
104
+ /// <param name="artifact"></param>
64
105
public void AddArtifact ( string location , Stream artifact )
65
106
{
66
107
_artifacts . Add ( ToLocationUrl ( location ) , artifact ) ;
67
108
}
68
109
110
+ /// <summary>
111
+ /// Returns the target of an OpenApiReference from within the workspace.
112
+ /// </summary>
113
+ /// <param name="reference">An instance of an OpenApiReference</param>
114
+ /// <returns></returns>
69
115
public IOpenApiReferenceable ResolveReference ( OpenApiReference reference )
70
116
{
71
- if ( ! _documents . TryGetValue ( new Uri ( reference . ExternalResource , UriKind . RelativeOrAbsolute ) , out var doc ) )
117
+ if ( _documents . TryGetValue ( new Uri ( BaseUrl , reference . ExternalResource ) , out var doc ) )
72
118
{
73
- return null ;
119
+ return doc . ResolveReference ( reference , true ) ;
74
120
}
75
-
76
- return doc . ResolveReference ( reference , true ) ;
121
+ else if ( _fragments . TryGetValue ( new Uri ( BaseUrl , reference . ExternalResource ) , out var fragment ) )
122
+ {
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
+ }
132
+ }
133
+ return null ;
134
+ }
135
+
136
+ /// <summary>
137
+ ///
138
+ /// </summary>
139
+ /// <param name="location"></param>
140
+ /// <returns></returns>
141
+ public Stream GetArtifact ( string location )
142
+ {
143
+ return _artifacts [ ToLocationUrl ( location ) ] ;
77
144
}
78
145
79
146
private Uri ToLocationUrl ( string location )
0 commit comments