Skip to content

Commit 7af8eb6

Browse files
committed
chore: updates for async API surface changes
1 parent 79ffbbf commit 7af8eb6

27 files changed

+230
-254
lines changed

src/OoasGui/MainForm.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,12 @@ private async Task ConvertAsync()
202202
}
203203

204204
string openApi = null;
205-
await Task.Run(() =>
206-
{
207-
_document = EdmModel.ConvertToOpenApi(Settings);
208-
MemoryStream stream = new MemoryStream();
209-
_document.Serialize(stream, Version, Format);
210-
stream.Flush();
211-
stream.Position = 0;
212-
openApi = new StreamReader(stream).ReadToEnd();
213-
});
205+
_document = EdmModel.ConvertToOpenApi(Settings);
206+
MemoryStream stream = new MemoryStream();
207+
await _document.SerializeAsync(stream, Version, Format);
208+
await stream.FlushAsync();
209+
stream.Position = 0;
210+
openApi = await new StreamReader(stream).ReadToEndAsync();
214211

215212
oasRichTextBox.Text = openApi;
216213
saveBtn.Enabled = true;
@@ -252,14 +249,9 @@ private async void saveBtn_Click(object sender, EventArgs e)
252249
if (saveFileDialog.ShowDialog() == DialogResult.OK)
253250
{
254251
string output = saveFileDialog.FileName;
255-
using (FileStream fs = File.Create(output))
256-
{
257-
await Task.Run(() =>
258-
{
259-
_document?.Serialize(fs, Version, Format);
260-
fs.Flush();
261-
});
262-
}
252+
using FileStream fs = File.Create(output);
253+
await _document?.SerializeAsync(fs, Version, Format);
254+
await fs.FlushAsync();
263255

264256
MessageBox.Show("Saved successfully!");
265257
}

src/OoasUtil/OpenApiGenerator.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ public async Task<bool> GenerateAsync(CancellationToken cancellationToken = defa
6060

6161
this.ModifySettings();
6262

63-
using (FileStream fs = File.Create(Output))
64-
{
65-
OpenApiDocument document = edmModel.ConvertToOpenApi(Settings);
66-
document.Serialize(fs, Settings.OpenApiSpecVersion, Format);
67-
await fs.FlushAsync(cancellationToken);
68-
}
63+
using FileStream fs = File.Create(Output);
64+
OpenApiDocument document = edmModel.ConvertToOpenApi(Settings);
65+
await document.SerializeAsync(fs, Settings.OpenApiSpecVersion, Format, cancellationToken).ConfigureAwait(false);
66+
await fs.FlushAsync(cancellationToken).ConfigureAwait(false);
6967
}
7068
catch(Exception e)
7169
{

test/Microsoft.OpenAPI.OData.Reader.Tests/EdmModelOpenApiExtensionsTest.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.IO;
88
using System.Text.Json.Nodes;
9+
using System.Threading.Tasks;
910
using Microsoft.OData.Edm;
1011
using Microsoft.OpenApi.Extensions;
1112
using Xunit;
@@ -33,7 +34,7 @@ public void ConvertToOpenApiThrowsArgumentNullModel()
3334
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml)]
3435
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml)]
3536
[InlineData(OpenApiSpecVersion.OpenApi3_1, OpenApiFormat.Yaml)]
36-
public void EmptyEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
37+
public async Task EmptyEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
3738
{
3839
// Arrange
3940
IEdmModel model = EdmModelHelper.EmptyModel;
@@ -44,7 +45,7 @@ public void EmptyEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiF
4445
};
4546

4647
// Act
47-
string result = WriteEdmModelAsOpenApi(model, format, openApiConvertSettings);
48+
string result = await WriteEdmModelAsOpenApi(model, format, openApiConvertSettings);
4849
var fileName = $"Empty.OpenApi.{GetVersion(specVersion)}{GetFormatExt(format)}";
4950

5051
// Assert
@@ -58,7 +59,7 @@ public void EmptyEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiF
5859
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml)]
5960
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml)]
6061
[InlineData(OpenApiSpecVersion.OpenApi3_1, OpenApiFormat.Yaml)]
61-
public void BasicEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
62+
public async Task BasicEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
6263
{
6364
// Arrange
6465
IEdmModel model = EdmModelHelper.BasicEdmModel;
@@ -71,7 +72,7 @@ public void BasicEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiF
7172
};
7273

7374
// Act
74-
string result = WriteEdmModelAsOpenApi(model, format, openApiConvertSettings);
75+
string result = await WriteEdmModelAsOpenApi(model, format, openApiConvertSettings);
7576
var fileName = $"Basic.OpenApi.{GetVersion(specVersion)}{GetFormatExt(format)}";
7677

7778
// Assert
@@ -85,7 +86,7 @@ public void BasicEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiF
8586
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml)]
8687
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml)]
8788
[InlineData(OpenApiSpecVersion.OpenApi3_1, OpenApiFormat.Yaml)]
88-
public void MultipleSchemasEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
89+
public async Task MultipleSchemasEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
8990
{
9091
// Arrange
9192
IEdmModel model = EdmModelHelper.MultipleSchemasEdmModel;
@@ -99,7 +100,7 @@ public void MultipleSchemasEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion
99100
};
100101

101102
// Act
102-
string result = WriteEdmModelAsOpenApi(model, format, openApiConvertSettings);
103+
string result = await WriteEdmModelAsOpenApi(model, format, openApiConvertSettings);
103104

104105
var fileName = $"Multiple.Schema.OpenApi.{GetVersion(specVersion)}{GetFormatExt(format)}";
105106

@@ -114,7 +115,7 @@ public void MultipleSchemasEdmModelToOpenApiWorks(OpenApiSpecVersion specVersion
114115
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml)]
115116
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml)]
116117
[InlineData(OpenApiSpecVersion.OpenApi3_1, OpenApiFormat.Yaml)]
117-
public void TripServiceMetadataToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
118+
public async Task TripServiceMetadataToOpenApiWorks(OpenApiSpecVersion specVersion, OpenApiFormat format)
118119
{
119120
// Arrange
120121
IEdmModel model = EdmModelHelper.TripServiceModel;
@@ -131,7 +132,7 @@ public void TripServiceMetadataToOpenApiWorks(OpenApiSpecVersion specVersion, Op
131132
IncludeAssemblyInfo = false
132133
};
133134
// Act
134-
string result = WriteEdmModelAsOpenApi(model, format, settings);
135+
string result = await WriteEdmModelAsOpenApi(model, format, settings);
135136

136137
var fileName = $"TripService.OpenApi.{GetVersion(specVersion)}{GetFormatExt(format)}";
137138

@@ -168,17 +169,17 @@ private static string GetVersion(OpenApiSpecVersion version) =>
168169
_ => throw new NotImplementedException()
169170
};
170171

171-
private static string WriteEdmModelAsOpenApi(IEdmModel model, OpenApiFormat target,
172+
private static async Task<string> WriteEdmModelAsOpenApi(IEdmModel model, OpenApiFormat target,
172173
OpenApiConvertSettings settings = null)
173174
{
174175
settings ??= new OpenApiConvertSettings();
175176
var document = model.ConvertToOpenApi(settings);
176177
Assert.NotNull(document); // guard
177178

178179
MemoryStream stream = new();
179-
document.Serialize(stream, settings.OpenApiSpecVersion, target);
180-
stream.Flush();
180+
await document.SerializeAsync(stream, settings.OpenApiSpecVersion, target);
181+
await stream.FlushAsync();
181182
stream.Position = 0;
182-
return new StreamReader(stream).ReadToEnd();
183+
return await new StreamReader(stream).ReadToEndAsync();
183184
}
184185
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiEdmTypeSchemaGeneratorTest.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Linq;
88
using System.Text.Json.Nodes;
9+
using System.Threading.Tasks;
910
using Microsoft.OData.Edm;
1011
using Microsoft.OpenApi.Extensions;
1112
using Microsoft.OpenApi.Models;
@@ -47,7 +48,7 @@ public void CreateEdmTypeSchemaThrowArgumentNullEdmTypeReference()
4748
[Theory]
4849
[InlineData(OpenApiSpecVersion.OpenApi2_0)]
4950
[InlineData(OpenApiSpecVersion.OpenApi3_0)]
50-
public void CreateEdmTypeSchemaReturnSchemaForNullableCollectionComplexType(OpenApiSpecVersion specVersion)
51+
public async Task CreateEdmTypeSchemaReturnSchemaForNullableCollectionComplexType(OpenApiSpecVersion specVersion)
5152
{
5253
// Arrange
5354
IEdmModel model = EdmModelHelper.TripServiceModel;
@@ -62,7 +63,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNullableCollectionComplexType(Open
6263
// Act
6364
var schema = context.CreateEdmTypeSchema(collectionType, new());
6465
Assert.NotNull(schema);
65-
var json = JsonNode.Parse(schema.SerializeAsJson(context.Settings.OpenApiSpecVersion));
66+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(context.Settings.OpenApiSpecVersion));
6667

6768
// & Assert
6869
if (specVersion == OpenApiSpecVersion.OpenApi2_0)
@@ -86,7 +87,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNullableCollectionComplexType(Open
8687
}
8788

8889
[Fact]
89-
public void CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionComplexType()
90+
public async Task CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionComplexType()
9091
{
9192
// Arrange
9293
IEdmModel model = EdmModelHelper.TripServiceModel;
@@ -98,7 +99,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionComplexType()
9899
// Act
99100
var schema = context.CreateEdmTypeSchema(collectionType, new());
100101
Assert.NotNull(schema);
101-
var json = JsonNode.Parse(schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
102+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
102103

103104
// & Assert
104105
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(@"{
@@ -110,7 +111,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionComplexType()
110111
}
111112

112113
[Fact]
113-
public void CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionPrimitiveType()
114+
public async Task CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionPrimitiveType()
114115
{
115116
// Arrange
116117
IEdmModel model = EdmCoreModel.Instance;
@@ -121,7 +122,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionPrimitiveType
121122
// Act
122123
var schema = context.CreateEdmTypeSchema(collectionType, new());
123124
Assert.NotNull(schema);
124-
var json = JsonNode.Parse(schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
125+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
125126

126127
// & Assert
127128
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(@"{
@@ -133,7 +134,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNonNullableCollectionPrimitiveType
133134
}
134135

135136
[Fact]
136-
public void CreateEdmTypeSchemaReturnSchemaForNullableCollectionPrimitiveType()
137+
public async Task CreateEdmTypeSchemaReturnSchemaForNullableCollectionPrimitiveType()
137138
{
138139
// Arrange
139140
IEdmModel model = EdmCoreModel.Instance;
@@ -144,7 +145,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNullableCollectionPrimitiveType()
144145
// Act
145146
var schema = context.CreateEdmTypeSchema(collectionType, new());
146147
Assert.NotNull(schema);
147-
var json = JsonNode.Parse(schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
148+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
148149

149150
// & Assert
150151
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(@"{
@@ -310,7 +311,7 @@ public void CreateEdmTypeSchemaReturnSchemaForEntityType(bool isNullable, OpenAp
310311
[Theory]
311312
[InlineData(true)]
312313
[InlineData(false)]
313-
public void CreateEdmTypeSchemaReturnSchemaForString(bool isNullable)
314+
public async Task CreateEdmTypeSchemaReturnSchemaForString(bool isNullable)
314315
{
315316
// Arrange
316317
IEdmModel model = EdmCoreModel.Instance;
@@ -320,7 +321,7 @@ public void CreateEdmTypeSchemaReturnSchemaForString(bool isNullable)
320321
// Act
321322
var schema = context.CreateEdmTypeSchema(edmTypeReference, new());
322323
Assert.NotNull(schema); // guard
323-
var json = JsonNode.Parse(schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
324+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
324325

325326
// & Assert
326327
if (isNullable)
@@ -341,7 +342,7 @@ public void CreateEdmTypeSchemaReturnSchemaForString(bool isNullable)
341342
[Theory]
342343
[InlineData(true)]
343344
[InlineData(false)]
344-
public void CreateEdmTypeSchemaReturnSchemaForInt32(bool isNullable)
345+
public async Task CreateEdmTypeSchemaReturnSchemaForInt32(bool isNullable)
345346
{
346347
// Arrange
347348
IEdmModel model = EdmCoreModel.Instance;
@@ -351,7 +352,7 @@ public void CreateEdmTypeSchemaReturnSchemaForInt32(bool isNullable)
351352
// Act
352353
var schema = context.CreateEdmTypeSchema(edmTypeReference, new());
353354
Assert.NotNull(schema); // guard
354-
var json = JsonNode.Parse(schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
355+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
355356

356357
// & Assert
357358
if (isNullable)
@@ -464,7 +465,7 @@ public void CreateEdmTypeSchemaReturnSchemaForInt64(bool isNullable, bool IEEE75
464465
[Theory]
465466
[InlineData(true)]
466467
[InlineData(false)]
467-
public void CreateEdmTypeSchemaReturnSchemaForGuid(bool isNullable)
468+
public async Task CreateEdmTypeSchemaReturnSchemaForGuid(bool isNullable)
468469
{
469470
// Arrange
470471
IEdmModel model = EdmCoreModel.Instance;
@@ -474,7 +475,7 @@ public void CreateEdmTypeSchemaReturnSchemaForGuid(bool isNullable)
474475
// Act
475476
var schema = context.CreateEdmTypeSchema(edmTypeReference, new());
476477
Assert.NotNull(schema); // guard
477-
var json = JsonNode.Parse(schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
478+
var json = JsonNode.Parse(await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
478479

479480
// & Assert
480481
if (isNullable)

0 commit comments

Comments
 (0)