19
19
using System . Xml . Xsl ;
20
20
using Microsoft . Extensions . Configuration ;
21
21
using Microsoft . Extensions . Logging ;
22
+ using Microsoft . Extensions . Options ;
22
23
using Microsoft . OData . Edm . Csdl ;
23
24
using Microsoft . OpenApi . ApiManifest ;
24
25
using Microsoft . OpenApi . ApiManifest . OpenAI ;
@@ -86,7 +87,8 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
86
87
}
87
88
88
89
// Load OpenAPI document
89
- var document = await GetOpenApi ( options , logger , options . MetadataVersion , cancellationToken ) . ConfigureAwait ( false ) ;
90
+ var format = OpenApiModelFactory . GetFormat ( options . OpenApi ) ;
91
+ var document = await GetOpenApi ( options , format , logger , options . MetadataVersion , cancellationToken ) . ConfigureAwait ( false ) ;
90
92
91
93
if ( options . FilterOptions != null )
92
94
{
@@ -213,7 +215,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
213
215
}
214
216
215
217
// Get OpenAPI document either from OpenAPI or CSDL
216
- private static async Task < OpenApiDocument > GetOpenApi ( HidiOptions options , ILogger logger , string ? metadataVersion = null , CancellationToken cancellationToken = default )
218
+ private static async Task < OpenApiDocument > GetOpenApi ( HidiOptions options , string format , ILogger logger , string ? metadataVersion = null , CancellationToken cancellationToken = default )
217
219
{
218
220
OpenApiDocument document ;
219
221
Stream stream ;
@@ -234,7 +236,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogg
234
236
await stream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
235
237
}
236
238
237
- document = await ConvertCsdlToOpenApi ( filteredStream ?? stream , metadataVersion , options . SettingsConfig , cancellationToken ) . ConfigureAwait ( false ) ;
239
+ document = await ConvertCsdlToOpenApi ( filteredStream ?? stream , format , metadataVersion , options . SettingsConfig , cancellationToken ) . ConfigureAwait ( false ) ;
238
240
stopwatch . Stop ( ) ;
239
241
logger . LogTrace ( "{Timestamp}ms: Generated OpenAPI with {Paths} paths." , stopwatch . ElapsedMilliseconds , document . Paths . Count ) ;
240
242
}
@@ -369,14 +371,16 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
369
371
{
370
372
stopwatch . Start ( ) ;
371
373
372
- result = await new OpenApiStreamReader ( new ( )
373
- {
374
+ var settings = new OpenApiReaderSettings
375
+ {
374
376
LoadExternalRefs = inlineExternal ,
375
377
BaseUrl = openApiFile . StartsWith ( "http" , StringComparison . OrdinalIgnoreCase ) ?
376
378
new ( openApiFile ) :
377
379
new Uri ( "file://" + new FileInfo ( openApiFile ) . DirectoryName + Path . DirectorySeparatorChar )
378
- }
379
- ) . ReadAsync ( stream , cancellationToken ) . ConfigureAwait ( false ) ;
380
+ } ;
381
+
382
+ var format = OpenApiModelFactory . GetFormat ( openApiFile ) ;
383
+ result = await OpenApiDocument . LoadAsync ( stream , format , settings , cancellationToken ) . ConfigureAwait ( false ) ;
380
384
381
385
logger . LogTrace ( "{Timestamp}ms: Completed parsing." , stopwatch . ElapsedMilliseconds ) ;
382
386
@@ -392,15 +396,15 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
392
396
/// </summary>
393
397
/// <param name="csdl">The CSDL stream.</param>
394
398
/// <returns>An OpenAPI document.</returns>
395
- public static async Task < OpenApiDocument > ConvertCsdlToOpenApi ( Stream csdl , string ? metadataVersion = null , IConfiguration ? settings = null , CancellationToken token = default )
399
+ public static async Task < OpenApiDocument > ConvertCsdlToOpenApi ( Stream csdl , string format , string ? metadataVersion = null , IConfiguration ? settings = null , CancellationToken token = default )
396
400
{
397
401
using var reader = new StreamReader ( csdl ) ;
398
402
var csdlText = await reader . ReadToEndAsync ( token ) . ConfigureAwait ( false ) ;
399
403
var edmModel = CsdlReader . Parse ( XElement . Parse ( csdlText ) . CreateReader ( ) ) ;
400
404
settings ??= SettingsUtilities . GetConfiguration ( ) ;
401
405
402
406
var document = edmModel . ConvertToOpenApi ( SettingsUtilities . GetOpenApiConvertSettings ( settings , metadataVersion ) ) ;
403
- document = FixReferences ( document ) ;
407
+ document = FixReferences ( document , format ) ;
404
408
405
409
return document ;
406
410
}
@@ -410,14 +414,15 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, stri
410
414
/// </summary>
411
415
/// <param name="document"> The converted OpenApiDocument.</param>
412
416
/// <returns> A valid OpenApiDocument instance.</returns>
413
- public static OpenApiDocument FixReferences ( OpenApiDocument document )
417
+ public static OpenApiDocument FixReferences ( OpenApiDocument document , string format )
414
418
{
415
419
// This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance.
416
420
// So we write it out, and read it back in again to fix it up.
417
421
418
422
var sb = new StringBuilder ( ) ;
419
423
document . SerializeAsV3 ( new OpenApiYamlWriter ( new StringWriter ( sb ) ) ) ;
420
- var doc = new OpenApiStringReader ( ) . Read ( sb . ToString ( ) , out _ ) ;
424
+
425
+ var doc = OpenApiDocument . Parse ( sb . ToString ( ) , format ) . OpenApiDocument ;
421
426
422
427
return doc ;
423
428
}
@@ -565,7 +570,8 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
565
570
throw new ArgumentException ( "Please input a file path or URL" ) ;
566
571
}
567
572
568
- var document = await GetOpenApi ( options , logger , null , cancellationToken ) . ConfigureAwait ( false ) ;
573
+ var format = OpenApiModelFactory . GetFormat ( options . OpenApi ) ;
574
+ var document = await GetOpenApi ( options , format , logger , null , cancellationToken ) . ConfigureAwait ( false ) ;
569
575
570
576
using ( logger . BeginScope ( "Creating diagram" ) )
571
577
{
@@ -726,7 +732,8 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
726
732
}
727
733
728
734
// Load OpenAPI document
729
- var document = await GetOpenApi ( options , logger , options . MetadataVersion , cancellationToken ) . ConfigureAwait ( false ) ;
735
+ var format = OpenApiModelFactory . GetFormat ( options . OpenApi ) ;
736
+ var document = await GetOpenApi ( options , format , logger , options . MetadataVersion , cancellationToken ) . ConfigureAwait ( false ) ;
730
737
731
738
cancellationToken . ThrowIfCancellationRequested ( ) ;
732
739
0 commit comments