Skip to content

Commit 3c64451

Browse files
committed
Fix merge conflicts
1 parent 19463dd commit 3c64451

File tree

32 files changed

+917
-283
lines changed

32 files changed

+917
-283
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog
9898

9999
// Load OpenAPI document
100100
var format = OpenApiModelFactory.GetFormat(options.OpenApi);
101-
var document = await GetOpenApi(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
101+
var document = await GetOpenApiAsync(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
102102

103103
if (options.FilterOptions != null)
104104
{
@@ -225,7 +225,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
225225
}
226226

227227
// Get OpenAPI document either from OpenAPI or CSDL
228-
private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, string format, ILogger logger, string? metadataVersion = null, CancellationToken cancellationToken = default)
228+
private static async Task<OpenApiDocument> GetOpenApiAsync(HidiOptions options, string format, ILogger logger, string? metadataVersion = null, CancellationToken cancellationToken = default)
229229
{
230230
OpenApiDocument document;
231231
Stream stream;
@@ -246,7 +246,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, strin
246246
await stream.DisposeAsync().ConfigureAwait(false);
247247
}
248248

249-
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, format, metadataVersion, options.SettingsConfig, cancellationToken).ConfigureAwait(false);
249+
document = await ConvertCsdlToOpenApiAsync(filteredStream ?? stream, format, metadataVersion, options.SettingsConfig, cancellationToken).ConfigureAwait(false);
250250
stopwatch.Stop();
251251
logger.LogTrace("{Timestamp}ms: Generated OpenAPI with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
252252
}
@@ -413,7 +413,7 @@ private static async Task<ReadResult> ParseOpenApiAsync(string openApiFile, bool
413413
/// </summary>
414414
/// <param name="csdl">The CSDL stream.</param>
415415
/// <returns>An OpenAPI document.</returns>
416-
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string format, string? metadataVersion = null, IConfiguration? settings = null, CancellationToken token = default)
416+
public static async Task<OpenApiDocument> ConvertCsdlToOpenApiAsync(Stream csdl, string format, string? metadataVersion = null, IConfiguration? settings = null, CancellationToken token = default)
417417
{
418418
using var reader = new StreamReader(csdl);
419419
var csdlText = await reader.ReadToEndAsync(token).ConfigureAwait(false);
@@ -588,7 +588,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
588588
}
589589

590590
var format = OpenApiModelFactory.GetFormat(options.OpenApi);
591-
var document = await GetOpenApi(options, format, logger, null, cancellationToken).ConfigureAwait(false);
591+
var document = await GetOpenApiAsync(options, format, logger, null, cancellationToken).ConfigureAwait(false);
592592

593593
using (logger.BeginScope("Creating diagram"))
594594
{
@@ -750,7 +750,7 @@ internal static async Task PluginManifestAsync(HidiOptions options, ILogger logg
750750

751751
// Load OpenAPI document
752752
var format = OpenApiModelFactory.GetFormat(options.OpenApi);
753-
var document = await GetOpenApi(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
753+
var document = await GetOpenApiAsync(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
754754

755755
cancellationToken.ThrowIfCancellationRequested();
756756

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>Latest</LangVersion>
@@ -22,6 +22,7 @@
2222
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
2323
</PropertyGroup>
2424
<ItemGroup>
25+
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
2526
<PackageReference Include="System.Text.Json" Version="8.0.4" />
2627
</ItemGroup>
2728

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Text.Json.Nodes;
78
using Microsoft.OpenApi.Helpers;
89
using Microsoft.OpenApi.Interfaces;
@@ -129,14 +130,14 @@ private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string,
129130
* Check if there is any example with an empty array as its value and set the flag `hasEmptyArray` to true
130131
* */
131132
var hasEmptyArray = examples.Values.Any( static example =>
132-
example.Value is OpenApiArray arr && arr.Count == 0
133+
example.Value is JsonArray arr && arr.Count == 0
133134
);
134135

135136
if (hasEmptyArray)
136137
{
137138
writer.WritePropertyName(OpenApiConstants.Examples);
138139
writer.WriteStartObject();
139-
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is OpenApiArray arr && arr.Count == 0))
140+
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is JsonArray arr && arr.Count == 0))
140141
{
141142
writer.WritePropertyName(kvp.Key);
142143
writer.WriteStartObject();

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.OpenApi.Models
1414
/// <summary>
1515
/// The Schema Object allows the definition of input and output data types.
1616
/// </summary>
17-
public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable
17+
public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable
1818
{
1919
private JsonNode _example;
2020
private JsonNode _default;
@@ -888,7 +888,10 @@ private void DowncastTypeArrayToV2OrV3(string[] array, IOpenApiWriter writer, Op
888888
// Find the non-null value and write it out
889889
var nonNullValue = array.First(v => v != OpenApiConstants.Null);
890890
writer.WriteProperty(OpenApiConstants.Type, nonNullValue);
891-
writer.WriteProperty(nullableProp, true);
891+
if (!Nullable)
892+
{
893+
writer.WriteProperty(nullableProp, true);
894+
}
892895
}
893896
}
894897
}

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
8686

8787
if (settings.LoadExternalRefs)
8888
{
89-
var diagnosticExternalRefs = await LoadExternalRefs(document, cancellationToken, settings, format);
89+
var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format);
9090
// Merge diagnostics of external reference
9191
if (diagnosticExternalRefs != null)
9292
{
@@ -189,7 +189,7 @@ private JsonNode LoadJsonNodes(TextReader input)
189189
return nodes;
190190
}
191191

192-
private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
192+
private async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
193193
{
194194
// Create workspace for all documents to live in.
195195
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Threading.Tasks;
1111
using Microsoft.OpenApi.Interfaces;
1212
using Microsoft.OpenApi.Models;
13+
using Microsoft.VisualStudio.Threading;
1314

1415
namespace Microsoft.OpenApi.Reader
1516
{
@@ -19,6 +20,8 @@ namespace Microsoft.OpenApi.Reader
1920
public static class OpenApiModelFactory
2021
{
2122
private static readonly HttpClient _httpClient = new();
23+
private static readonly JoinableTaskContext _joinableTaskContext = new();
24+
private static readonly JoinableTaskFactory _joinableTaskFactory = new(_joinableTaskContext);
2225

2326
static OpenApiModelFactory()
2427
{
@@ -33,7 +36,7 @@ static OpenApiModelFactory()
3336
/// <returns>An OpenAPI document instance.</returns>
3437
public static ReadResult Load(string url, OpenApiReaderSettings settings = null)
3538
{
36-
return LoadAsync(url, settings).GetAwaiter().GetResult();
39+
return _joinableTaskFactory.Run(async () => await LoadAsync(url, settings));
3740
}
3841

3942
/// <summary>
@@ -49,7 +52,9 @@ public static ReadResult Load(Stream stream,
4952
{
5053
settings ??= new OpenApiReaderSettings();
5154

52-
var result = LoadAsync(stream, format, settings).GetAwaiter().GetResult();
55+
// Run the async method synchronously using JoinableTaskFactory
56+
var result = _joinableTaskFactory.Run(async () => await LoadAsync(stream, format, settings));
57+
5358
if (!settings.LeaveStreamOpen)
5459
{
5560
stream.Dispose();
@@ -69,7 +74,9 @@ public static ReadResult Load(TextReader input,
6974
string format,
7075
OpenApiReaderSettings settings = null)
7176
{
72-
return LoadAsync(input, format, settings).GetAwaiter().GetResult();
77+
// Run the async method synchronously using JoinableTaskFactory
78+
var result = _joinableTaskFactory.Run(async () => await LoadAsync(input, format, settings));
79+
return result;
7380
}
7481

7582
/// <summary>
@@ -81,7 +88,7 @@ public static ReadResult Load(TextReader input,
8188
public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings settings = null)
8289
{
8390
var format = GetFormat(url);
84-
var stream = await GetStream(url);
91+
var stream = await GetStreamAsync(url);
8592
return await LoadAsync(stream, format, settings);
8693
}
8794

@@ -145,7 +152,24 @@ public static ReadResult Parse(string input,
145152
format ??= OpenApiConstants.Json;
146153
settings ??= new OpenApiReaderSettings();
147154
using var reader = new StringReader(input);
148-
return LoadAsync(reader, format, settings).GetAwaiter().GetResult();
155+
156+
return _joinableTaskFactory.Run(async () => await ParseAsync(input, reader, format, settings));
157+
}
158+
159+
/// <summary>
160+
/// An Async method to prevent synchornously blocking the calling thread.
161+
/// </summary>
162+
/// <param name="input"></param>
163+
/// <param name="reader"></param>
164+
/// <param name="format"></param>
165+
/// <param name="settings"></param>
166+
/// <returns></returns>
167+
public static async Task<ReadResult> ParseAsync(string input,
168+
StringReader reader,
169+
string format = null,
170+
OpenApiReaderSettings settings = null)
171+
{
172+
return await LoadAsync(reader, format, settings);
149173
}
150174

151175
/// <summary>
@@ -183,7 +207,9 @@ public static T Load<T>(string url, OpenApiSpecVersion version, out OpenApiDiagn
183207
{
184208
var format = GetFormat(url);
185209
settings ??= new OpenApiReaderSettings();
186-
var stream = GetStream(url).GetAwaiter().GetResult();
210+
211+
var stream = _joinableTaskFactory.Run(async () => await GetStreamAsync(url));
212+
187213
return Load<T>(stream, version, format, out diagnostic, settings);
188214
}
189215

@@ -227,7 +253,8 @@ private static string GetContentType(string url)
227253
{
228254
if (!string.IsNullOrEmpty(url))
229255
{
230-
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
256+
var response = _joinableTaskFactory.Run(async () => await _httpClient.GetAsync(url));
257+
//var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
231258
var mediaType = response.Content.Headers.ContentType.MediaType;
232259
return mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First();
233260
}
@@ -260,7 +287,7 @@ public static string GetFormat(string url)
260287
return null;
261288
}
262289

263-
private static async Task<Stream> GetStream(string url)
290+
private static async Task<Stream> GetStreamAsync(string url)
264291
{
265292
Stream stream;
266293
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
@@ -297,6 +324,5 @@ SecurityException or
297324

298325
return stream;
299326
}
300-
301327
}
302328
}

src/Microsoft.OpenApi/Reader/Services/DefaultStreamLoader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public DefaultStreamLoader(Uri baseUrl)
2727
{
2828
this.baseUrl = baseUrl;
2929
}
30+
/// <inheritdoc/>
3031

3132
[Obsolete]
3233
[EditorBrowsable(EditorBrowsableState.Never)]

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using Microsoft.Extensions.Logging;
@@ -232,7 +232,7 @@ public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
232232

233233
// Act
234234
using var stream = File.OpenRead(filePath);
235-
var doc = new OpenApiStreamReader().Read(stream, out var diagnostic);
235+
var doc = OpenApiDocument.Load(stream, "yaml").OpenApiDocument;
236236

237237
var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds);
238238
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate);

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.CommandLine;
@@ -13,6 +13,7 @@
1313
using Microsoft.OpenApi.OData;
1414
using Microsoft.OpenApi.Reader;
1515
using Microsoft.OpenApi.Readers;
16+
using Microsoft.OpenApi.Services;
1617
using Xunit;
1718

1819
namespace Microsoft.OpenApi.Hidi.Tests
@@ -27,44 +28,6 @@ public OpenApiServiceTests()
2728
_logger = new Logger<OpenApiServiceTests>(_loggerFactory);
2829
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yml, new OpenApiYamlReader());
2930
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
30-
31-
[Fact]
32-
public async Task ReturnConvertedCSDLFileAsync()
33-
{
34-
// Arrange
35-
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles", "Todo.xml");
36-
var fileInput = new FileInfo(filePath);
37-
var csdlStream = fileInput.OpenRead();
38-
// Act
39-
var openApiDoc = await OpenApiService.ConvertCsdlToOpenApiAsync(csdlStream);
40-
var expectedPathCount = 5;
41-
42-
// Assert
43-
Assert.NotNull(openApiDoc);
44-
Assert.NotEmpty(openApiDoc.Paths);
45-
Assert.Equal(expectedPathCount, openApiDoc.Paths.Count);
46-
}
47-
48-
[Theory]
49-
[InlineData("Todos.Todo.UpdateTodo", null, 1)]
50-
[InlineData("Todos.Todo.ListTodo", null, 1)]
51-
[InlineData(null, "Todos.Todo", 5)]
52-
public async Task ReturnFilteredOpenApiDocBasedOnOperationIdsAndInputCsdlDocumentAsync(string? operationIds, string? tags, int expectedPathCount)
53-
{
54-
// Arrange
55-
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles", "Todo.xml");
56-
var fileInput = new FileInfo(filePath);
57-
var csdlStream = fileInput.OpenRead();
58-
59-
// Act
60-
var openApiDoc = await OpenApiService.ConvertCsdlToOpenApiAsync(csdlStream);
61-
var predicate = OpenApiFilterService.CreatePredicate(operationIds, tags);
62-
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(openApiDoc, predicate);
63-
64-
// Assert
65-
Assert.NotNull(subsetOpenApiDocument);
66-
Assert.NotEmpty(subsetOpenApiDocument.Paths);
67-
Assert.Equal(expectedPathCount, subsetOpenApiDocument.Paths.Count);
6831
}
6932

7033
[Fact]
@@ -198,23 +161,6 @@ public async Task ShowCommandGeneratesMermaidHtmlFileWithMermaidDiagramAsync()
198161
Assert.True(File.Exists(filePath));
199162
}
200163

201-
[Fact]
202-
public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiagramAsync()
203-
{
204-
var options = new HidiOptions
205-
{
206-
Csdl = Path.Combine("UtilityFiles", "Todo.xml"),
207-
CsdlFilter = "todos",
208-
Output = new("sample.md")
209-
};
210-
211-
// create a dummy ILogger instance for testing
212-
await OpenApiService.ShowOpenApiDocumentAsync(options, _logger);
213-
214-
var output = await File.ReadAllTextAsync(options.Output.FullName);
215-
Assert.Contains("graph LR", output, StringComparison.Ordinal);
216-
}
217-
218164
[Fact]
219165
public Task ThrowIfOpenApiUrlIsNotProvidedWhenValidatingAsync()
220166
{
@@ -309,24 +255,6 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAsync()
309255
Assert.NotEmpty(output);
310256
}
311257

312-
[Fact]
313-
public async Task TransformCommandConvertsCsdlWithDefaultOutputNameAsync()
314-
{
315-
var options = new HidiOptions
316-
{
317-
Csdl = Path.Combine("UtilityFiles", "Todo.xml"),
318-
CleanOutput = true,
319-
TerseOutput = false,
320-
InlineLocal = false,
321-
InlineExternal = false,
322-
};
323-
// create a dummy ILogger instance for testing
324-
await OpenApiService.TransformOpenApiDocumentAsync(options, _logger);
325-
326-
var output = await File.ReadAllTextAsync("output.yml");
327-
Assert.NotEmpty(output);
328-
}
329-
330258
[Fact]
331259
public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchFormatAsync()
332260
{

test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -64,8 +64,8 @@ public async Task StreamShouldReadWhenInitializedAsync()
6464
var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml");
6565

6666
// Read V3 as YAML
67-
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);
68-
Assert.NotNull(openApiDocument);
67+
var result = OpenApiDocument.Load(stream, "yaml");
68+
Assert.NotNull(result.OpenApiDocument);
6969
}
7070
}
7171
}

0 commit comments

Comments
 (0)