Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
string filterbyoperationids = context.ParseResult.GetValueForOption(FilterByOperationIdsOption);
string filterbytags = context.ParseResult.GetValueForOption(FilterByTagsOption);
string filterbycollection = context.ParseResult.GetValueForOption(FilterByCollectionOption);

CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));

using var loggerFactory = Logger.ConfigureLogger(logLevel);
Expand Down
15 changes: 9 additions & 6 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ CancellationToken cancellationToken
stream.Position = 0;
}

document = await ConvertCsdlToOpenApi(stream, settingsFile);
document = await ConvertCsdlToOpenApi(stream, settingsFile, cancellationToken);
stopwatch.Stop();
logger.LogTrace("{timestamp}ms: Generated OpenAPI with {paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
}
Expand Down Expand Up @@ -216,6 +216,10 @@ CancellationToken cancellationToken
textWriter.Flush();
}
}
catch(TaskCanceledException)
{
Console.Error.WriteLine("CTRL+C pressed, aborting the operation.");
}
catch (Exception ex)
{
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
Expand Down Expand Up @@ -324,12 +328,12 @@ internal static IConfiguration GetConfiguration(string settingsFile)
/// </summary>
/// <param name="csdl">The CSDL stream.</param>
/// <returns>An OpenAPI document.</returns>
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string settingsFile = null)
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string settingsFile = null, CancellationToken token = default)
{
using var reader = new StreamReader(csdl);
var csdlText = await reader.ReadToEndAsync();
var csdlText = await reader.ReadToEndAsync(token);
var edmModel = CsdlReader.Parse(XElement.Parse(csdlText).CreateReader());

var config = GetConfiguration(settingsFile);
var settings = new OpenApiConvertSettings()
{
Expand All @@ -353,9 +357,8 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, stri
EnableTypeDisambiguationForDefaultValueOfOdataTypeProperty = true
};
config.GetSection("OpenApiConvertSettings").Bind(settings);

OpenApiDocument document = edmModel.ConvertToOpenApi(settings);

OpenApiDocument document = edmModel.ConvertToOpenApi(settings);
document = FixReferences(document);

return document;
Expand Down
17 changes: 7 additions & 10 deletions src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Parsing;

using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Handlers;

Expand All @@ -18,9 +16,8 @@ namespace Microsoft.OpenApi.Hidi
static class Program
{
static async Task Main(string[] args)
{
var rootCommand = new RootCommand() {
};
{
var rootCommand = new RootCommand() {};

// command option parameters and aliases
var descriptionOption = new Option<string>("--openapi", "Input OpenAPI description file path or URL");
Expand Down Expand Up @@ -120,12 +117,12 @@ static async Task Main(string[] args)

rootCommand.Add(transformCommand);
rootCommand.Add(validateCommand);

// Parse the incoming args and invoke the handler
await rootCommand.InvokeAsync(args);

//// Wait for logger to write messages to the console before exiting
await Task.Delay(10);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task ReturnFilteredOpenApiDocBasedOnOperationIdsAndInputCsdlDocumen
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles\\Todo.xml");
var fileInput = new FileInfo(filePath);
var csdlStream = fileInput.OpenRead();

// Act
var openApiDoc = await OpenApiService.ConvertCsdlToOpenApi(csdlStream);
var predicate = OpenApiFilterService.CreatePredicate(operationIds, tags);
Expand Down