diff --git a/.editorconfig b/.editorconfig index 892f86c4..fc328cb5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,3 +2,4 @@ # IDE0009: Member access should be qualified. dotnet_diagnostic.IDE0009.severity = none +dotnet_diagnostic.CA2016.severity = warning diff --git a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj index 7232473b..e4b3f649 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj +++ b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj @@ -51,6 +51,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/OoasGui/MainForm.cs b/src/OoasGui/MainForm.cs index 6b0c1ac0..e10bbae3 100644 --- a/src/OoasGui/MainForm.cs +++ b/src/OoasGui/MainForm.cs @@ -54,29 +54,36 @@ public MainForm() csdlRichTextBox.WordWrap = false; oasRichTextBox.WordWrap = false; } - +#pragma warning disable VSTHRD100 private async void jsonRadioBtn_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Format = OpenApiFormat.Json; - await Convert(); + await ConvertAsync(); } +#pragma warning disable VSTHRD100 private async void yamlRadioBtn_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Format = OpenApiFormat.Yaml; - await Convert(); + await ConvertAsync(); } +#pragma warning disable VSTHRD100 private async void v2RadioBtn_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Settings.OpenApiSpecVersion = Version = OpenApiSpecVersion.OpenApi2_0; - await Convert(); + await ConvertAsync(); } +#pragma warning disable VSTHRD100 private async void v3RadioBtn_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Settings.OpenApiSpecVersion = Version = OpenApiSpecVersion.OpenApi3_0; - await Convert(); + await ConvertAsync(); } private void fromFileRadioBtn_CheckedChanged(object sender, EventArgs e) @@ -96,7 +103,9 @@ private void fromUrlRadioBtn_CheckedChanged(object sender, EventArgs e) loadBtn.Enabled = true; } +#pragma warning disable VSTHRD100 private async void btnBrowse_Click(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "CSDL files (*.xml)|*.xml|All files (*.*)|*.*"; @@ -111,7 +120,7 @@ private async void btnBrowse_Click(object sender, EventArgs e) fileTextBox.Text = openFileDialog.FileName; csdlRichTextBox.Text = text; Settings.ServiceRoot = new Uri(openFileDialog.FileName); - await Convert(); + await ConvertAsync(); } catch (Exception ex) { @@ -126,7 +135,9 @@ private async void btnBrowse_Click(object sender, EventArgs e) private static HttpClient client = new(); +#pragma warning disable VSTHRD100 private async void loadBtn_Click(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { string url = urlTextBox.Text; @@ -149,7 +160,7 @@ private async void loadBtn_Click(object sender, EventArgs e) LoadEdm(url, csdl); csdlRichTextBox.Text = FormatXml(csdl); Settings.ServiceRoot = requestUri; - await Convert(); + await ConvertAsync(); } catch(Exception ex) { @@ -181,7 +192,7 @@ private void LoadEdm(string resource, string text) EdmModel = model; } - private async Task Convert() + private async Task ConvertAsync() { saveBtn.Enabled = false; @@ -222,7 +233,9 @@ private string FormatXml(string xml) return sw.ToString(); } +#pragma warning disable VSTHRD100 private async void saveBtn_Click(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { SaveFileDialog saveFileDialog = new SaveFileDialog(); if (Format == OpenApiFormat.Json) @@ -252,22 +265,28 @@ await Task.Run(() => } } +#pragma warning disable VSTHRD100 private async void operationIdcheckBox_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Settings.EnableOperationId = !Settings.EnableOperationId; - await Convert (); + await ConvertAsync (); } +#pragma warning disable VSTHRD100 private async void VerifyEdmModelcheckBox_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Settings.VerifyEdmModel = !Settings.VerifyEdmModel; - await Convert(); + await ConvertAsync(); } +#pragma warning disable VSTHRD100 private async void NavPathcheckBox_CheckedChanged(object sender, EventArgs e) +#pragma warning restore VSTHRD100 { Settings.EnableNavigationPropertyPath = !Settings.EnableNavigationPropertyPath; - await Convert(); + await ConvertAsync(); } } } diff --git a/src/OoasUtil/FileOpenApiGenerator.cs b/src/OoasUtil/FileOpenApiGenerator.cs index f820a58e..446ab60c 100644 --- a/src/OoasUtil/FileOpenApiGenerator.cs +++ b/src/OoasUtil/FileOpenApiGenerator.cs @@ -13,6 +13,8 @@ using Microsoft.OData.Edm.Csdl; using Microsoft.OpenApi; using Microsoft.OpenApi.OData; +using System.Threading.Tasks; +using System.Threading; namespace OoasUtil { @@ -42,11 +44,11 @@ public FileOpenApiGenerator(string input, string output, OpenApiFormat format, O /// /// Process the arguments. /// - protected override IEdmModel GetEdmModel() + protected override async Task GetEdmModelAsync(CancellationToken cancellationToken = default) { try { - string csdl = File.ReadAllText(Input); + string csdl = await File.ReadAllTextAsync(Input, cancellationToken); var directory = Path.GetDirectoryName(Input); var parsed = XElement.Parse(csdl); using (XmlReader mainReader = parsed.CreateReader()) diff --git a/src/OoasUtil/OpenApiGenerator.cs b/src/OoasUtil/OpenApiGenerator.cs index fd170255..3a921803 100644 --- a/src/OoasUtil/OpenApiGenerator.cs +++ b/src/OoasUtil/OpenApiGenerator.cs @@ -11,6 +11,8 @@ using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.OData; +using System.Threading.Tasks; +using System.Threading; namespace OoasUtil { @@ -40,7 +42,7 @@ internal abstract class OpenApiGenerator /// The output. /// The output format. /// Conversion settings. - public OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSettings settings) + protected OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSettings settings) { Output = output; Format = format; @@ -50,11 +52,11 @@ public OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSetti /// /// Generate the Open Api. /// - public bool Generate() + public async Task GenerateAsync(CancellationToken cancellationToken = default) { try { - IEdmModel edmModel = GetEdmModel(); + IEdmModel edmModel = await GetEdmModelAsync(cancellationToken); this.ModifySettings(); @@ -62,7 +64,7 @@ public bool Generate() { OpenApiDocument document = edmModel.ConvertToOpenApi(Settings); document.Serialize(fs, Settings.OpenApiSpecVersion, Format); - fs.Flush(); + await fs.FlushAsync(cancellationToken); } } catch(Exception e) @@ -78,6 +80,6 @@ protected virtual void ModifySettings() { } - protected abstract IEdmModel GetEdmModel(); + protected abstract Task GetEdmModelAsync(CancellationToken cancellationToken = default); } } diff --git a/src/OoasUtil/Program.cs b/src/OoasUtil/Program.cs index b5ba3089..59f19488 100644 --- a/src/OoasUtil/Program.cs +++ b/src/OoasUtil/Program.cs @@ -12,7 +12,7 @@ namespace OoasUtil { class Program { - static int Main(string[] args) + static async System.Threading.Tasks.Task Main(string[] args) { // args = new[] { "--json", "--input", @"E:\work\OneApiDesign\test\TripService.OData.xml", "-o", @"E:\work\OneApiDesign\test1\Trip.json" }; // args = new[] { "--yaml", "-i", @"E:\work\OneApiDesign\test\TripService.OData.xml", "-o", @"E:\work\OneApiDesign\test1\Trip.yaml" }; @@ -53,7 +53,7 @@ static int Main(string[] args) generator = new UrlOpenApiGenerator(new Uri(processer.Input), processer.Output, processer.Format.Value, settings); } - if (generator.Generate()) + if (await generator.GenerateAsync()) { Console.WriteLine("Succeeded!"); return 1; diff --git a/src/OoasUtil/UrlOpenApiGenerator.cs b/src/OoasUtil/UrlOpenApiGenerator.cs index 91e7e925..7325da48 100644 --- a/src/OoasUtil/UrlOpenApiGenerator.cs +++ b/src/OoasUtil/UrlOpenApiGenerator.cs @@ -12,6 +12,7 @@ using Microsoft.OpenApi.OData; using System.Net.Http; using System.Threading.Tasks; +using System.Threading; namespace OoasUtil { @@ -41,17 +42,17 @@ public UrlOpenApiGenerator(Uri input, string output, OpenApiFormat format, OpenA /// /// Get the Edm model. /// - protected override IEdmModel GetEdmModel() + protected override async Task GetEdmModelAsync(CancellationToken cancellationToken = default) { Uri requestUri = new (Input.OriginalString + "/$metadata"); - string csdl = GetModelDocumentAsync(requestUri).GetAwaiter().GetResult(); + string csdl = await GetModelDocumentAsync(requestUri, cancellationToken); return CsdlReader.Parse(XElement.Parse(csdl).CreateReader()); } - private async Task GetModelDocumentAsync(Uri requestUri) { - HttpResponseMessage response = await client.GetAsync(requestUri); - return await response.Content.ReadAsStringAsync(); + private async Task GetModelDocumentAsync(Uri requestUri, CancellationToken cancellationToken) { + using HttpResponseMessage response = await client.GetAsync(requestUri, cancellationToken); + return await response.Content.ReadAsStringAsync(cancellationToken); } private static readonly HttpClient client = new ();