@@ -45,35 +45,102 @@ public IEnumerable<OpenApiDocument> Documents {
4545 /// </summary>
4646 public IEnumerable < Stream > Artifacts { get ; }
4747
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>
4870 public bool Contains ( string location )
4971 {
50- return _documents . ContainsKey ( ToLocationUrl ( location ) ) ;
72+ Uri key = ToLocationUrl ( location ) ;
73+ return _documents . ContainsKey ( key ) || _fragments . ContainsKey ( key ) || _artifacts . ContainsKey ( key ) ;
5174 }
5275
76+ /// <summary>
77+ /// Add an OpenApiDocument to the workspace.
78+ /// </summary>
79+ /// <param name="location"></param>
80+ /// <param name="document"></param>
5381 public void AddDocument ( string location , OpenApiDocument document )
5482 {
5583 document . Workspace = this ;
5684 _documents . Add ( ToLocationUrl ( location ) , document ) ;
5785 }
5886
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 )
6096 {
6197 _fragments . Add ( ToLocationUrl ( location ) , fragment ) ;
6298 }
6399
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>
64105 public void AddArtifact ( string location , Stream artifact )
65106 {
66107 _artifacts . Add ( ToLocationUrl ( location ) , artifact ) ;
67108 }
68109
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>
69115 public IOpenApiReferenceable ResolveReference ( OpenApiReference reference )
70116 {
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 ) )
72118 {
73- return null ;
119+ return doc . ResolveReference ( reference , true ) ;
74120 }
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 ) ] ;
77144 }
78145
79146 private Uri ToLocationUrl ( string location )
0 commit comments