Skip to content

Commit 6c1e502

Browse files
committed
Pass cancellation token to the conversion method and degrade gracefully when an operation is terminated
1 parent 7867fdd commit 6c1e502

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ CancellationToken cancellationToken
102102
stream.Position = 0;
103103
}
104104

105-
document = await ConvertCsdlToOpenApi(stream, settingsFile);
105+
document = await ConvertCsdlToOpenApi(stream, cancellationToken, settingsFile);
106106
stopwatch.Stop();
107107
logger.LogTrace("{timestamp}ms: Generated OpenAPI with {paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
108108
}
@@ -216,6 +216,10 @@ CancellationToken cancellationToken
216216
textWriter.Flush();
217217
}
218218
}
219+
catch(TaskCanceledException)
220+
{
221+
Console.Error.WriteLine("CTRL+C pressed, aborting the operation.");
222+
}
219223
catch (Exception ex)
220224
{
221225
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
@@ -324,12 +328,12 @@ internal static IConfiguration GetConfiguration(string settingsFile)
324328
/// </summary>
325329
/// <param name="csdl">The CSDL stream.</param>
326330
/// <returns>An OpenAPI document.</returns>
327-
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string settingsFile = null)
331+
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, CancellationToken token, string settingsFile = null)
328332
{
329333
using var reader = new StreamReader(csdl);
330-
var csdlText = await reader.ReadToEndAsync();
334+
var csdlText = await reader.ReadToEndAsync(token);
331335
var edmModel = CsdlReader.Parse(XElement.Parse(csdlText).CreateReader());
332-
336+
333337
var config = GetConfiguration(settingsFile);
334338
var settings = new OpenApiConvertSettings()
335339
{
@@ -353,9 +357,8 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, stri
353357
EnableTypeDisambiguationForDefaultValueOfOdataTypeProperty = true
354358
};
355359
config.GetSection("OpenApiConvertSettings").Bind(settings);
356-
357-
OpenApiDocument document = edmModel.ConvertToOpenApi(settings);
358360

361+
OpenApiDocument document = edmModel.ConvertToOpenApi(settings);
359362
document = FixReferences(document);
360363

361364
return document;

0 commit comments

Comments
 (0)