Skip to content

Commit 575a48a

Browse files
committed
Update hidi to use the Load/Parse methods
1 parent 957ca8d commit 575a48a

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Xml.Xsl;
2020
using Microsoft.Extensions.Configuration;
2121
using Microsoft.Extensions.Logging;
22+
using Microsoft.Extensions.Options;
2223
using Microsoft.OData.Edm.Csdl;
2324
using Microsoft.OpenApi.ApiManifest;
2425
using Microsoft.OpenApi.ApiManifest.OpenAI;
@@ -86,7 +87,8 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
8687
}
8788

8889
// 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);
9092

9193
if (options.FilterOptions != null)
9294
{
@@ -213,7 +215,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
213215
}
214216

215217
// 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)
217219
{
218220
OpenApiDocument document;
219221
Stream stream;
@@ -234,7 +236,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogg
234236
await stream.DisposeAsync().ConfigureAwait(false);
235237
}
236238

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);
238240
stopwatch.Stop();
239241
logger.LogTrace("{Timestamp}ms: Generated OpenAPI with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
240242
}
@@ -369,14 +371,16 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
369371
{
370372
stopwatch.Start();
371373

372-
result = await new OpenApiStreamReader(new()
373-
{
374+
var settings = new OpenApiReaderSettings
375+
{
374376
LoadExternalRefs = inlineExternal,
375377
BaseUrl = openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ?
376378
new(openApiFile) :
377379
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);
380384

381385
logger.LogTrace("{Timestamp}ms: Completed parsing.", stopwatch.ElapsedMilliseconds);
382386

@@ -392,15 +396,15 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
392396
/// </summary>
393397
/// <param name="csdl">The CSDL stream.</param>
394398
/// <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)
396400
{
397401
using var reader = new StreamReader(csdl);
398402
var csdlText = await reader.ReadToEndAsync(token).ConfigureAwait(false);
399403
var edmModel = CsdlReader.Parse(XElement.Parse(csdlText).CreateReader());
400404
settings ??= SettingsUtilities.GetConfiguration();
401405

402406
var document = edmModel.ConvertToOpenApi(SettingsUtilities.GetOpenApiConvertSettings(settings, metadataVersion));
403-
document = FixReferences(document);
407+
document = FixReferences(document, format);
404408

405409
return document;
406410
}
@@ -410,14 +414,15 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, stri
410414
/// </summary>
411415
/// <param name="document"> The converted OpenApiDocument.</param>
412416
/// <returns> A valid OpenApiDocument instance.</returns>
413-
public static OpenApiDocument FixReferences(OpenApiDocument document)
417+
public static OpenApiDocument FixReferences(OpenApiDocument document, string format)
414418
{
415419
// This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance.
416420
// So we write it out, and read it back in again to fix it up.
417421

418422
var sb = new StringBuilder();
419423
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;
421426

422427
return doc;
423428
}
@@ -565,7 +570,8 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
565570
throw new ArgumentException("Please input a file path or URL");
566571
}
567572

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);
569575

570576
using (logger.BeginScope("Creating diagram"))
571577
{
@@ -726,7 +732,8 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
726732
}
727733

728734
// 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);
730737

731738
cancellationToken.ThrowIfCancellationRequested();
732739

0 commit comments

Comments
 (0)