Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -51,6 +51,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
</ItemGroup>

<ItemGroup>
Expand Down
41 changes: 30 additions & 11 deletions src/OoasGui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 (*.*)|*.*";
Expand All @@ -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)
{
Expand All @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -181,7 +192,7 @@ private void LoadEdm(string resource, string text)
EdmModel = model;
}

private async Task Convert()
private async Task ConvertAsync()
{
saveBtn.Enabled = false;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
}
}
6 changes: 4 additions & 2 deletions src/OoasUtil/FileOpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -42,11 +44,11 @@ public FileOpenApiGenerator(string input, string output, OpenApiFormat format, O
/// <summary>
/// Process the arguments.
/// </summary>
protected override IEdmModel GetEdmModel()
protected override async Task<IEdmModel> 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())
Expand Down
12 changes: 7 additions & 5 deletions src/OoasUtil/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -40,7 +42,7 @@ internal abstract class OpenApiGenerator
/// <param name="output">The output.</param>
/// <param name="format">The output format.</param>
/// <param name="settings">Conversion settings.</param>
public OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSettings settings)
protected OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSettings settings)
{
Output = output;
Format = format;
Expand All @@ -50,19 +52,19 @@ public OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSetti
/// <summary>
/// Generate the Open Api.
/// </summary>
public bool Generate()
public async Task<bool> GenerateAsync(CancellationToken cancellationToken = default)
{
try
{
IEdmModel edmModel = GetEdmModel();
IEdmModel edmModel = await GetEdmModelAsync(cancellationToken);

this.ModifySettings();

using (FileStream fs = File.Create(Output))
{
OpenApiDocument document = edmModel.ConvertToOpenApi(Settings);
document.Serialize(fs, Settings.OpenApiSpecVersion, Format);
fs.Flush();
await fs.FlushAsync();
}
}
catch(Exception e)
Expand All @@ -78,6 +80,6 @@ protected virtual void ModifySettings()
{
}

protected abstract IEdmModel GetEdmModel();
protected abstract Task<IEdmModel> GetEdmModelAsync(CancellationToken cancellationToken = default);
}
}
4 changes: 2 additions & 2 deletions src/OoasUtil/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OoasUtil
{
class Program
{
static int Main(string[] args)
static async System.Threading.Tasks.Task<int> 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" };
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/OoasUtil/UrlOpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.OpenApi.OData;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;

namespace OoasUtil
{
Expand Down Expand Up @@ -41,16 +42,16 @@ public UrlOpenApiGenerator(Uri input, string output, OpenApiFormat format, OpenA
/// <summary>
/// Get the Edm model.
/// </summary>
protected override IEdmModel GetEdmModel()
protected override async Task<IEdmModel> 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<string> GetModelDocumentAsync(Uri requestUri) {
HttpResponseMessage response = await client.GetAsync(requestUri);
private async Task<string> GetModelDocumentAsync(Uri requestUri, CancellationToken cancellationToken) {
HttpResponseMessage response = await client.GetAsync(requestUri, cancellationToken);
return await response.Content.ReadAsStringAsync();
}
private static readonly HttpClient client = new ();
Expand Down