@@ -63,7 +63,7 @@ public OpenApiWorkspace(Uri baseUrl)
63
63
/// </summary>
64
64
public OpenApiWorkspace ( )
65
65
{
66
- BaseUrl = new ( "http ://openapi.net/workspace/ " ) ;
66
+ BaseUrl = new ( "file ://" + Environment . CurrentDirectory + $ " { Path . DirectorySeparatorChar } ") ;
67
67
}
68
68
69
69
/// <summary>
@@ -75,50 +75,7 @@ public OpenApiWorkspace(OpenApiWorkspace workspace) { }
75
75
///
76
76
/// </summary>
77
77
public IDictionary < Uri , OpenApiComponents > ComponentsRegistry { get ; } = new Dictionary < Uri , OpenApiComponents > ( ) ;
78
-
79
- /// <summary>
80
- ///
81
- /// </summary>
82
- /// <param name="uri"></param>
83
- /// <param name="components"></param>
84
- /// <exception cref="ArgumentNullException"></exception>
85
- public void RegisterComponents ( Uri uri , OpenApiComponents components )
86
- {
87
- if ( uri == null ) throw new ArgumentNullException ( nameof ( uri ) ) ;
88
- if ( components == null ) throw new ArgumentNullException ( nameof ( components ) ) ;
89
- ComponentsRegistry [ uri ] = components ;
90
- }
91
-
92
- /// <summary>
93
- ///
94
- /// </summary>
95
- /// <param name="document"></param>
96
- /// <exception cref="ArgumentNullException"></exception>
97
- public void RegisterComponents ( OpenApiDocument document )
98
- {
99
- if ( document == null ) throw new ArgumentNullException ( nameof ( document ) ) ;
100
- if ( document . Components == null ) throw new ArgumentNullException ( nameof ( document . Components ) ) ;
101
- ComponentsRegistry [ GetDocumentUri ( document ) ] = document . Components ;
102
- }
103
-
104
- /// <summary>
105
- ///
106
- /// </summary>
107
- /// <param name="uri"></param>
108
- /// <param name="components"></param>
109
- /// <returns></returns>
110
- public bool TryGetComponents ( Uri uri , out OpenApiComponents components )
111
- {
112
- if ( uri == null )
113
- {
114
- components = null ;
115
- return false ;
116
- }
117
-
118
- ComponentsRegistry . TryGetValue ( uri , out components ) ;
119
- return ( components != null ) ;
120
- }
121
-
78
+
122
79
/// <summary>
123
80
/// Verify if workspace contains a document based on its URL.
124
81
/// </summary>
@@ -127,7 +84,7 @@ public bool TryGetComponents(Uri uri, out OpenApiComponents components)
127
84
public bool Contains ( string location )
128
85
{
129
86
var key = ToLocationUrl ( location ) ;
130
- return _documents . ContainsKey ( key ) || _fragments . ContainsKey ( key ) || _artifacts . ContainsKey ( key ) ;
87
+ return _documents . ContainsKey ( key ) || _fragments . ContainsKey ( key ) || _artifacts . ContainsKey ( key ) || _schemaFragments . ContainsKey ( key ) ;
131
88
}
132
89
133
90
/// <summary>
@@ -139,44 +96,11 @@ public void AddDocument(string location, OpenApiDocument document)
139
96
{
140
97
document . Workspace = this ;
141
98
var locationUrl = ToLocationUrl ( location ) ;
142
- _documents . Add ( locationUrl , document ) ;
143
- if ( document . Components != null )
144
- {
145
- RegisterComponents ( locationUrl , document . Components ) ;
146
- }
147
- }
148
-
149
- /// <summary>
150
- /// Add an OpenApiDocument to the workspace.
151
- /// </summary>
152
- /// <param name="document">The OpenAPI document.</param>
153
- public void AddDocument ( OpenApiDocument document )
154
- {
155
- // document.Workspace = this; TODO
156
-
157
- // Register components in this doc.
158
- if ( document . Components != null )
159
- {
160
- RegisterComponents ( GetDocumentUri ( document ) , document . Components ) ;
161
- }
162
- }
163
99
164
- /// <summary>
165
- ///
166
- /// </summary>
167
- /// <param name="document"></param>
168
- /// <returns></returns>
169
- private Uri GetDocumentUri ( OpenApiDocument document )
170
- {
171
- if ( document == null ) return null ;
172
-
173
- string docUri = ( document . Servers . FirstOrDefault ( ) != null ) ? document . Servers . First ( ) . Url : document . BaseUri . OriginalString ;
174
- if ( ! Uri . TryCreate ( docUri , UriKind . Absolute , out _ ) )
100
+ if ( ! _documents . ContainsKey ( locationUrl ) )
175
101
{
176
- docUri = $ "http://openapi.net/ { docUri } " ;
102
+ _documents . Add ( locationUrl , document ) ;
177
103
}
178
-
179
- return new Uri ( docUri ) ;
180
104
}
181
105
182
106
/// <summary>
@@ -200,10 +124,10 @@ public void AddFragment(string location, IOpenApiReferenceable fragment)
200
124
public void AddSchemaFragment ( string location , JsonSchema fragment )
201
125
{
202
126
var locationUri = ToLocationUrl ( location ) ;
203
- _schemaFragments . Add ( locationUri , fragment ) ;
204
- var schemaComponent = new OpenApiComponents ( ) ;
205
- schemaComponent . Schemas . Add ( locationUri . OriginalString , fragment ) ;
206
- ComponentsRegistry [ locationUri ] = schemaComponent ;
127
+ if ( ! _schemaFragments . ContainsKey ( locationUri ) )
128
+ {
129
+ _schemaFragments . Add ( locationUri , fragment ) ;
130
+ }
207
131
}
208
132
209
133
/// <summary>
@@ -217,151 +141,31 @@ public void AddArtifact(string location, Stream artifact)
217
141
}
218
142
219
143
/// <summary>
220
- /// Returns the target of an OpenApiReference from within the workspace.
144
+ /// Returns the target of a referenceable item from within the workspace.
221
145
/// </summary>
222
- /// <param name="reference">An instance of an OpenApiReference</param>
146
+ /// <typeparam name="T"></typeparam>
147
+ /// <param name="reference"></param>
223
148
/// <returns></returns>
224
- public IOpenApiReferenceable ResolveReference ( OpenApiReference reference )
149
+ public T ResolveReference < T > ( OpenApiReference reference )
225
150
{
226
151
var uri = new Uri ( BaseUrl , reference . ExternalResource ) ;
227
152
if ( _documents . TryGetValue ( uri , out var doc ) )
228
153
{
229
- // return doc.ResolveReference(reference, false); // TODO: Resolve internally, don't refer to doc.
230
- return ResolveReference < IOpenApiReferenceable > ( reference . Id , reference . Type , doc . Components ) ;
154
+ return ResolveReference < T > ( reference . Id , reference . Type , doc . Components ) ;
231
155
}
232
156
else if ( _fragments . TryGetValue ( uri , out var fragment ) )
233
157
{
234
158
var jsonPointer = new JsonPointer ( $ "/{ reference . Id ?? string . Empty } ") ;
235
- return fragment . ResolveReference ( jsonPointer ) ;
236
- }
237
- return null ;
238
-
239
- }
240
-
241
-
242
- //public JsonSchema ResolveJsonSchemaReference(Uri reference)
243
- // {
244
- // TryResolveReference<JsonSchema>(reference.OriginalString, ReferenceType.Schema, document.BaseUri, out var resolvedSchema);
245
-
246
- // if (resolvedSchema != null)
247
- // {
248
- // var resolvedSchemaBuilder = new JsonSchemaBuilder();
249
- // var description = resolvedSchema.GetDescription();
250
- // var summary = resolvedSchema.GetSummary();
251
-
252
- // foreach (var keyword in resolvedSchema.Keywords)
253
- // {
254
- // resolvedSchemaBuilder.Add(keyword);
255
-
256
- // // Replace the resolved schema's description with that of the schema reference
257
- // if (!string.IsNullOrEmpty(description))
258
- // {
259
- // resolvedSchemaBuilder.Description(description);
260
- // }
261
-
262
- // // Replace the resolved schema's summary with that of the schema reference
263
- // if (!string.IsNullOrEmpty(summary))
264
- // {
265
- // resolvedSchemaBuilder.Summary(summary);
266
- // }
267
- // }
268
-
269
- // return resolvedSchemaBuilder.Build();
270
- // }
271
- // else
272
- // {
273
- // var referenceId = reference.OriginalString.Split('/').LastOrDefault();
274
- // throw new OpenApiException(string.Format(Properties.SRResource.InvalidReferenceId, referenceId));
275
- // }
276
- //}
277
-
278
- /// <summary>
279
- ///
280
- /// </summary>
281
- /// <typeparam name="T"></typeparam>
282
- /// <param name="referenceV3"></param>
283
- /// <param name="referenceType"></param>
284
- /// <param name="docBaseUri"></param>
285
- /// <param name="value"></param>
286
- /// <returns></returns>
287
- /// <exception cref="OpenApiException"></exception>
288
- public bool TryResolveReference < T > ( string referenceV3 , ReferenceType ? referenceType , out T value , Uri docBaseUri = null )
289
- {
290
- value = default ;
291
- if ( string . IsNullOrEmpty ( referenceV3 ) ) return false ;
292
-
293
- var referenceId = referenceV3 . Split ( '/' ) . LastOrDefault ( ) ;
294
-
295
- // The first part of the referenceId before the # should give us our location url
296
- // if the 1st part is missing, then the reference is in the entry document
297
- var locationUrl = ( referenceV3 . Contains ( '#' ) ) ? referenceV3 . Substring ( 0 , referenceV3 . IndexOf ( '#' ) ) : null ;
298
-
299
- ComponentsRegistry . TryGetValue ( docBaseUri , out var componentsTest ) ;
300
-
301
- OpenApiComponents components ;
302
- if ( string . IsNullOrEmpty ( locationUrl ) )
303
- {
304
- // Get the entry level document components
305
- // or the 1st registry component (if entry level has no components)
306
- components = ComponentsRegistry . FirstOrDefault ( ) . Value ;
159
+ return ( T ) fragment . ResolveReference ( jsonPointer ) ;
307
160
}
308
- else
161
+ else if ( _schemaFragments . TryGetValue ( uri , out var schemaFragment ) )
309
162
{
310
- // Try convert to absolute uri
311
- Uri uriLocation = ToLocationUrl ( locationUrl ) ;
312
-
313
- ComponentsRegistry . TryGetValue ( uriLocation , out components ) ;
163
+ return ( T ) ( schemaFragment as IBaseDocument ) ;
314
164
}
165
+ return default ;
315
166
316
- if ( components == null ) return false ;
317
-
318
- switch ( referenceType )
319
- {
320
- case ReferenceType . PathItem :
321
- value = ( T ) ( IOpenApiReferenceable ) components . PathItems [ referenceId ] ;
322
- return ( value != null ) ;
323
-
324
- case ReferenceType . Response :
325
- value = ( T ) ( IOpenApiReferenceable ) components . Responses [ referenceId ] ;
326
- return ( value != null ) ;
327
-
328
- case ReferenceType . Parameter :
329
- value = ( T ) ( IOpenApiReferenceable ) components . Parameters [ referenceId ] ;
330
- return ( value != null ) ;
331
-
332
- case ReferenceType . Example :
333
- value = ( T ) ( IOpenApiReferenceable ) components . Examples [ referenceId ] ;
334
- return ( value != null ) ;
335
-
336
- case ReferenceType . RequestBody :
337
- value = ( T ) ( IOpenApiReferenceable ) components . RequestBodies [ referenceId ] ;
338
- return ( value != null ) ;
339
-
340
- case ReferenceType . Header :
341
- value = ( T ) ( IOpenApiReferenceable ) components . Headers [ referenceId ] ;
342
- return ( value != null ) ;
343
-
344
- case ReferenceType . SecurityScheme :
345
- value = ( T ) ( IOpenApiReferenceable ) components . SecuritySchemes [ referenceId ] ;
346
- return ( value != null ) ;
347
-
348
- case ReferenceType . Link :
349
- value = ( T ) ( IOpenApiReferenceable ) components . Links [ referenceId ] ;
350
- return ( value != null ) ;
351
-
352
- case ReferenceType . Callback :
353
- value = ( T ) ( IOpenApiReferenceable ) components . Callbacks [ referenceId ] ;
354
- return ( value != null ) ;
355
-
356
- case ReferenceType . Schema :
357
- value = ( T ) ( IBaseDocument ) components . Schemas [ referenceId ] ;
358
- return ( value != null ) ;
359
-
360
- default :
361
- throw new OpenApiException ( Properties . SRResource . InvalidReferenceType ) ;
362
- }
363
167
}
364
-
168
+
365
169
/// <summary>
366
170
///
367
171
/// </summary>
@@ -392,7 +196,6 @@ public T ResolveReference<T>(string referenceId, ReferenceType? referenceType, O
392
196
} ;
393
197
}
394
198
395
-
396
199
/// <summary>
397
200
///
398
201
/// </summary>
@@ -405,20 +208,7 @@ public Stream GetArtifact(string location)
405
208
406
209
private Uri ToLocationUrl ( string location )
407
210
{
408
- // Try convert to absolute uri
409
- return ( Uri . TryCreate ( location , UriKind . Absolute , out var uri ) ) == true ? uri : new Uri ( BaseUrl , location ) ;
410
-
411
- //if (Uri.TryCreate(location, UriKind.Absolute, out var uri))
412
- // {
413
- // locationUri = new Uri(BaseUrl, uri.LocalPath);
414
- // }
415
- //else
416
- //{
417
- // locationUri = new Uri(BaseUrl, location);
418
- //}
419
- //return locationUri;
420
-
421
- // return new(BaseUrl, location);
211
+ return new ( BaseUrl , location ) ;
422
212
}
423
213
424
214
private static JsonSchema FetchSchemaFromRegistry ( Uri reference )
0 commit comments