diff --git a/README.md b/README.md index c804787c1..c4b6068c0 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ var httpClient = new HttpClient var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml"); // Read V3 as YAML -var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic); +var openApiDocument = OpenApiDocument.LoadAsync(stream).OpenApiDocument; // Write V2 as JSON var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json); diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 7dfb5d797..e2efbbdb5 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -97,7 +97,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog } // Load OpenAPI document - var format = OpenApiModelFactory.GetFormat(options.OpenApi); + var format = await OpenApiModelFactory.GetFormatAsync(options.OpenApi, cancellationToken).ConfigureAwait(false); var document = await GetOpenApiAsync(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false); if (options.FilterOptions != null) @@ -396,8 +396,7 @@ private static async Task ParseOpenApiAsync(string openApiFile, bool new Uri("file://" + new FileInfo(openApiFile).DirectoryName + Path.DirectorySeparatorChar) }; - var format = OpenApiModelFactory.GetFormat(openApiFile); - result = await OpenApiDocument.LoadAsync(stream, format, settings, cancellationToken).ConfigureAwait(false); + result = await OpenApiDocument.LoadAsync(stream, settings, cancellationToken).ConfigureAwait(false); logger.LogTrace("{Timestamp}ms: Completed parsing.", stopwatch.ElapsedMilliseconds); @@ -421,7 +420,7 @@ public static async Task ConvertCsdlToOpenApiAsync(Stream csdl, settings ??= SettingsUtilities.GetConfiguration(); var document = edmModel.ConvertToOpenApi(SettingsUtilities.GetOpenApiConvertSettings(settings, metadataVersion)); - document = FixReferences(document, format); + document = await FixReferencesAsync(document, format).ConfigureAwait(false); return document; } @@ -431,7 +430,7 @@ public static async Task ConvertCsdlToOpenApiAsync(Stream csdl, /// /// The converted OpenApiDocument. /// A valid OpenApiDocument instance. - public static OpenApiDocument FixReferences(OpenApiDocument document, string format) + public static async Task FixReferencesAsync(OpenApiDocument document, string format) { // This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance. // So we write it out, and read it back in again to fix it up. @@ -439,9 +438,9 @@ public static OpenApiDocument FixReferences(OpenApiDocument document, string for var sb = new StringBuilder(); document.SerializeAsV3(new OpenApiYamlWriter(new StringWriter(sb))); - var doc = OpenApiDocument.Parse(sb.ToString(), format).OpenApiDocument; + var result = await OpenApiDocument.ParseAsync(sb.ToString()).ConfigureAwait(false); - return doc; + return result.OpenApiDocument; } /// @@ -587,7 +586,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl throw new ArgumentException("Please input a file path or URL"); } - var format = OpenApiModelFactory.GetFormat(options.OpenApi); + var format = await OpenApiModelFactory.GetFormatAsync(options.OpenApi, cancellationToken).ConfigureAwait(false); var document = await GetOpenApiAsync(options, format, logger, null, cancellationToken).ConfigureAwait(false); using (logger.BeginScope("Creating diagram")) @@ -749,10 +748,10 @@ internal static async Task PluginManifestAsync(HidiOptions options, ILogger logg } // Load OpenAPI document - var format = OpenApiModelFactory.GetFormat(options.OpenApi); + var format = await OpenApiModelFactory.GetFormatAsync(options.OpenApi, cancellationToken).ConfigureAwait(false); var document = await GetOpenApiAsync(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false); - cancellationToken.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); if (options.FilterOptions != null) { diff --git a/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs index cff6dd1da..69fe98b43 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs @@ -10,7 +10,6 @@ using Microsoft.OpenApi.Reader; using SharpYaml.Serialization; using System.Linq; -using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Readers { @@ -46,26 +45,26 @@ public async Task ReadAsync(TextReader input, } /// - public T ReadFragment(TextReader input, - OpenApiSpecVersion version, - out OpenApiDiagnostic diagnostic, - OpenApiReaderSettings settings = null) where T : IOpenApiElement + public async Task> ReadFragmentAsync(TextReader input, + OpenApiSpecVersion version, + OpenApiReaderSettings settings = null, + CancellationToken token = default) where T : IOpenApiElement { JsonNode jsonNode; // Parse the YAML try { - jsonNode = LoadJsonNodesFromYamlDocument(input); + jsonNode = await Task.Run(() => LoadJsonNodesFromYamlDocument(input)); } catch (JsonException ex) { - diagnostic = new(); + var diagnostic = new OpenApiDiagnostic(); diagnostic.Errors.Add(new($"#line={ex.LineNumber}", ex.Message)); return default; } - return ReadFragment(jsonNode, version, out diagnostic); + return ReadFragment(jsonNode, version, settings); } /// @@ -82,15 +81,15 @@ static JsonNode LoadJsonNodesFromYamlDocument(TextReader input) } /// - public async Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default) + public async Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, CancellationToken cancellationToken = default) { - return await OpenApiReaderRegistry.DefaultReader.ReadAsync(jsonNode, settings, OpenApiConstants.Yaml, cancellationToken); + return await OpenApiReaderRegistry.DefaultReader.ReadAsync(jsonNode, settings, cancellationToken); } /// - public T ReadFragment(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement + public ReadFragmentResult ReadFragment(JsonNode input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T : IOpenApiElement { - return OpenApiReaderRegistry.DefaultReader.ReadFragment(input, version, out diagnostic); + return OpenApiReaderRegistry.DefaultReader.ReadFragment(input, version, settings); } } } diff --git a/src/Microsoft.OpenApi.Workbench/MainModel.cs b/src/Microsoft.OpenApi.Workbench/MainModel.cs index d518645a5..f67f7154c 100644 --- a/src/Microsoft.OpenApi.Workbench/MainModel.cs +++ b/src/Microsoft.OpenApi.Workbench/MainModel.cs @@ -257,8 +257,7 @@ internal async Task ParseDocumentAsync() } } - var format = OpenApiModelFactory.GetFormat(_inputFile); - var readResult = await OpenApiDocument.LoadAsync(stream, format); + var readResult = await OpenApiDocument.LoadAsync(stream); var document = readResult.OpenApiDocument; var context = readResult.OpenApiDiagnostic; diff --git a/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs b/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs index 5f8b1cb22..1b3fa9e37 100644 --- a/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs +++ b/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs @@ -29,28 +29,26 @@ public interface IOpenApiReader /// The JsonNode input. /// The Reader settings to be used during parsing. /// Propagates notifications that operations should be cancelled. - /// The OpenAPI format. /// - Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default); + Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, CancellationToken cancellationToken = default); /// /// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element. /// /// TextReader containing OpenAPI description to parse. /// Version of the OpenAPI specification that the fragment conforms to. - /// Returns diagnostic object containing errors detected during parsing. /// The OpenApiReader settings. + /// /// Instance of newly created IOpenApiElement. - T ReadFragment(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement; + Task> ReadFragmentAsync(TextReader input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null, CancellationToken token = default) where T : IOpenApiElement; /// /// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element. /// /// TextReader containing OpenAPI description to parse. /// Version of the OpenAPI specification that the fragment conforms to. - /// Returns diagnostic object containing errors detected during parsing. /// The OpenApiReader settings. /// Instance of newly created IOpenApiElement. - T ReadFragment(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement; + ReadFragmentResult ReadFragment(JsonNode input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T: IOpenApiElement; } } diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index 0261fcff9..1ac3b9a39 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -535,93 +535,51 @@ private static string ConvertByteArrayToString(byte[] hash) return Workspace?.ResolveReference(uriLocation); } - /// - /// Parses a local file path or Url into an Open API document. - /// - /// The path to the OpenAPI file. - /// - /// - public static ReadResult Load(string url, OpenApiReaderSettings? settings = null) - { - return OpenApiModelFactory.Load(url, settings); - } - - /// - /// Reads the stream input and parses it into an Open API document. - /// - /// Stream containing OpenAPI description to parse. - /// The OpenAPI format to use during parsing. - /// The OpenApi reader settings. - /// - public static ReadResult Load(Stream stream, - string format, - OpenApiReaderSettings? settings = null) - { - return OpenApiModelFactory.Load(stream, format, settings); - } - - /// - /// Reads the text reader content and parses it into an Open API document. - /// - /// TextReader containing OpenAPI description to parse. - /// The OpenAPI format to use during parsing. - /// The OpenApi reader settings. - /// - public static ReadResult Load(TextReader input, - string format, - OpenApiReaderSettings? settings = null) - { - return OpenApiModelFactory.Load(input, format, settings); - } - /// /// Parses a local file path or Url into an Open API document. /// /// The path to the OpenAPI file. /// The OpenApi reader settings. + /// /// - public static async Task LoadAsync(string url, OpenApiReaderSettings? settings = null) + public static async Task LoadAsync(string url, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default) { - return await OpenApiModelFactory.LoadAsync(url, settings); + return await OpenApiModelFactory.LoadAsync(url, settings, cancellationToken); } /// /// Reads the stream input and parses it into an Open API document. /// /// Stream containing OpenAPI description to parse. - /// The OpenAPI format to use during parsing. /// The OpenApi reader settings. /// Propagates information about operation cancelling. /// - public static async Task LoadAsync(Stream stream, string format, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default) + public static async Task LoadAsync(Stream stream, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default) { - return await OpenApiModelFactory.LoadAsync(stream, format, settings, cancellationToken); + return await OpenApiModelFactory.LoadAsync(stream, settings: settings, cancellationToken: cancellationToken); } /// /// Reads the text reader content and parses it into an Open API document. /// /// TextReader containing OpenAPI description to parse. - /// The OpenAPI format to use during parsing. /// The OpenApi reader settings. /// - public static async Task LoadAsync(TextReader input, string format, OpenApiReaderSettings? settings = null) + public static async Task LoadAsync(TextReader input, OpenApiReaderSettings? settings = null) { - return await OpenApiModelFactory.LoadAsync(input, format, settings); + return await OpenApiModelFactory.LoadAsync(input, settings: settings); } /// /// Parses a string into a object. /// /// The string input. - /// /// /// - public static ReadResult Parse(string input, - string? format = null, - OpenApiReaderSettings? settings = null) + public static async Task ParseAsync(string input, + OpenApiReaderSettings? settings = null) { - return OpenApiModelFactory.Parse(input, format, settings); + return await OpenApiModelFactory.ParseAsync(input, settings); } } diff --git a/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs b/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs index 27aad722e..dabe93bf8 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs @@ -14,7 +14,6 @@ using Microsoft.OpenApi.Services; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Reader.Services; -using System.Collections.Generic; using System; namespace Microsoft.OpenApi.Reader @@ -42,7 +41,7 @@ public async Task ReadAsync(TextReader input, // Parse the JSON text in the TextReader into JsonNodes try { - jsonNode = LoadJsonNodes(input); + jsonNode = await LoadJsonNodesAsync(input, cancellationToken); } catch (JsonException ex) { @@ -62,12 +61,10 @@ public async Task ReadAsync(TextReader input, /// /// The JsonNode input. /// The Reader settings to be used during parsing. - /// The OpenAPI format. /// Propagates notifications that operations should be cancelled. /// public async Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, - string format = null, CancellationToken cancellationToken = default) { var diagnostic = new OpenApiDiagnostic(); @@ -86,7 +83,7 @@ public async Task ReadAsync(JsonNode jsonNode, if (settings.LoadExternalRefs) { - var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format); + var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings); // Merge diagnostics of external reference if (diagnosticExternalRefs != null) { @@ -124,35 +121,34 @@ public async Task ReadAsync(JsonNode jsonNode, } /// - public T ReadFragment(TextReader input, - OpenApiSpecVersion version, - out OpenApiDiagnostic diagnostic, - OpenApiReaderSettings settings = null) where T : IOpenApiElement + public async Task> ReadFragmentAsync(TextReader input, + OpenApiSpecVersion version, + OpenApiReaderSettings settings = null, + CancellationToken token = default) where T: IOpenApiElement { JsonNode jsonNode; // Parse the JSON try { - jsonNode = LoadJsonNodes(input); + jsonNode = await LoadJsonNodesAsync(input, token); } catch (JsonException ex) { - diagnostic = new(); + var diagnostic = new OpenApiDiagnostic(); diagnostic.Errors.Add(new($"#line={ex.LineNumber}", ex.Message)); return default; } - return ReadFragment(jsonNode, version, out diagnostic); + return ReadFragment(jsonNode, version, settings); } /// - public T ReadFragment(JsonNode input, - OpenApiSpecVersion version, - out OpenApiDiagnostic diagnostic, - OpenApiReaderSettings settings = null) where T : IOpenApiElement + public ReadFragmentResult ReadFragment(JsonNode input, + OpenApiSpecVersion version, + OpenApiReaderSettings settings = null) where T : IOpenApiElement { - diagnostic = new(); + var diagnostic = new OpenApiDiagnostic(); settings ??= new OpenApiReaderSettings(); var context = new ParsingContext(diagnostic) { @@ -162,7 +158,7 @@ public T ReadFragment(JsonNode input, IOpenApiElement element = null; try { - // Parse the OpenAPI element + // Parse the OpenAPI element asynchronously element = context.ParseFragment(input, version); } catch (OpenApiException ex) @@ -180,16 +176,24 @@ public T ReadFragment(JsonNode input, } } - return (T)element; + return new ReadFragmentResult + { + Element = (T)element, + OpenApiDiagnostic = diagnostic + }; } - private JsonNode LoadJsonNodes(TextReader input) + private async Task LoadJsonNodesAsync(TextReader input, CancellationToken token = default) { - var nodes = JsonNode.Parse(input.ReadToEnd()); - return nodes; +#if NETSTANDARD2_0 + var content = await input.ReadToEndAsync(); +#else + var content = await input.ReadToEndAsync(token); +#endif + return JsonNode.Parse(content); } - private async Task LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null) + private async Task LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings) { // Create workspace for all documents to live in. var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri); @@ -198,7 +202,7 @@ private async Task LoadExternalRefsAsync(OpenApiDocument docu // Load this root document into the workspace var streamLoader = new DefaultStreamLoader(settings.BaseUrl); var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings); - return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, cancellationToken); + return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, null, cancellationToken); } } } diff --git a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs index ddabdc6be..b665f7259 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs @@ -8,6 +8,7 @@ using System.Security; using System.Threading; using System.Threading.Tasks; +using Microsoft.OpenApi.Exceptions; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; @@ -25,70 +26,16 @@ static OpenApiModelFactory() OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Json, new OpenApiJsonReader()); } - /// - /// Loads the input URL and parses it into an Open API document. - /// - /// The path to the OpenAPI file. - /// The OpenApi reader settings. - /// An OpenAPI document instance. - public static ReadResult Load(string url, OpenApiReaderSettings settings = null) - { -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - return LoadAsync(url, settings).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - } - - /// - /// Loads the input stream and parses it into an Open API document. - /// - /// The input stream. - /// The OpenApi reader settings. - /// The OpenAPI format. - /// An OpenAPI document instance. - public static ReadResult Load(Stream stream, - string format, - OpenApiReaderSettings settings = null) - { - settings ??= new OpenApiReaderSettings(); - -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - var result = LoadAsync(stream, format, settings).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - - if (!settings.LeaveStreamOpen) - { - stream.Dispose(); - } - - return result; - } - - /// - /// Loads the TextReader input and parses it into an Open API document. - /// - /// The TextReader input. - /// The OpenApi reader settings. - /// The Open API format - /// An OpenAPI document instance. - public static ReadResult Load(TextReader input, - string format, - OpenApiReaderSettings settings = null) - { -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - var result = LoadAsync(input, format, settings).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - return result; - } - /// /// Loads the input URL and parses it into an Open API document. /// /// The path to the OpenAPI file /// The OpenApi reader settings. + /// /// - public static async Task LoadAsync(string url, OpenApiReaderSettings settings = null) + public static async Task LoadAsync(string url, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default) { - var format = GetFormat(url); + var format = await GetFormatAsync(url, cancellationToken); var stream = await GetStreamAsync(url); return await LoadAsync(stream, format, settings); } @@ -101,29 +48,23 @@ public static async Task LoadAsync(string url, OpenApiReaderSettings /// Propagates notification that operations should be cancelled. /// The Open API format /// - public static async Task LoadAsync(Stream input, string format, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default) + public static async Task LoadAsync(Stream input, string format = null, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default) { - Utils.CheckArgumentNull(format, nameof(format)); settings ??= new OpenApiReaderSettings(); - Stream preparedStream; - - // Avoid buffering for JSON documents - if (input is MemoryStream || format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase)) - { - preparedStream = input; - } - else - { - // Buffer stream for non-JSON formats (e.g., YAML) since they require synchronous reading - preparedStream = new MemoryStream(); - await input.CopyToAsync(preparedStream, 81920, cancellationToken); - preparedStream.Position = 0; - } + // Prepare the stream based on seekability and format + var (preparedStream, detectedFormat) = await PrepareStreamForReadingAsync(input, format, cancellationToken); + format ??= detectedFormat; // Use StreamReader to process the prepared stream (buffered for YAML, direct for JSON) using var reader = new StreamReader(preparedStream, default, true, -1, settings.LeaveStreamOpen); - return await LoadAsync(reader, format, settings, cancellationToken); + var result = await LoadAsync(reader, format, settings, cancellationToken); + + if (!settings.LeaveStreamOpen) + { + input.Dispose(); + } + return result; } @@ -135,9 +76,9 @@ public static async Task LoadAsync(Stream input, string format, Open /// The OpenApi reader settings. /// Propagates notification that operations should be cancelled. /// - public static async Task LoadAsync(TextReader input, string format, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default) + public static async Task LoadAsync(TextReader input, string format = null, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default) { - Utils.CheckArgumentNull(format, nameof(format)); + format ??= InspectTextReaderFormat(input); var reader = OpenApiReaderRegistry.GetReader(format); return await reader.ReadAsync(input, settings, cancellationToken); } @@ -145,36 +86,15 @@ public static async Task LoadAsync(TextReader input, string format, /// /// Reads the input string and parses it into an Open API document. /// - /// The input string. - /// The Open API format - /// The OpenApi reader settings. - /// An OpenAPI document instance. - public static ReadResult Parse(string input, - string format = null, - OpenApiReaderSettings settings = null) - { - format ??= OpenApiConstants.Json; - settings ??= new OpenApiReaderSettings(); - using var reader = new StringReader(input); - -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - return ParseAsync(input, reader, format, settings).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - } - - /// - /// An Async method to prevent synchornously blocking the calling thread. - /// /// - /// - /// /// /// public static async Task ParseAsync(string input, - StringReader reader, - string format = null, - OpenApiReaderSettings settings = null) + OpenApiReaderSettings settings = null) { + var format = InspectInputFormat(input); + settings ??= new OpenApiReaderSettings(); + using var reader = new StringReader(input); return await LoadAsync(reader, format, settings); } @@ -183,90 +103,87 @@ public static async Task ParseAsync(string input, /// /// The input string. /// - /// The diagnostic entity containing information from the reading process. - /// The Open API format /// The OpenApi reader settings. /// An OpenAPI document instance. - public static T Parse(string input, - OpenApiSpecVersion version, - out OpenApiDiagnostic diagnostic, - string format = null, - OpenApiReaderSettings settings = null) where T : IOpenApiElement + public static async Task> ParseAsync(string input, + OpenApiSpecVersion version, + OpenApiReaderSettings settings = null) where T : IOpenApiElement { - format ??= OpenApiConstants.Json; + var format = InspectInputFormat(input); settings ??= new OpenApiReaderSettings(); using var reader = new StringReader(input); - return Load(reader, version, out diagnostic, format, settings); + return await LoadAsync(reader, version, format, settings); } /// /// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element. /// - /// /// The path to the OpenAPI file /// Version of the OpenAPI specification that the fragment conforms to. - /// Returns diagnostic object containing errors detected during parsing. /// The OpenApiReader settings. + /// /// Instance of newly created IOpenApiElement. /// The OpenAPI element. - public static T Load(string url, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement + public static async Task> LoadAsync(string url, + OpenApiSpecVersion version, + OpenApiReaderSettings settings = null, + CancellationToken cancellationToken = default) where T : IOpenApiElement { - var format = GetFormat(url); + var format = await GetFormatAsync(url, cancellationToken); settings ??= new OpenApiReaderSettings(); - -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - var stream = GetStreamAsync(url).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - - return Load(stream, version, format, out diagnostic, settings); + var stream = await GetStreamAsync(url); + return await LoadAsync(stream, version, format, settings); } /// /// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element. /// - /// /// Stream containing OpenAPI description to parse. /// Version of the OpenAPI specification that the fragment conforms to. /// - /// Returns diagnostic object containing errors detected during parsing. /// The OpenApiReader settings. /// Instance of newly created IOpenApiElement. /// The OpenAPI element. - public static T Load(Stream input, OpenApiSpecVersion version, string format, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement + public static async Task> LoadAsync(Stream input, + OpenApiSpecVersion version, + string format = null, + OpenApiReaderSettings settings = null) where T : IOpenApiElement { - format ??= OpenApiConstants.Json; + format ??= InspectStreamFormat(input); using var reader = new StreamReader(input); - return Load(reader, version, out diagnostic, format, settings); + return await LoadAsync(reader, version, format, settings); } /// /// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element. /// - /// /// TextReader containing OpenAPI description to parse. /// Version of the OpenAPI specification that the fragment conforms to. /// The OpenAPI format. - /// Returns diagnostic object containing errors detected during parsing. /// The OpenApiReader settings. /// Instance of newly created IOpenApiElement. /// The OpenAPI element. - public static T Load(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, string format, OpenApiReaderSettings settings = null) where T : IOpenApiElement + public static async Task> LoadAsync(TextReader input, + OpenApiSpecVersion version, + string format = null, + OpenApiReaderSettings settings = null) where T : IOpenApiElement { - format ??= OpenApiConstants.Json; - return OpenApiReaderRegistry.GetReader(format).ReadFragment(input, version, out diagnostic, settings); + format ??= InspectTextReaderFormat(input); + return await OpenApiReaderRegistry.GetReader(format).ReadFragmentAsync(input, version, settings); } - - private static string GetContentType(string url) + private static async Task GetContentTypeAsync(string url, CancellationToken token = default) { if (!string.IsNullOrEmpty(url)) { -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - var response = _httpClient.GetAsync(url).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - +#if NETSTANDARD2_0 + var response = await _httpClient.GetAsync(url); +#else + var response = await _httpClient.GetAsync(url, token); +#endif var mediaType = response.Content.Headers.ContentType.MediaType; - return mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); + var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); + return contentType; } return null; @@ -276,18 +193,28 @@ private static string GetContentType(string url) /// Infers the OpenAPI format from the input URL. /// /// The input URL. + /// /// The OpenAPI format. - public static string GetFormat(string url) + public static async Task GetFormatAsync(string url, CancellationToken token = default) { if (!string.IsNullOrEmpty(url)) { - if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || + url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { // URL examples ---> https://example.com/path/to/file.json, https://example.com/path/to/file.yaml var path = new Uri(url); - var urlSuffix = path.Segments[path.Segments.Length - 1].Split('.').LastOrDefault(); - - return !string.IsNullOrEmpty(urlSuffix) ? urlSuffix : GetContentType(url).Split('/').LastOrDefault(); + var urlSuffix = path.Segments.LastOrDefault().Split('.').LastOrDefault(); + + if (!string.IsNullOrEmpty(urlSuffix)) + { + return urlSuffix; + } + else + { + var contentType = await GetContentTypeAsync(url, token); + return contentType.Split('/').LastOrDefault(); + } } else { @@ -297,6 +224,100 @@ public static string GetFormat(string url) return null; } + private static async Task<(Stream preparedStream, string format)> PrepareStreamForReadingAsync(Stream input, string format, CancellationToken token = default) + { + Stream preparedStream = input; + + if (!input.CanSeek) + { + // Use a temporary buffer to read a small portion for format detection + using var bufferStream = new MemoryStream(); + await input.CopyToAsync(bufferStream, 1024, token); + bufferStream.Position = 0; + + // Inspect the format from the buffered portion + format ??= InspectStreamFormat(bufferStream); + + // If format is JSON, no need to buffer further — use the original stream. + if (format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase)) + { + preparedStream = input; + } + else + { + // YAML or other non-JSON format; copy remaining input to a new stream. + preparedStream = new MemoryStream(); + bufferStream.Position = 0; + await bufferStream.CopyToAsync(preparedStream, 81920, token); // Copy buffered portion + await input.CopyToAsync(preparedStream, 81920, token); // Copy remaining data + preparedStream.Position = 0; + } + } + else + { + format ??= InspectStreamFormat(input); + + if (!format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase)) + { + // Buffer stream for non-JSON formats (e.g., YAML) since they require synchronous reading + preparedStream = new MemoryStream(); + await input.CopyToAsync(preparedStream, 81920, token); + preparedStream.Position = 0; + } + } + + return(preparedStream, format); + } + + private static string InspectStreamFormat(Stream stream) + { + if (stream == null) throw new ArgumentNullException(nameof(stream)); + + long initialPosition = stream.Position; + int firstByte = stream.ReadByte(); + + // Check if stream is empty or contains only whitespace + if (firstByte == -1) + { + stream.Position = initialPosition; + throw new InvalidOperationException("Stream is empty or contains only whitespace."); + } + + // Skip whitespace if present and read the next non-whitespace byte + if (char.IsWhiteSpace((char)firstByte)) + { + firstByte = stream.ReadByte(); + + // If still whitespace or end of stream, throw an error + if (firstByte == -1 || char.IsWhiteSpace((char)firstByte)) + { + stream.Position = initialPosition; + throw new InvalidOperationException("Stream is empty or contains only whitespace."); + } + } + + stream.Position = initialPosition; // Reset the stream position to the beginning + + char firstChar = (char)firstByte; + return firstChar switch + { + '{' or '[' => OpenApiConstants.Json, // If the first character is '{' or '[', assume JSON + _ => OpenApiConstants.Yaml // Otherwise assume YAML + }; + } + + private static string InspectTextReaderFormat(TextReader reader) + { + // Read the first line or a few characters from the input + var input = reader.ReadLine().Trim(); + return InspectInputFormat(input); + } + + private static string InspectInputFormat(string input) + { + return input.StartsWith("{", StringComparison.OrdinalIgnoreCase) || input.StartsWith("[", StringComparison.OrdinalIgnoreCase) ? OpenApiConstants.Json : OpenApiConstants.Yaml; + } + private static async Task GetStreamAsync(string url) { Stream stream; diff --git a/src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs b/src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs index fa0040ff8..7caa28424 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Text.Json.Nodes; -using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.MicrosoftExtensions; using Microsoft.OpenApi.Validations; @@ -77,7 +76,7 @@ public class OpenApiReaderSettings /// /// Whether to leave the object open after reading - /// from an OpenApiStreamReader object. + /// from a StreamReader object. /// public bool LeaveStreamOpen { get; set; } diff --git a/src/Microsoft.OpenApi/Reader/ReadResult.cs b/src/Microsoft.OpenApi/Reader/ReadResult.cs index 77a18ff78..6ed3e647c 100644 --- a/src/Microsoft.OpenApi/Reader/ReadResult.cs +++ b/src/Microsoft.OpenApi/Reader/ReadResult.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Reader @@ -13,7 +14,23 @@ public class ReadResult /// /// The parsed OpenApiDocument. Null will be returned if the document could not be parsed. /// - public OpenApiDocument OpenApiDocument { set; get; } + public OpenApiDocument OpenApiDocument { get; set; } + /// + /// OpenApiDiagnostic contains the Errors reported while parsing + /// + public OpenApiDiagnostic OpenApiDiagnostic { get; set; } + } + + /// + /// Container object used for returning the result of reading an OpenAPI fragment + /// + public class ReadFragmentResult where T: IOpenApiElement + { + /// + /// The parsed fragment. + /// + public T Element { get; set; } + /// /// OpenApiDiagnostic contains the Errors reported while parsing /// diff --git a/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs b/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs index a3462da70..035f27774 100644 --- a/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs +++ b/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs @@ -22,7 +22,6 @@ public OpenApiWorkspaceLoader(OpenApiWorkspace workspace, IStreamLoader loader, internal async Task LoadAsync(OpenApiReference reference, OpenApiDocument document, - string format = null, OpenApiDiagnostic diagnostic = null, CancellationToken cancellationToken = default) { @@ -46,7 +45,7 @@ internal async Task LoadAsync(OpenApiReference reference, if (!_workspace.Contains(item.ExternalResource)) { var input = await _loader.LoadAsync(new(item.ExternalResource, UriKind.RelativeOrAbsolute)); - var result = await OpenApiDocument.LoadAsync(input, format, _readerSettings, cancellationToken); + var result = await OpenApiDocument.LoadAsync(input, _readerSettings, cancellationToken); // Merge diagnostics if (result.OpenApiDiagnostic != null) { @@ -54,7 +53,7 @@ internal async Task LoadAsync(OpenApiReference reference, } if (result.OpenApiDocument != null) { - var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, format, diagnostic, cancellationToken); + var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, diagnostic, cancellationToken); diagnostic = loadDiagnostic; } } diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs index 3bd9efd2a..53b2efcd2 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs @@ -224,7 +224,7 @@ public void ThrowsInvalidOperationExceptionInCreatePredicateWhenInvalidArguments } [Fact] - public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly() + public async Task CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly() { // Arrange var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles", "docWithReusableHeadersAndExamples.yaml"); @@ -232,10 +232,10 @@ public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly() // Act using var stream = File.OpenRead(filePath); - var doc = OpenApiDocument.Load(stream, "yaml").OpenApiDocument; + var res = await OpenApiDocument.LoadAsync(stream); var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds); - var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate); + var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(res.OpenApiDocument, predicate); var response = subsetOpenApiDocument.Paths["/items"].Operations[OperationType.Get]?.Responses?["200"]; var responseHeader = response?.Headers["x-custom-header"]; @@ -244,7 +244,7 @@ public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly() var targetExamples = subsetOpenApiDocument.Components?.Examples; // Assert - Assert.Same(doc.Servers, subsetOpenApiDocument.Servers); + Assert.Same(res.OpenApiDocument.Servers, subsetOpenApiDocument.Servers); Assert.False(responseHeader?.UnresolvedReference); Assert.False(mediaTypeExample?.UnresolvedReference); Assert.NotNull(targetHeaders); diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs index 5ecd58071..32a14dc49 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs @@ -23,18 +23,18 @@ public OpenApiDiagnosticTests() } [Fact] - public void DetectedSpecificationVersionShouldBeV2_0() + public async Task DetectedSpecificationVersionShouldBeV2_0() { - var actual = OpenApiDocument.Load("V2Tests/Samples/basic.v2.yaml"); + var actual = await OpenApiDocument.LoadAsync("V2Tests/Samples/basic.v2.yaml"); actual.OpenApiDiagnostic.Should().NotBeNull(); actual.OpenApiDiagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi2_0); } [Fact] - public void DetectedSpecificationVersionShouldBeV3_0() + public async Task DetectedSpecificationVersionShouldBeV3_0() { - var actual = OpenApiDocument.Load("V3Tests/Samples/OpenApiDocument/minimalDocument.yaml"); + var actual = await OpenApiDocument.LoadAsync("V3Tests/Samples/OpenApiDocument/minimalDocument.yaml"); actual.OpenApiDiagnostic.Should().NotBeNull(); actual.OpenApiDiagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi3_0); diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs index 82a410946..210f6f4a7 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs @@ -11,30 +11,30 @@ namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests { - public class OpenApiStreamReaderTests + public class OpenApiDocumentLoadTests { private const string SampleFolderPath = "V3Tests/Samples/OpenApiDocument/"; - public OpenApiStreamReaderTests() + public OpenApiDocumentLoadTests() { OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader()); } [Fact] - public void StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse() + public async Task StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse() { using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml")); var settings = new OpenApiReaderSettings { LeaveStreamOpen = false }; - _ = OpenApiDocument.Load(stream, "yaml", settings); + _ = await OpenApiDocument.LoadAsync(stream, settings); Assert.False(stream.CanRead); } [Fact] - public void StreamShouldNotCloseIfLeaveStreamOpenSettingEqualsTrue() + public async Task StreamShouldNotCloseIfLeaveStreamOpenSettingEqualsTrue() { using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml")); var settings = new OpenApiReaderSettings { LeaveStreamOpen = true }; - _ = OpenApiDocument.Load(stream, "yaml", settings); + _ = await OpenApiDocument.LoadAsync(stream, settings); Assert.True(stream.CanRead); } @@ -48,7 +48,7 @@ public async Task StreamShouldNotBeDisposedIfLeaveStreamOpenSettingIsTrueAsync() memoryStream.Position = 0; var stream = memoryStream; - var result = OpenApiDocument.Load(stream, "yaml", new OpenApiReaderSettings { LeaveStreamOpen = true }); + var result = await OpenApiDocument.LoadAsync(stream, new OpenApiReaderSettings { LeaveStreamOpen = true }); stream.Seek(0, SeekOrigin.Begin); // does not throw an object disposed exception Assert.True(stream.CanRead); } @@ -64,7 +64,7 @@ public async Task StreamShouldReadWhenInitializedAsync() var stream = await httpClient.GetStreamAsync("20fe7a7b720a0e48e5842d002ac418b12a8201df/tests/v3.0/pass/petstore.yaml"); // Read V3 as YAML - var result = OpenApiDocument.Load(stream, "yaml"); + var result = await OpenApiDocument.LoadAsync(stream); Assert.NotNull(result.OpenApiDocument); } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs index 0b044e78b..1f6cbb7e8 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Exceptions; using Microsoft.OpenApi.Models; @@ -12,11 +13,11 @@ namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests public class UnsupportedSpecVersionTests { [Fact] - public void ThrowOpenApiUnsupportedSpecVersionException() + public async Task ThrowOpenApiUnsupportedSpecVersionException() { try { - _ = OpenApiDocument.Load("OpenApiReaderTests/Samples/unsupported.v1.yaml"); + _ = await OpenApiDocument.LoadAsync("OpenApiReaderTests/Samples/unsupported.v1.yaml"); } catch (OpenApiUnsupportedSpecVersionException exception) { diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs index 4aca9b54e..fdceacd09 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Threading.Tasks; using Microsoft.OpenApi.Interfaces; @@ -44,7 +44,7 @@ public async Task LoadingDocumentWithResolveAllReferencesShouldLoadDocumentIntoW await wr.FlushAsync(); stream.Position = 0; - var result = await OpenApiDocument.LoadAsync(stream, OpenApiConstants.Yaml, settings: settings); + var result = await OpenApiDocument.LoadAsync(stream, settings: settings); Assert.NotNull(result.OpenApiDocument.Workspace); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs index 3f7c669b0..a9a9e0f7a 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.Collections.Generic; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Exceptions; using Microsoft.OpenApi.Models; @@ -19,7 +20,7 @@ public ParseNodeTests() } [Fact] - public void BrokenSimpleList() + public async Task BrokenSimpleList() { var input = """ @@ -31,7 +32,7 @@ public void BrokenSimpleList() paths: { } """; - var result = OpenApiDocument.Parse(input, "yaml"); + var result = await OpenApiDocument.ParseAsync(input); result.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List() { new OpenApiError(new OpenApiReaderException("Expected a value.")), @@ -40,7 +41,7 @@ public void BrokenSimpleList() } [Fact] - public void BadSchema() + public async Task BadSchema() { var input = """ openapi: 3.0.0 @@ -58,7 +59,7 @@ public void BadSchema() schema: asdasd """; - var res= OpenApiDocument.Parse(input, "yaml"); + var res = await OpenApiDocument.ParseAsync(input); res.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List { diff --git a/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs b/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs index d6fb3b8ba..04577149e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models.References; @@ -22,10 +23,10 @@ public TryLoadReferenceV2Tests() } [Fact] - public void LoadParameterReference() + public async Task LoadParameterReference() { // Arrange - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); var reference = new OpenApiParameterReference("skipParam", result.OpenApiDocument); // Assert @@ -47,9 +48,9 @@ public void LoadParameterReference() } [Fact] - public void LoadSecuritySchemeReference() + public async Task LoadSecuritySchemeReference() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); var reference = new OpenApiSecuritySchemeReference("api_key_sample", result.OpenApiDocument); @@ -65,9 +66,9 @@ public void LoadSecuritySchemeReference() } [Fact] - public void LoadResponseReference() + public async Task LoadResponseReference() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); var reference = new OpenApiResponseReference("NotFound", result.OpenApiDocument); @@ -85,9 +86,9 @@ public void LoadResponseReference() } [Fact] - public void LoadResponseAndSchemaReference() + public async Task LoadResponseAndSchemaReference() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); var reference = new OpenApiResponseReference("GeneralError", result.OpenApiDocument); // Assert diff --git a/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs b/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs index 9d7727aae..37fb1ab2b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.Text.Json.Nodes; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; @@ -14,7 +15,7 @@ namespace Microsoft.OpenApi.Readers.Tests public class TestCustomExtension { [Fact] - public void ParseCustomExtension() + public async Task ParseCustomExtension() { var description = """ @@ -40,7 +41,7 @@ public void ParseCustomExtension() OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader()); var diag = new OpenApiDiagnostic(); - var actual = OpenApiDocument.Parse(description, "yaml", settings: settings); + var actual = await OpenApiDocument.ParseAsync(description, settings: settings); var fooExtension = actual.OpenApiDocument.Info.Extensions["x-foo"] as FooExtension; diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs index b3e30c672..150c4c585 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs @@ -1,7 +1,8 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -18,13 +19,13 @@ public class ComparisonTests [InlineData("minimal")] [InlineData("basic")] //[InlineData("definitions")] //Currently broken due to V3 references not behaving the same as V2 - public void EquivalentV2AndV3DocumentsShouldProduceEquivalentObjects(string fileName) + public async Task EquivalentV2AndV3DocumentsShouldProduceEquivalentObjects(string fileName) { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); using var streamV2 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")); using var streamV3 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")); - var result1 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")); - var result2 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")); + var result1 = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")); + var result2 = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")); result2.OpenApiDocument.Should().BeEquivalentTo(result1.OpenApiDocument, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs index 413d3ee7b..4b31c476c 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -11,7 +12,7 @@ namespace Microsoft.OpenApi.Readers.Tests.V2Tests public class OpenApiContactTests { [Fact] - public void ParseStringContactFragmentShouldSucceed() + public async Task ParseStringContactFragmentShouldSucceed() { var input = """ @@ -23,12 +24,12 @@ public void ParseStringContactFragmentShouldSucceed() """; // Act - var contact = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi2_0, out var diagnostic); + var contact = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi2_0); // Assert - diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); + contact.OpenApiDiagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); - contact.Should().BeEquivalentTo( + contact.Element.Should().BeEquivalentTo( new OpenApiContact { Email = "support@swagger.io", diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs index 596269644..cc456925b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Threading; +using System.Threading.Tasks; using FluentAssertions; using FluentAssertions.Equivalency; using Microsoft.OpenApi.Any; @@ -31,12 +32,12 @@ public OpenApiDocumentTests() // The equivalent of English 1,000.36 in French and Danish is 1.000,36 [InlineData("fr-FR")] [InlineData("da-DK")] - public void ParseDocumentWithDifferentCultureShouldSucceed(string culture) + public async Task ParseDocumentWithDifferentCultureShouldSucceed(string culture) { Thread.CurrentThread.CurrentCulture = new(culture); Thread.CurrentThread.CurrentUICulture = new(culture); - var result = OpenApiDocument.Parse( + var result = await OpenApiDocument.ParseAsync( """ swagger: 2.0 info: @@ -54,8 +55,7 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture) exclusiveMaximum: true exclusiveMinimum: false paths: {} - """, - "yaml"); + """); result.OpenApiDocument.Should().BeEquivalentTo( new OpenApiDocument @@ -109,9 +109,9 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture) } [Fact] - public void ShouldParseProducesInAnyOrder() + public async Task ShouldParseProducesInAnyOrder() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "twoResponses.json")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "twoResponses.json")); var okSchema = new OpenApiSchema { @@ -268,10 +268,10 @@ public void ShouldParseProducesInAnyOrder() } [Fact] - public void ShouldAssignSchemaToAllResponses() + public async Task ShouldAssignSchemaToAllResponses() { using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleProduces.json")); - var result = OpenApiDocument.Load(stream, OpenApiConstants.Json); + var result = await OpenApiDocument.LoadAsync(stream); Assert.Equal(OpenApiSpecVersion.OpenApi2_0, result.OpenApiDiagnostic.SpecificationVersion); @@ -298,13 +298,13 @@ public void ShouldAssignSchemaToAllResponses() } [Fact] - public void ShouldAllowComponentsThatJustContainAReference() + public async Task ShouldAllowComponentsThatJustContainAReference() { // Act - var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "ComponentRootReference.json")).OpenApiDocument; - var schema1 = actual.Components.Schemas["AllPets"]; + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "ComponentRootReference.json")); + var schema1 = actual.OpenApiDocument.Components.Schemas["AllPets"]; Assert.False(schema1.UnresolvedReference); - var schema2 = actual.ResolveReferenceTo(schema1.Reference); + var schema2 = actual.OpenApiDocument.ResolveReferenceTo(schema1.Reference); if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id) { // detected a cycle - this code gets triggered @@ -313,14 +313,14 @@ public void ShouldAllowComponentsThatJustContainAReference() } [Fact] - public void ParseDocumentWithDefaultContentTypeSettingShouldSucceed() + public async Task ParseDocumentWithDefaultContentTypeSettingShouldSucceed() { var settings = new OpenApiReaderSettings { DefaultContentType = ["application/json"] }; - var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"), settings); + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"), settings); var mediaType = actual.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content; Assert.Contains("application/json", mediaType); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs index 2e5779adb..fa964d72f 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -14,7 +15,7 @@ public OpenApiServerTests() } [Fact] - public void NoServer() + public async Task NoServer() { var input = """ @@ -25,13 +26,13 @@ public void NoServer() paths: {} """; - var result = OpenApiDocument.Parse(input, "yaml"); + var result = await OpenApiDocument.ParseAsync(input); Assert.Empty(result.OpenApiDocument.Servers); } [Fact] - public void JustSchemeNoDefault() + public async Task JustSchemeNoDefault() { var input = """ @@ -43,13 +44,13 @@ public void JustSchemeNoDefault() - http paths: {} """; - var result = OpenApiDocument.Parse(input, "yaml"); + var result = await OpenApiDocument.ParseAsync(input); Assert.Empty(result.OpenApiDocument.Servers); } [Fact] - public void JustHostNoDefault() + public async Task JustHostNoDefault() { var input = """ @@ -60,7 +61,7 @@ public void JustHostNoDefault() host: www.foo.com paths: {} """; - var result = OpenApiDocument.Parse(input, "yaml"); + var result = await OpenApiDocument.ParseAsync(input); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -68,7 +69,7 @@ public void JustHostNoDefault() } [Fact] - public void NoBasePath() + public async Task NoBasePath() { var input = """ @@ -86,14 +87,14 @@ public void NoBasePath() BaseUrl = new("https://www.foo.com/spec.yaml") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); Assert.Equal("http://www.foo.com", server.Url); } [Fact] - public void JustBasePathNoDefault() + public async Task JustBasePathNoDefault() { var input = """ @@ -104,7 +105,7 @@ public void JustBasePathNoDefault() basePath: /baz paths: {} """; - var result = OpenApiDocument.Parse(input, "yaml"); + var result = await OpenApiDocument.ParseAsync(input); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -112,7 +113,7 @@ public void JustBasePathNoDefault() } [Fact] - public void JustSchemeWithCustomHost() + public async Task JustSchemeWithCustomHost() { var input = """ @@ -129,7 +130,7 @@ public void JustSchemeWithCustomHost() BaseUrl = new("https://bing.com/foo") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -137,7 +138,7 @@ public void JustSchemeWithCustomHost() } [Fact] - public void JustSchemeWithCustomHostWithEmptyPath() + public async Task JustSchemeWithCustomHostWithEmptyPath() { var input = """ @@ -154,7 +155,7 @@ public void JustSchemeWithCustomHostWithEmptyPath() BaseUrl = new("https://bing.com") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -162,7 +163,7 @@ public void JustSchemeWithCustomHostWithEmptyPath() } [Fact] - public void JustBasePathWithCustomHost() + public async Task JustBasePathWithCustomHost() { var input = """ @@ -178,7 +179,7 @@ public void JustBasePathWithCustomHost() BaseUrl = new("https://bing.com") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -186,7 +187,7 @@ public void JustBasePathWithCustomHost() } [Fact] - public void JustHostWithCustomHost() + public async Task JustHostWithCustomHost() { var input = """ @@ -202,7 +203,7 @@ public void JustHostWithCustomHost() BaseUrl = new("https://bing.com") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -210,7 +211,7 @@ public void JustHostWithCustomHost() } [Fact] - public void JustHostWithCustomHostWithApi() + public async Task JustHostWithCustomHostWithApi() { var input = """ @@ -227,14 +228,14 @@ public void JustHostWithCustomHostWithApi() BaseUrl = new("https://dev.bing.com/api/description.yaml") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); Assert.Equal("https://prod.bing.com", server.Url); } [Fact] - public void MultipleServers() + public async Task MultipleServers() { var input = """ @@ -253,7 +254,7 @@ public void MultipleServers() BaseUrl = new("https://dev.bing.com/api") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Equal(2, result.OpenApiDocument.Servers.Count); Assert.Equal("http://dev.bing.com/api", server.Url); @@ -261,7 +262,7 @@ public void MultipleServers() } [Fact] - public void LocalHostWithCustomHost() + public async Task LocalHostWithCustomHost() { var input = """ @@ -278,7 +279,7 @@ public void LocalHostWithCustomHost() BaseUrl = new("https://bing.com") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); var server = result.OpenApiDocument.Servers.First(); Assert.Single(result.OpenApiDocument.Servers); @@ -286,7 +287,7 @@ public void LocalHostWithCustomHost() } [Fact] - public void InvalidHostShouldYieldError() + public async Task InvalidHostShouldYieldError() { var input = """ @@ -303,7 +304,7 @@ public void InvalidHostShouldYieldError() BaseUrl = new("https://bing.com") }; - var result = OpenApiDocument.Parse(input, "yaml", settings); + var result = await OpenApiDocument.ParseAsync(input, settings); result.OpenApiDocument.Servers.Count.Should().Be(0); result.OpenApiDiagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs index 638d69667..f2fea5388 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs @@ -26,10 +26,10 @@ public OpenApiDocumentTests() } [Fact] - public void ParseDocumentWithWebhooksShouldSucceed() + public async Task ParseDocumentWithWebhooksShouldSucceed() { // Arrange and Act - var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "documentWithWebhooks.yaml")); + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "documentWithWebhooks.yaml")); var petSchema = new OpenApiSchemaReference("petSchema", actual.OpenApiDocument); var newPetSchema = new OpenApiSchemaReference("newPetSchema", actual.OpenApiDocument); @@ -205,10 +205,10 @@ public void ParseDocumentWithWebhooksShouldSucceed() } [Fact] - public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() + public async Task ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { // Arrange && Act - var actual = OpenApiDocument.Load("V31Tests/Samples/OpenApiDocument/documentWithReusablePaths.yaml"); + var actual = await OpenApiDocument.LoadAsync("V31Tests/Samples/OpenApiDocument/documentWithReusablePaths.yaml"); var components = new OpenApiComponents { @@ -401,14 +401,14 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() } [Fact] - public void ParseDocumentWithExampleInSchemaShouldSucceed() + public async Task ParseDocumentWithExampleInSchemaShouldSucceed() { // Arrange var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false }); // Act - var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithExample.yaml")); + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExample.yaml")); actual.OpenApiDocument.SerializeAsV31(writer); // Assert @@ -416,10 +416,10 @@ public void ParseDocumentWithExampleInSchemaShouldSucceed() } [Fact] - public void ParseDocumentWithPatternPropertiesInSchemaWorks() + public async Task ParseDocumentWithPatternPropertiesInSchemaWorks() { // Arrange and Act - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml")); var actualSchema = result.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; var expectedSchema = new OpenApiSchema @@ -473,10 +473,10 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks() } [Fact] - public void ParseDocumentWithReferenceByIdGetsResolved() + public async Task ParseDocumentWithReferenceByIdGetsResolved() { // Arrange and Act - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithReferenceById.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithReferenceById.yaml")); var responseSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Post].RequestBody.Content["application/json"].Schema; @@ -523,10 +523,10 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks() // Act var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalRefById.yaml"), settings); - var doc2 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "externalResource.yaml")).OpenApiDocument; + var result2 = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalResource.yaml")); var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters.First().Schema; - result.OpenApiDocument.Workspace.RegisterComponents(doc2); + result.OpenApiDocument.Workspace.RegisterComponents(result2.OpenApiDocument); // Assert requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved @@ -536,9 +536,9 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks() public async Task ParseDocumentWith31PropertiesWorks() { var path = Path.Combine(SampleFolderPath, "documentWith31Properties.yaml"); - var doc = OpenApiDocument.Load(path).OpenApiDocument; + var res = await OpenApiDocument.LoadAsync(path); var outputStringWriter = new StringWriter(); - doc.SerializeAsV31(new OpenApiYamlWriter(outputStringWriter)); + res.OpenApiDocument.SerializeAsV31(new OpenApiYamlWriter(outputStringWriter)); outputStringWriter.Flush(); var actual = outputStringWriter.GetStringBuilder().ToString(); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs index 967bb0f3e..577429d03 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Text.Json.Nodes; +using System.Threading.Tasks; using FluentAssertions; using FluentAssertions.Equivalency; using Microsoft.OpenApi.Models; @@ -24,7 +25,7 @@ public OpenApiSchemaTests() } [Fact] - public void ParseBasicV31SchemaShouldSucceed() + public async Task ParseBasicV31SchemaShouldSucceed() { var expectedObject = new OpenApiSchema() { @@ -75,15 +76,15 @@ public void ParseBasicV31SchemaShouldSucceed() }; // Act - var schema = OpenApiModelFactory.Load( - System.IO.Path.Combine(SampleFolderPath, "jsonSchema.json"), OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync( + System.IO.Path.Combine(SampleFolderPath, "jsonSchema.json"), OpenApiSpecVersion.OpenApi3_1); // Assert - schema.Should().BeEquivalentTo(expectedObject); + schema.Element.Should().BeEquivalentTo(expectedObject); } [Fact] - public void ParseSchemaWithTypeArrayWorks() + public async Task ParseSchemaWithTypeArrayWorks() { // Arrange var schema = @"{ @@ -102,10 +103,10 @@ public void ParseSchemaWithTypeArrayWorks() }; // Act - var actual = OpenApiModelFactory.Parse(schema, OpenApiSpecVersion.OpenApi3_1, out _); + var actual = await OpenApiModelFactory.ParseAsync(schema, OpenApiSpecVersion.OpenApi3_1); // Assert - actual.Should().BeEquivalentTo(expected); + actual.Element.Should().BeEquivalentTo(expected); } [Fact] @@ -146,12 +147,12 @@ public void TestSchemaCopyConstructorWithTypeArrayWorks() } [Fact] - public void ParseV31SchemaShouldSucceed() + public async Task ParseV31SchemaShouldSucceed() { var path = Path.Combine(SampleFolderPath, "schema.yaml"); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); var expectedSchema = new OpenApiSchema { Type = JsonSchemaType.Object, @@ -166,15 +167,15 @@ public void ParseV31SchemaShouldSucceed() }; // Assert - schema.Should().BeEquivalentTo(expectedSchema); + schema.Element.Should().BeEquivalentTo(expectedSchema); } [Fact] - public void ParseAdvancedV31SchemaShouldSucceed() + public async Task ParseAdvancedV31SchemaShouldSucceed() { // Arrange and Act var path = Path.Combine(SampleFolderPath, "advancedSchema.yaml"); - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); var expectedSchema = new OpenApiSchema { @@ -248,14 +249,14 @@ public void ParseAdvancedV31SchemaShouldSucceed() }; // Assert - schema.Should().BeEquivalentTo(expectedSchema, options => options + schema.Element.Should().BeEquivalentTo(expectedSchema, options => options .IgnoringCyclicReferences() .Excluding((IMemberInfo memberInfo) => memberInfo.Path.EndsWith("Parent"))); } [Fact] - public void ParseSchemaWithExamplesShouldSucceed() + public async Task ParseSchemaWithExamplesShouldSucceed() { // Arrange var input = @" @@ -265,10 +266,10 @@ public void ParseSchemaWithExamplesShouldSucceed() - ubuntu "; // Act - var schema = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_1, out _, "yaml"); + var result = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi3_1); // Assert - schema.Examples.Should().HaveCount(2); + result.Element.Examples.Should().HaveCount(2); } [Fact] @@ -295,7 +296,7 @@ public void CloningSchemaWithExamplesAndEnumsShouldSucceed() } [Fact] - public void SerializeV31SchemaWithMultipleTypesAsV3Works() + public async Task SerializeV31SchemaWithMultipleTypesAsV3Works() { // Arrange var expected = @"type: string @@ -304,17 +305,17 @@ public void SerializeV31SchemaWithMultipleTypesAsV3Works() var path = Path.Combine(SampleFolderPath, "schemaWithTypeArray.yaml"); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); var writer = new StringWriter(); - schema.SerializeAsV3(new OpenApiYamlWriter(writer)); + schema.Element.SerializeAsV3(new OpenApiYamlWriter(writer)); var schema1String = writer.ToString(); schema1String.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); } [Fact] - public void SerializeV31SchemaWithMultipleTypesAsV2Works() + public async Task SerializeV31SchemaWithMultipleTypesAsV2Works() { // Arrange var expected = @"type: string @@ -323,17 +324,17 @@ public void SerializeV31SchemaWithMultipleTypesAsV2Works() var path = Path.Combine(SampleFolderPath, "schemaWithTypeArray.yaml"); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); var writer = new StringWriter(); - schema.SerializeAsV2(new OpenApiYamlWriter(writer)); + schema.Element.SerializeAsV2(new OpenApiYamlWriter(writer)); var schema1String = writer.ToString(); schema1String.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); } [Fact] - public void SerializeV3SchemaWithNullableAsV31Works() + public async Task SerializeV3SchemaWithNullableAsV31Works() { // Arrange var expected = @"type: @@ -343,17 +344,17 @@ public void SerializeV3SchemaWithNullableAsV31Works() var path = Path.Combine(SampleFolderPath, "schemaWithNullable.yaml"); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_0, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_0); var writer = new StringWriter(); - schema.SerializeAsV31(new OpenApiYamlWriter(writer)); + schema.Element.SerializeAsV31(new OpenApiYamlWriter(writer)); var schemaString = writer.ToString(); schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); } [Fact] - public void SerializeV2SchemaWithNullableExtensionAsV31Works() + public async Task SerializeV2SchemaWithNullableExtensionAsV31Works() { // Arrange var expected = @"type: @@ -364,17 +365,17 @@ public void SerializeV2SchemaWithNullableExtensionAsV31Works() var path = Path.Combine(SampleFolderPath, "schemaWithNullableExtension.yaml"); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi2_0, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi2_0); var writer = new StringWriter(); - schema.SerializeAsV31(new OpenApiYamlWriter(writer)); + schema.Element.SerializeAsV31(new OpenApiYamlWriter(writer)); var schemaString = writer.ToString(); schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); } [Fact] - public void SerializeSchemaWithTypeArrayAndNullableDoesntEmitType() + public async Task SerializeSchemaWithTypeArrayAndNullableDoesntEmitType() { var input = @"type: - ""string"" @@ -383,10 +384,10 @@ public void SerializeSchemaWithTypeArrayAndNullableDoesntEmitType() var expected = @"{ }"; - var schema = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_1, out _, "yaml"); + var schema = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi3_1); var writer = new StringWriter(); - schema.SerializeAsV2(new OpenApiYamlWriter(writer)); + schema.Element.SerializeAsV2(new OpenApiYamlWriter(writer)); var schemaString = writer.ToString(); schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); @@ -395,20 +396,20 @@ public void SerializeSchemaWithTypeArrayAndNullableDoesntEmitType() [Theory] [InlineData("schemaWithNullable.yaml")] [InlineData("schemaWithNullableExtension.yaml")] - public void LoadSchemaWithNullableExtensionAsV31Works(string filePath) + public async Task LoadSchemaWithNullableExtensionAsV31Works(string filePath) { // Arrange var path = Path.Combine(SampleFolderPath, filePath); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); // Assert - schema.Type.Should().Be(JsonSchemaType.String | JsonSchemaType.Null); + schema.Element.Type.Should().Be(JsonSchemaType.String | JsonSchemaType.Null); } [Fact] - public void SerializeSchemaWithJsonSchemaKeywordsWorks() + public async Task SerializeSchemaWithJsonSchemaKeywordsWorks() { // Arrange var expected = @"$id: https://example.com/schemas/person.schema.yaml @@ -441,15 +442,15 @@ public void SerializeSchemaWithJsonSchemaKeywordsWorks() var path = Path.Combine(SampleFolderPath, "schemaWithJsonSchemaKeywords.yaml"); // Act - var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); // serialization var writer = new StringWriter(); - schema.SerializeAsV31(new OpenApiYamlWriter(writer)); + schema.Element.SerializeAsV31(new OpenApiYamlWriter(writer)); var schemaString = writer.ToString(); // Assert - schema.Vocabulary.Keys.Count.Should().Be(5); + schema.Element.Vocabulary.Keys.Count.Should().Be(5); schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs index cab621c14..cc48f0822 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Expressions; using Microsoft.OpenApi.Models; @@ -21,15 +22,15 @@ public OpenApiCallbackTests() } [Fact] - public void ParseBasicCallbackShouldSucceed() + public async Task ParseBasicCallbackShouldSucceed() { // Act - var callback = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "basicCallback.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic); + var callback = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "basicCallback.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); + callback.OpenApiDiagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); - callback.Should().BeEquivalentTo( + callback.Element.Should().BeEquivalentTo( new OpenApiCallback { PathItems = @@ -64,12 +65,12 @@ public void ParseBasicCallbackShouldSucceed() } [Fact] - public void ParseCallbackWithReferenceShouldSucceed() + public async Task ParseCallbackWithReferenceShouldSucceed() { using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "callbackWithReference.yaml")); // Act - var result = OpenApiModelFactory.Load(stream, OpenApiConstants.Yaml); + var result = await OpenApiModelFactory.LoadAsync(stream); // Assert var path = result.OpenApiDocument.Paths.First().Value; @@ -122,10 +123,10 @@ public void ParseCallbackWithReferenceShouldSucceed() } [Fact] - public void ParseMultipleCallbacksWithReferenceShouldSucceed() + public async Task ParseMultipleCallbacksWithReferenceShouldSucceed() { // Act - var result = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "multipleCallbacksWithReference.yaml")); + var result = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "multipleCallbacksWithReference.yaml")); // Assert var path = result.OpenApiDocument.Paths.First().Value; diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs index d6d0422c4..a18bf09d1 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -11,7 +12,7 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests public class OpenApiContactTests { [Fact] - public void ParseStringContactFragmentShouldSucceed() + public async Task ParseStringContactFragmentShouldSucceed() { var input = """ @@ -23,12 +24,12 @@ public void ParseStringContactFragmentShouldSucceed() """; // Act - var contact = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic, OpenApiConstants.Json); + var contact = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi3_0); // Assert - diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); + contact.OpenApiDiagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); - contact.Should().BeEquivalentTo( + contact.Element.Should().BeEquivalentTo( new OpenApiContact { Email = "support@swagger.io", diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs index 6556ade48..0ec4fddec 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -20,16 +21,16 @@ public OpenApiDiscriminatorTests() } [Fact] - public void ParseBasicDiscriminatorShouldSucceed() + public async Task ParseBasicDiscriminatorShouldSucceed() { // Arrange using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml")); // Act - var discriminator = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Yaml, out var diagnostic); + var discriminator = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - discriminator.Should().BeEquivalentTo( + discriminator.Element.Should().BeEquivalentTo( new OpenApiDiscriminator { PropertyName = "pet_type", diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs index 1382d0248..9c88f77aa 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.IO; using System.Linq; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Interfaces; @@ -33,59 +34,51 @@ public OpenApiDocumentTests() public T Clone(T element) where T : IOpenApiSerializable { - using (var stream = new MemoryStream()) + using var stream = new MemoryStream(); + IOpenApiWriter writer; + var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); + writer = new OpenApiJsonWriter(streamWriter, new OpenApiJsonWriterSettings() { - IOpenApiWriter writer; - var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); - writer = new OpenApiJsonWriter(streamWriter, new OpenApiJsonWriterSettings() - { - InlineLocalReferences = true - }); - element.SerializeAsV3(writer); - writer.Flush(); - stream.Position = 0; - - using (var streamReader = new StreamReader(stream)) - { - var result = streamReader.ReadToEnd(); - return OpenApiModelFactory.Parse(result, OpenApiSpecVersion.OpenApi3_0, out OpenApiDiagnostic diagnostic4); - } - } + InlineLocalReferences = true + }); + element.SerializeAsV3(writer); + writer.Flush(); + stream.Position = 0; + + using var streamReader = new StreamReader(stream); + var result = streamReader.ReadToEnd(); + var res = OpenApiModelFactory.ParseAsync(result, OpenApiSpecVersion.OpenApi3_0).GetAwaiter().GetResult(); + return (T)res.Element; } public OpenApiSecurityScheme CloneSecurityScheme(OpenApiSecurityScheme element) { - using (var stream = new MemoryStream()) + using var stream = new MemoryStream(); + IOpenApiWriter writer; + var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); + writer = new OpenApiJsonWriter(streamWriter, new OpenApiJsonWriterSettings() { - IOpenApiWriter writer; - var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); - writer = new OpenApiJsonWriter(streamWriter, new OpenApiJsonWriterSettings() - { - InlineLocalReferences = true - }); - element.SerializeAsV3(writer); - writer.Flush(); - stream.Position = 0; + InlineLocalReferences = true + }); + element.SerializeAsV3(writer); + writer.Flush(); + stream.Position = 0; - using (var streamReader = new StreamReader(stream)) - { - var result = streamReader.ReadToEnd(); - return OpenApiModelFactory.Parse(result, OpenApiSpecVersion.OpenApi3_0, out OpenApiDiagnostic diagnostic4); - } - } + using var streamReader = new StreamReader(stream); + var result = streamReader.ReadToEnd(); + return (OpenApiSecurityScheme)OpenApiModelFactory.ParseAsync(result, OpenApiSpecVersion.OpenApi3_0).Result.Element; } [Fact] - public void ParseDocumentFromInlineStringShouldSucceed() + public async Task ParseDocumentFromInlineStringShouldSucceed() { - var result = OpenApiDocument.Parse( + var result = await OpenApiDocument.ParseAsync( @" openapi : 3.0.0 info: title: Simple Document version: 0.9.1 -paths: {}", - OpenApiConstants.Yaml); +paths: {}"); result.OpenApiDocument.Should().BeEquivalentTo( new OpenApiDocument @@ -110,10 +103,10 @@ public void ParseDocumentFromInlineStringShouldSucceed() } [Fact] - public void ParseBasicDocumentWithMultipleServersShouldSucceed() + public async Task ParseBasicDocumentWithMultipleServersShouldSucceed() { var path = System.IO.Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml"); - var result = OpenApiDocument.Load(path); + var result = await OpenApiDocument.LoadAsync(path); result.OpenApiDiagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() @@ -150,10 +143,10 @@ public void ParseBasicDocumentWithMultipleServersShouldSucceed() }, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); } [Fact] - public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic() + public async Task ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic() { using var stream = Resources.GetStream(System.IO.Path.Combine(SampleFolderPath, "brokenMinimalDocument.yaml")); - var result = OpenApiDocument.Load(stream, OpenApiConstants.Yaml); + var result = await OpenApiDocument.LoadAsync(stream); result.OpenApiDocument.Should().BeEquivalentTo( new OpenApiDocument @@ -178,9 +171,9 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic() } [Fact] - public void ParseMinimalDocumentShouldSucceed() + public async Task ParseMinimalDocumentShouldSucceed() { - var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "minimalDocument.yaml")); + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "minimalDocument.yaml")); result.OpenApiDocument.Should().BeEquivalentTo( new OpenApiDocument @@ -205,10 +198,10 @@ public void ParseMinimalDocumentShouldSucceed() } [Fact] - public void ParseStandardPetStoreDocumentShouldSucceed() + public async Task ParseStandardPetStoreDocumentShouldSucceed() { using var stream = Resources.GetStream(System.IO.Path.Combine(SampleFolderPath, "petStore.yaml")); - var actual = OpenApiDocument.Load(stream, OpenApiConstants.Yaml); + var actual = await OpenApiDocument.LoadAsync(stream); var components = new OpenApiComponents { @@ -591,10 +584,10 @@ public void ParseStandardPetStoreDocumentShouldSucceed() } [Fact] - public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() + public async Task ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { using var stream = Resources.GetStream(System.IO.Path.Combine(SampleFolderPath, "petStoreWithTagAndSecurity.yaml")); - var actual = OpenApiDocument.Load(stream, OpenApiConstants.Yaml); + var actual = await OpenApiDocument.LoadAsync(stream); var components = new OpenApiComponents { @@ -1103,9 +1096,9 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() } [Fact] - public void ParsePetStoreExpandedShouldSucceed() + public async Task ParsePetStoreExpandedShouldSucceed() { - var actual = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "petStoreExpanded.yaml")); + var actual = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "petStoreExpanded.yaml")); // TODO: Create the object in memory and compare with the one read from YAML file. @@ -1114,9 +1107,9 @@ public void ParsePetStoreExpandedShouldSucceed() } [Fact] - public void GlobalSecurityRequirementShouldReferenceSecurityScheme() + public async Task GlobalSecurityRequirementShouldReferenceSecurityScheme() { - var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "securedApi.yaml")); + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "securedApi.yaml")); var securityRequirement = result.OpenApiDocument.SecurityRequirements.First(); @@ -1125,9 +1118,9 @@ public void GlobalSecurityRequirementShouldReferenceSecurityScheme() } [Fact] - public void HeaderParameterShouldAllowExample() + public async Task HeaderParameterShouldAllowExample() { - var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "apiWithFullHeaderComponent.yaml")); + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "apiWithFullHeaderComponent.yaml")); var exampleHeader = result.OpenApiDocument.Components?.Headers?["example-header"]; Assert.NotNull(exampleHeader); @@ -1187,7 +1180,7 @@ public void HeaderParameterShouldAllowExample() } [Fact] - public void ParseDocumentWithReferencedSecuritySchemeWorks() + public async Task ParseDocumentWithReferencedSecuritySchemeWorks() { // Act var settings = new OpenApiReaderSettings @@ -1195,7 +1188,7 @@ public void ParseDocumentWithReferencedSecuritySchemeWorks() ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences }; - var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "docWithSecuritySchemeReference.yaml"), settings); + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "docWithSecuritySchemeReference.yaml"), settings); var securityScheme = result.OpenApiDocument.Components.SecuritySchemes["OAuth2"]; // Assert @@ -1204,7 +1197,7 @@ public void ParseDocumentWithReferencedSecuritySchemeWorks() } [Fact] - public void ParseDocumentWithJsonSchemaReferencesWorks() + public async Task ParseDocumentWithJsonSchemaReferencesWorks() { // Arrange using var stream = Resources.GetStream(System.IO.Path.Combine(SampleFolderPath, "docWithJsonSchema.yaml")); @@ -1214,7 +1207,7 @@ public void ParseDocumentWithJsonSchemaReferencesWorks() { ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences }; - var result = OpenApiDocument.Load(stream, OpenApiConstants.Yaml, settings); + var result = await OpenApiDocument.LoadAsync(stream, settings); var actualSchema = result.OpenApiDocument.Paths["/users/{userId}"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; @@ -1224,10 +1217,10 @@ public void ParseDocumentWithJsonSchemaReferencesWorks() } [Fact] - public void ValidateExampleShouldNotHaveDataTypeMismatch() + public async Task ValidateExampleShouldNotHaveDataTypeMismatch() { // Act - var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "documentWithDateExampleInSchema.yaml"), new OpenApiReaderSettings + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "documentWithDateExampleInSchema.yaml"), new OpenApiReaderSettings { ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences @@ -1239,7 +1232,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatch() } [Fact] - public void ParseDocWithRefsUsingProxyReferencesSucceeds() + public async Task ParseDocWithRefsUsingProxyReferencesSucceeds() { // Arrange var expected = new OpenApiDocument @@ -1330,7 +1323,8 @@ public void ParseDocWithRefsUsingProxyReferencesSucceeds() using var stream = Resources.GetStream(System.IO.Path.Combine(SampleFolderPath, "minifiedPetStore.yaml")); // Act - var doc = OpenApiDocument.Load(stream, "yaml").OpenApiDocument; + var result = await OpenApiDocument.LoadAsync(stream); + var doc = result.OpenApiDocument; var actualParam = doc.Paths["/pets"].Operations[OperationType.Get].Parameters.First(); var outputDoc = doc.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0).MakeLineBreaksEnvironmentNeutral(); var output = actualParam.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); @@ -1346,9 +1340,9 @@ public void ParseDocWithRefsUsingProxyReferencesSucceeds() } [Fact] - public void ParseBasicDocumentWithServerVariableShouldSucceed() + public async Task ParseBasicDocumentWithServerVariableShouldSucceed() { - var result = OpenApiDocument.Parse(""" + var result = await OpenApiDocument.ParseAsync(""" openapi : 3.0.0 info: title: The API @@ -1361,7 +1355,7 @@ public void ParseBasicDocumentWithServerVariableShouldSucceed() default: v2 enum: [v1, v2] paths: {} - """, "yaml"); + """); var expected = new OpenApiDocument { @@ -1399,9 +1393,9 @@ public void ParseBasicDocumentWithServerVariableShouldSucceed() } [Fact] - public void ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail() + public async Task ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail() { - var result = OpenApiDocument.Parse(""" + var result = await OpenApiDocument.ParseAsync(""" openapi : 3.0.0 info: title: The API @@ -1413,7 +1407,7 @@ public void ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail() version: enum: [v1, v2] paths: {} - """, "yaml"); + """); result.OpenApiDiagnostic.Errors.Should().NotBeEmpty(); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs index eaf802d8c..0012f88c4 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -20,13 +21,13 @@ public OpenApiEncodingTests() } [Fact] - public void ParseBasicEncodingShouldSucceed() + public async Task ParseBasicEncodingShouldSucceed() { // Act - var encoding = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "basicEncoding.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var encoding = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "basicEncoding.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - encoding.Should().BeEquivalentTo( + encoding.Element.Should().BeEquivalentTo( new OpenApiEncoding { ContentType = "application/xml; charset=utf-8" @@ -34,15 +35,15 @@ public void ParseBasicEncodingShouldSucceed() } [Fact] - public void ParseAdvancedEncodingShouldSucceed() + public async Task ParseAdvancedEncodingShouldSucceed() { using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedEncoding.yaml")); // Act - var encoding = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Yaml, out _); + var encoding = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - encoding.Should().BeEquivalentTo( + encoding.Element.Should().BeEquivalentTo( new OpenApiEncoding { ContentType = "image/png, image/jpeg", diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs index 84f028f6b..aaf542624 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs @@ -3,8 +3,8 @@ using System.IO; using System.Text.Json.Nodes; +using System.Threading.Tasks; using FluentAssertions; -using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; using Xunit; @@ -22,9 +22,9 @@ public OpenApiExampleTests() } [Fact] - public void ParseAdvancedExampleShouldSucceed() + public async Task ParseAdvancedExampleShouldSucceed() { - var example = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "advancedExample.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic); + var result = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "advancedExample.yaml"), OpenApiSpecVersion.OpenApi3_0); var expected = new OpenApiExample { Value = new JsonObject @@ -62,12 +62,12 @@ public void ParseAdvancedExampleShouldSucceed() } }; - var actualRoot = example.Value["versions"][0]["status"].Root; + var actualRoot = result.Element.Value["versions"][0]["status"].Root; var expectedRoot = expected.Value["versions"][0]["status"].Root; - diagnostic.Errors.Should().BeEmpty(); + result.OpenApiDiagnostic.Errors.Should().BeEmpty(); - example.Should().BeEquivalentTo(expected, options => options.IgnoringCyclicReferences() + result.Element.Should().BeEquivalentTo(expected, options => options.IgnoringCyclicReferences() .Excluding(e => e.Value["versions"][0]["status"].Root) .Excluding(e => e.Value["versions"][0]["id"].Root) .Excluding(e => e.Value["versions"][0]["links"][0]["href"].Root) @@ -79,9 +79,9 @@ public void ParseAdvancedExampleShouldSucceed() } [Fact] - public void ParseExampleForcedStringSucceed() + public async Task ParseExampleForcedStringSucceed() { - var result= OpenApiDocument.Load(Path.Combine(SampleFolderPath, "explicitString.yaml")); + var result= await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "explicitString.yaml")); result.OpenApiDiagnostic.Errors.Should().BeEmpty(); } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiInfoTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiInfoTests.cs index 2fa75cf60..69fdfe750 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiInfoTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiInfoTests.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Text.Json.Nodes; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; @@ -23,13 +24,13 @@ public OpenApiInfoTests() } [Fact] - public void ParseAdvancedInfoShouldSucceed() + public async Task ParseAdvancedInfoShouldSucceed() { // Act - var openApiInfo = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "advancedInfo.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic); + var openApiInfo = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "advancedInfo.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - openApiInfo.Should().BeEquivalentTo( + openApiInfo.Element.Should().BeEquivalentTo( new OpenApiInfo { Title = "Advanced Info", @@ -80,13 +81,13 @@ public void ParseAdvancedInfoShouldSucceed() } [Fact] - public void ParseBasicInfoShouldSucceed() + public async Task ParseBasicInfoShouldSucceed() { // Act - var openApiInfo = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "basicInfo.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var openApiInfo = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "basicInfo.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - openApiInfo.Should().BeEquivalentTo( + openApiInfo.Element.Should().BeEquivalentTo( new OpenApiInfo { Title = "Basic Info", @@ -108,15 +109,15 @@ public void ParseBasicInfoShouldSucceed() } [Fact] - public void ParseMinimalInfoShouldSucceed() + public async Task ParseMinimalInfoShouldSucceed() { using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalInfo.yaml")); // Act - var openApiInfo = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, "yaml", out _); + var openApiInfo = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - openApiInfo.Should().BeEquivalentTo( + openApiInfo.Element.Should().BeEquivalentTo( new OpenApiInfo { Title = "Minimal Info", diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs index 26de35edb..92fd58d96 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; @@ -24,13 +25,13 @@ public OpenApiMediaTypeTests() } [Fact] - public void ParseMediaTypeWithExampleShouldSucceed() + public async Task ParseMediaTypeWithExampleShouldSucceed() { // Act - var mediaType = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "mediaTypeWithExample.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic); + var mediaType = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "mediaTypeWithExample.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - mediaType.Should().BeEquivalentTo( + mediaType.Element.Should().BeEquivalentTo( new OpenApiMediaType { Example = 5, @@ -45,13 +46,13 @@ public void ParseMediaTypeWithExampleShouldSucceed() } [Fact] - public void ParseMediaTypeWithExamplesShouldSucceed() + public async Task ParseMediaTypeWithExamplesShouldSucceed() { // Act - var mediaType = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "mediaTypeWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic); + var mediaType = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "mediaTypeWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - mediaType.Should().BeEquivalentTo( + mediaType.Element.Should().BeEquivalentTo( new OpenApiMediaType { Examples = diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs index 9ba96bbda..90f37154c 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models.References; @@ -21,9 +22,9 @@ public OpenApiOperationTests() } [Fact] - public void OperationWithSecurityRequirementShouldReferenceSecurityScheme() + public async Task OperationWithSecurityRequirementShouldReferenceSecurityScheme() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "securedOperation.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "securedOperation.yaml")); var securityScheme = result.OpenApiDocument.Paths["/"].Operations[OperationType.Get].Security.First().Keys.First(); @@ -32,10 +33,10 @@ public void OperationWithSecurityRequirementShouldReferenceSecurityScheme() } [Fact] - public void ParseOperationWithParameterWithNoLocationShouldSucceed() + public async Task ParseOperationWithParameterWithNoLocationShouldSucceed() { // Act - var operation = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "operationWithParameterWithNoLocation.json"), OpenApiSpecVersion.OpenApi3_0, out _); + var operation = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "operationWithParameterWithNoLocation.json"), OpenApiSpecVersion.OpenApi3_0); var expectedOp = new OpenApiOperation { Tags = @@ -72,7 +73,7 @@ public void ParseOperationWithParameterWithNoLocationShouldSucceed() }; // Assert - expectedOp.Should().BeEquivalentTo(operation, + expectedOp.Should().BeEquivalentTo(operation.Element, options => options.Excluding(x => x.Tags[0].Reference.HostDocument) .Excluding(x => x.Tags[0].Extensions)); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs index e0f6460aa..974236a13 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs @@ -11,6 +11,7 @@ using Xunit; using Microsoft.OpenApi.Reader.V3; using Microsoft.OpenApi.Services; +using System.Threading.Tasks; namespace Microsoft.OpenApi.Readers.Tests.V3Tests { @@ -25,16 +26,16 @@ public OpenApiParameterTests() } [Fact] - public void ParsePathParameterShouldSucceed() + public async Task ParsePathParameterShouldSucceed() { // Arrange using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "pathParameter.yaml")); // Act - var parameter = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, "yaml", out _); + var parameter = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = ParameterLocation.Path, @@ -49,13 +50,13 @@ public void ParsePathParameterShouldSucceed() } [Fact] - public void ParseQueryParameterShouldSucceed() + public async Task ParseQueryParameterShouldSucceed() { // Act - var parameter = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "queryParameter.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var parameter = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "queryParameter.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = ParameterLocation.Query, @@ -76,13 +77,13 @@ public void ParseQueryParameterShouldSucceed() } [Fact] - public void ParseQueryParameterWithObjectTypeShouldSucceed() + public async Task ParseQueryParameterWithObjectTypeShouldSucceed() { // Act - var parameter = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "queryParameterWithObjectType.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var parameter = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "queryParameterWithObjectType.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = ParameterLocation.Query, @@ -100,16 +101,16 @@ public void ParseQueryParameterWithObjectTypeShouldSucceed() } [Fact] - public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed() + public async Task ParseQueryParameterWithObjectTypeAndContentShouldSucceed() { // Arrange using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameterWithObjectTypeAndContent.yaml")); // Act - var parameter = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, "yaml", out _); + var parameter = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = ParameterLocation.Query, @@ -144,13 +145,13 @@ public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed() } [Fact] - public void ParseHeaderParameterShouldSucceed() + public async Task ParseHeaderParameterShouldSucceed() { // Act - var parameter = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "headerParameter.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var parameter = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "headerParameter.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = ParameterLocation.Header, @@ -172,13 +173,13 @@ public void ParseHeaderParameterShouldSucceed() } [Fact] - public void ParseParameterWithNullLocationShouldSucceed() + public async Task ParseParameterWithNullLocationShouldSucceed() { // Act - var parameter = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "parameterWithNullLocation.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var parameter = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "parameterWithNullLocation.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = null, @@ -193,16 +194,16 @@ public void ParseParameterWithNullLocationShouldSucceed() } [Fact] - public void ParseParameterWithNoLocationShouldSucceed() + public async Task ParseParameterWithNoLocationShouldSucceed() { // Arrange using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithNoLocation.yaml")); // Act - var parameter = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, "yaml", out _); + var parameter = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = null, @@ -217,16 +218,16 @@ public void ParseParameterWithNoLocationShouldSucceed() } [Fact] - public void ParseParameterWithUnknownLocationShouldSucceed() + public async Task ParseParameterWithUnknownLocationShouldSucceed() { // Arrange using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithUnknownLocation.yaml")); // Act - var parameter = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, "yaml", out _); + var parameter = await OpenApiModelFactory.LoadAsync(stream, OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = null, @@ -241,13 +242,13 @@ public void ParseParameterWithUnknownLocationShouldSucceed() } [Fact] - public void ParseParameterWithExampleShouldSucceed() + public async Task ParseParameterWithExampleShouldSucceed() { // Act - var parameter = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "parameterWithExample.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var parameter = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "parameterWithExample.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = null, @@ -264,13 +265,13 @@ public void ParseParameterWithExampleShouldSucceed() } [Fact] - public void ParseParameterWithExamplesShouldSucceed() + public async Task ParseParameterWithExamplesShouldSucceed() { // Act - var parameter = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "parameterWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var parameter = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "parameterWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - parameter.Should().BeEquivalentTo( + parameter.Element.Should().BeEquivalentTo( new OpenApiParameter { In = null, diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiResponseTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiResponseTests.cs index 09a1d00a1..4f798ad39 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiResponseTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiResponseTests.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -21,9 +22,9 @@ public OpenApiResponseTests() } [Fact] - public void ResponseWithReferencedHeaderShouldReferenceComponent() + public async Task ResponseWithReferencedHeaderShouldReferenceComponent() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "responseWithHeaderReference.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "responseWithHeaderReference.yaml")); var response = result.OpenApiDocument.Components.Responses["Test"]; var expected = response.Headers.First().Value; diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs index 81cb4376b..2501fbcad 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs @@ -16,6 +16,7 @@ using Microsoft.OpenApi.Reader.V3; using FluentAssertions.Equivalency; using Microsoft.OpenApi.Models.References; +using System.Threading.Tasks; namespace Microsoft.OpenApi.Readers.Tests.V3Tests { @@ -58,7 +59,7 @@ public void ParsePrimitiveSchemaShouldSucceed() } [Fact] - public void ParseExampleStringFragmentShouldSucceed() + public async Task ParseExampleStringFragmentShouldSucceed() { var input = @" { @@ -68,12 +69,12 @@ public void ParseExampleStringFragmentShouldSucceed() var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi3_0); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); - openApiAny.Should().BeEquivalentTo(new OpenApiAny( + openApiAny.Element.Should().BeEquivalentTo(new OpenApiAny( new JsonObject { ["foo"] = "bar", @@ -82,7 +83,7 @@ public void ParseExampleStringFragmentShouldSucceed() } [Fact] - public void ParseEnumFragmentShouldSucceed() + public async Task ParseEnumFragmentShouldSucceed() { var input = @" [ @@ -92,12 +93,12 @@ public void ParseEnumFragmentShouldSucceed() var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi3_0); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); - openApiAny.Should().BeEquivalentTo(new OpenApiAny( + openApiAny.Element.Should().BeEquivalentTo(new OpenApiAny( new JsonArray { "foo", @@ -106,7 +107,7 @@ public void ParseEnumFragmentShouldSucceed() } [Fact] - public void ParsePathFragmentShouldSucceed() + public async Task ParsePathFragmentShouldSucceed() { var input = @" summary: externally referenced path item @@ -118,12 +119,12 @@ public void ParsePathFragmentShouldSucceed() var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic, "yaml"); + var openApiAny = await OpenApiModelFactory.ParseAsync(input, OpenApiSpecVersion.OpenApi3_0); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); - openApiAny.Should().BeEquivalentTo( + openApiAny.Element.Should().BeEquivalentTo( new OpenApiPathItem { Summary = "externally referenced path item", @@ -230,10 +231,10 @@ public void ParseBasicSchemaWithExampleShouldSucceed() } [Fact] - public void ParseBasicSchemaWithReferenceShouldSucceed() + public async Task ParseBasicSchemaWithReferenceShouldSucceed() { // Act - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "basicSchemaWithReference.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "basicSchemaWithReference.yaml")); // Assert var components = result.OpenApiDocument.Components; @@ -300,10 +301,10 @@ public void ParseBasicSchemaWithReferenceShouldSucceed() } [Fact] - public void ParseAdvancedSchemaWithReferenceShouldSucceed() + public async Task ParseAdvancedSchemaWithReferenceShouldSucceed() { // Act - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "advancedSchemaWithReference.yaml")); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "advancedSchemaWithReference.yaml")); var expectedComponents = new OpenApiComponents { diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSecuritySchemeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSecuritySchemeTests.cs index ef1aa0fdb..50acf4364 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSecuritySchemeTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSecuritySchemeTests.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -20,13 +21,13 @@ public OpenApiSecuritySchemeTests() } [Fact] - public void ParseHttpSecuritySchemeShouldSucceed() + public async Task ParseHttpSecuritySchemeShouldSucceed() { // Act - var securityScheme = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "httpSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var securityScheme = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "httpSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - securityScheme.Should().BeEquivalentTo( + securityScheme.Element.Should().BeEquivalentTo( new OpenApiSecurityScheme { Type = SecuritySchemeType.Http, @@ -35,13 +36,13 @@ public void ParseHttpSecuritySchemeShouldSucceed() } [Fact] - public void ParseApiKeySecuritySchemeShouldSucceed() + public async Task ParseApiKeySecuritySchemeShouldSucceed() { // Act - var securityScheme = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var securityScheme = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - securityScheme.Should().BeEquivalentTo( + securityScheme.Element.Should().BeEquivalentTo( new OpenApiSecurityScheme { Type = SecuritySchemeType.ApiKey, @@ -51,13 +52,13 @@ public void ParseApiKeySecuritySchemeShouldSucceed() } [Fact] - public void ParseBearerSecuritySchemeShouldSucceed() + public async Task ParseBearerSecuritySchemeShouldSucceed() { // Act - var securityScheme = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "bearerSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var securityScheme = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "bearerSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - securityScheme.Should().BeEquivalentTo( + securityScheme.Element.Should().BeEquivalentTo( new OpenApiSecurityScheme { Type = SecuritySchemeType.Http, @@ -67,13 +68,13 @@ public void ParseBearerSecuritySchemeShouldSucceed() } [Fact] - public void ParseOAuth2SecuritySchemeShouldSucceed() + public async Task ParseOAuth2SecuritySchemeShouldSucceed() { // Act - var securityScheme = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "oauth2SecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var securityScheme = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "oauth2SecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - securityScheme.Should().BeEquivalentTo( + securityScheme.Element.Should().BeEquivalentTo( new OpenApiSecurityScheme { Type = SecuritySchemeType.OAuth2, @@ -93,13 +94,13 @@ public void ParseOAuth2SecuritySchemeShouldSucceed() } [Fact] - public void ParseOpenIdConnectSecuritySchemeShouldSucceed() + public async Task ParseOpenIdConnectSecuritySchemeShouldSucceed() { // Act - var securityScheme = OpenApiModelFactory.Load(Path.Combine(SampleFolderPath, "openIdConnectSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0, out _); + var securityScheme = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "openIdConnectSecurityScheme.yaml"), OpenApiSpecVersion.OpenApi3_0); // Assert - securityScheme.Should().BeEquivalentTo( + securityScheme.Element.Should().BeEquivalentTo( new OpenApiSecurityScheme { Type = SecuritySchemeType.OpenIdConnect, diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiXmlTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiXmlTests.cs index c0d99793e..1802823c1 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiXmlTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiXmlTests.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Reader; @@ -21,13 +22,13 @@ public OpenApiXmlTests() } [Fact] - public void ParseBasicXmlShouldSucceed() + public async Task ParseBasicXmlShouldSucceed() { // Act - var xml = OpenApiModelFactory.Load(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0, "yaml", out _); + var xml = await OpenApiModelFactory.LoadAsync(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0); // Assert - xml.Should().BeEquivalentTo( + xml.Element.Should().BeEquivalentTo( new OpenApiXml { Name = "name1", diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs index 884ffa68c..b4cce6981 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs @@ -1684,14 +1684,14 @@ public void SerializeRelativeRootPathWithHostAsV2JsonWorks() } [Fact] - public void TestHashCodesForSimilarOpenApiDocuments() + public async Task TestHashCodesForSimilarOpenApiDocuments() { // Arrange var sampleFolderPath = "Models/Samples/"; - var doc1 = ParseInputFile(Path.Combine(sampleFolderPath, "sampleDocument.yaml")); - var doc2 = ParseInputFile(Path.Combine(sampleFolderPath, "sampleDocument.yaml")); - var doc3 = ParseInputFile(Path.Combine(sampleFolderPath, "sampleDocumentWithWhiteSpaces.yaml")); + var doc1 = await ParseInputFileAsync(Path.Combine(sampleFolderPath, "sampleDocument.yaml")); + var doc2 = await ParseInputFileAsync(Path.Combine(sampleFolderPath, "sampleDocument.yaml")); + var doc3 = await ParseInputFileAsync(Path.Combine(sampleFolderPath, "sampleDocumentWithWhiteSpaces.yaml")); // Act && Assert /* @@ -1702,14 +1702,13 @@ And reading in similar documents(one has a whitespace) yields the same hash code Assert.Equal(doc1.HashCode, doc3.HashCode); } - private static OpenApiDocument ParseInputFile(string filePath) + private static async Task ParseInputFileAsync(string filePath) { // Read in the input yaml file using FileStream stream = File.OpenRead(filePath); - var format = OpenApiModelFactory.GetFormat(filePath); - var openApiDoc = OpenApiDocument.Load(stream, format).OpenApiDocument; + var result = await OpenApiDocument.LoadAsync(stream); - return openApiDoc; + return result.OpenApiDocument; } [Fact] @@ -1999,7 +1998,7 @@ public void SerializeDocumentWithRootJsonSchemaDialectPropertyWorks() } [Fact] - public void SerializeV31DocumentWithRefsInWebhooksWorks() + public async Task SerializeV31DocumentWithRefsInWebhooksWorks() { var expected = @"description: Returns all pets from the system that the user has access to operationId: findPets @@ -2013,11 +2012,11 @@ public void SerializeV31DocumentWithRefsInWebhooksWorks() items: type: object"; - var doc = OpenApiDocument.Load("Models/Samples/docWithReusableWebhooks.yaml").OpenApiDocument; + var result = await OpenApiDocument.LoadAsync("Models/Samples/docWithReusableWebhooks.yaml"); var stringWriter = new StringWriter(); var writer = new OpenApiYamlWriter(stringWriter, new OpenApiWriterSettings { InlineLocalReferences = true }); - var webhooks = doc.Webhooks["pets"].Operations; + var webhooks = result.OpenApiDocument.Webhooks["pets"].Operations; webhooks[OperationType.Get].SerializeAsV31(writer); var actual = stringWriter.ToString(); @@ -2025,7 +2024,7 @@ public void SerializeV31DocumentWithRefsInWebhooksWorks() } [Fact] - public void SerializeDocWithDollarIdInDollarRefSucceeds() + public async Task SerializeDocWithDollarIdInDollarRefSucceeds() { var expected = @"openapi: '3.1.1' info: @@ -2067,9 +2066,9 @@ public void SerializeDocWithDollarIdInDollarRefSucceeds() radius: type: number "; - var doc = OpenApiDocument.Load("Models/Samples/docWithDollarId.yaml").OpenApiDocument; + var result = await OpenApiDocument.LoadAsync("Models/Samples/docWithDollarId.yaml"); - var actual = doc.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1); + var actual = result.OpenApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1); actual.MakeLineBreaksEnvironmentNeutral().Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral()); } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs index 3a16f4d2a..a30dae1e7 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs @@ -9,7 +9,6 @@ using Microsoft.OpenApi.Models.References; using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; -using Microsoft.OpenApi.Services; using Microsoft.OpenApi.Writers; using VerifyXunit; using Xunit; @@ -17,7 +16,7 @@ namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiCallbackReferenceTests + public class OpenApiCallbackReferenceTests : IAsyncLifetime { // OpenApi doc with external $ref private const string OpenApi = @" @@ -128,20 +127,29 @@ public class OpenApiCallbackReferenceTests '200': description: ok"; - private readonly OpenApiCallbackReference _externalCallbackReference; - private readonly OpenApiCallbackReference _localCallbackReference; + private OpenApiCallbackReference _externalCallbackReference; + private OpenApiCallbackReference _localCallbackReference; public OpenApiCallbackReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - OpenApiDocument openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - OpenApiDocument openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + } + + public async Task InitializeAsync() + { + OpenApiDocument openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + OpenApiDocument openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", openApiDoc_2.BaseUri); openApiDoc.Workspace.RegisterComponents(openApiDoc_2); _externalCallbackReference = new("callbackEvent", openApiDoc, "https://myserver.com/beta"); _localCallbackReference = new("callbackEvent", openApiDoc_2); } + public Task DisposeAsync() + { + return Task.CompletedTask; + } + [Fact] public void CallbackReferenceResolutionWorks() { diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs index 4ea8cdef9..deb9c5f95 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiExampleReferenceTests + public class OpenApiExampleReferenceTests : IAsyncLifetime { // OpenApi doc with external $ref private const string OpenApi = @" @@ -105,16 +104,20 @@ public class OpenApiExampleReferenceTests name: John Doe "; - private readonly OpenApiExampleReference _localExampleReference; - private readonly OpenApiExampleReference _externalExampleReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiExampleReference _localExampleReference; + private OpenApiExampleReference _externalExampleReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiExampleReferenceTests() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); @@ -129,7 +132,7 @@ public OpenApiExampleReferenceTests() Summary = "Example of an external user", Description = "This is an example of an external user" }; - } + } [Fact] public void ExampleReferenceResolutionWorks() @@ -183,5 +186,10 @@ public async Task SerializeExampleReferenceAsV31JsonWorks(bool produceTerseOutpu // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs index cfdf4ab1c..7c05170eb 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiHeaderReferenceTests + public class OpenApiHeaderReferenceTests : IAsyncLifetime { // OpenApi doc with external $ref private const string OpenApi= @" @@ -74,16 +73,20 @@ public class OpenApiHeaderReferenceTests type: string "; - private readonly OpenApiHeaderReference _localHeaderReference; - private readonly OpenApiHeaderReference _externalHeaderReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiHeaderReference _localHeaderReference; + private OpenApiHeaderReference _externalHeaderReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiHeaderReferenceTests() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); @@ -97,6 +100,7 @@ public OpenApiHeaderReferenceTests() Description = "Location of the externally referenced post" }; } + [Fact] public void HeaderReferenceResolutionWorks() @@ -158,6 +162,11 @@ public async Task SerializeHeaderReferenceAsV2JsonWorksAsync(bool produceTerseOu // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); + } + + public Task DisposeAsync() + { + return Task.CompletedTask; } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs index 87d2db06e..624bd04e0 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiLinkReferenceTests + public class OpenApiLinkReferenceTests : IAsyncLifetime { // OpenApi doc with external $ref private const string OpenApi = @" @@ -117,16 +116,20 @@ public class OpenApiLinkReferenceTests type: string "; - private readonly OpenApiLinkReference _localLinkReference; - private readonly OpenApiLinkReference _externalLinkReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiLinkReference _localLinkReference; + private OpenApiLinkReference _externalLinkReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiLinkReferenceTests() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); @@ -191,5 +194,10 @@ public async Task SerializeLinkReferenceAsV31JsonWorks(bool produceTerseOutput) // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs index c00db94f5..98b17a6a0 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiParameterReferenceTests + public class OpenApiParameterReferenceTests : IAsyncLifetime { // OpenApi doc with external $ref private const string OpenApi = @" @@ -75,16 +74,20 @@ public class OpenApiParameterReferenceTests minimum: 1 maximum: 100 "; - private readonly OpenApiParameterReference _localParameterReference; - private readonly OpenApiParameterReference _externalParameterReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiParameterReference _localParameterReference; + private OpenApiParameterReference _externalParameterReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiParameterReferenceTests() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); @@ -161,5 +164,10 @@ public async Task SerializeParameterReferenceAsV2JsonWorksAsync(bool produceTers // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs index a2d9b525d..777561f03 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiPathItemReferenceTests + public class OpenApiPathItemReferenceTests : IAsyncLifetime { private const string OpenApi = @" openapi: 3.1.1 @@ -72,16 +71,20 @@ public class OpenApiPathItemReferenceTests description: User deleted successfully "; - private readonly OpenApiPathItemReference _localPathItemReference; - private readonly OpenApiPathItemReference _externalPathItemReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiPathItemReference _localPathItemReference; + private OpenApiPathItemReference _externalPathItemReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiPathItemReferenceTests() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); _openApiDoc_2.Workspace.RegisterComponents(_openApiDoc_2); @@ -135,5 +138,10 @@ public async Task SerializePathItemReferenceAsV31JsonWorks(bool produceTerseOutp // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs index 54521e83c..addf43fe2 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiRequestBodyReferenceTests + public class OpenApiRequestBodyReferenceTests : IAsyncLifetime { private readonly string OpenApi = @" openapi: 3.0.0 @@ -80,16 +79,20 @@ public class OpenApiRequestBodyReferenceTests type: string "; - private readonly OpenApiRequestBodyReference _localRequestBodyReference; - private readonly OpenApiRequestBodyReference _externalRequestBodyReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiRequestBodyReference _localRequestBodyReference; + private OpenApiRequestBodyReference _externalRequestBodyReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiRequestBodyReferenceTests() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); @@ -155,5 +158,10 @@ public async Task SerializeRequestBodyReferenceAsV31JsonWorks(bool produceTerseO // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs index 4b6b25564..c97ae1a11 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs @@ -10,14 +10,13 @@ using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Writers; -using Microsoft.OpenApi.Services; using VerifyXunit; using Xunit; namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiResponseReferenceTest + public class OpenApiResponseReferenceTest : IAsyncLifetime { private const string OpenApi = @" openapi: 3.0.0 @@ -63,16 +62,20 @@ public class OpenApiResponseReferenceTest type: string "; - private readonly OpenApiResponseReference _localResponseReference; - private readonly OpenApiResponseReference _externalResponseReference; - private readonly OpenApiDocument _openApiDoc; - private readonly OpenApiDocument _openApiDoc_2; + private OpenApiResponseReference _localResponseReference; + private OpenApiResponseReference _externalResponseReference; + private OpenApiDocument _openApiDoc; + private OpenApiDocument _openApiDoc_2; public OpenApiResponseReferenceTest() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + _openApiDoc = (await OpenApiDocument.ParseAsync(OpenApi)).OpenApiDocument; + _openApiDoc_2 = (await OpenApiDocument.ParseAsync(OpenApi_2)).OpenApiDocument; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); @@ -137,5 +140,10 @@ public async Task SerializeResponseReferenceAsV31JsonWorks(bool produceTerseOutp // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs index 7fcd7dfd8..7cb700b83 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs @@ -15,7 +15,7 @@ namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiSecuritySchemeReferenceTests + public class OpenApiSecuritySchemeReferenceTests : IAsyncLifetime { private const string OpenApi = @" openapi: 3.0.3 @@ -39,12 +39,16 @@ public class OpenApiSecuritySchemeReferenceTests in: header "; - readonly OpenApiSecuritySchemeReference _openApiSecuritySchemeReference; + OpenApiSecuritySchemeReference _openApiSecuritySchemeReference; public OpenApiSecuritySchemeReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - var result = OpenApiDocument.Parse(OpenApi, "yaml"); + } + + public async Task InitializeAsync() + { + var result = (await OpenApiDocument.ParseAsync(OpenApi)); _openApiSecuritySchemeReference = new("mySecurityScheme", result.OpenApiDocument); } @@ -89,5 +93,9 @@ public async Task SerializeSecuritySchemeReferenceAsV31JsonWorks(bool produceTer // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs index 82f1b27a2..22174d17c 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs @@ -15,7 +15,7 @@ namespace Microsoft.OpenApi.Tests.Models.References { [Collection("DefaultSettings")] - public class OpenApiTagReferenceTest + public class OpenApiTagReferenceTest : IAsyncLifetime { private const string OpenApi = @"openapi: 3.0.3 info: @@ -58,12 +58,16 @@ public class OpenApiTagReferenceTest description: Operations about users. "; - readonly OpenApiTagReference _openApiTagReference; + OpenApiTagReference _openApiTagReference; public OpenApiTagReferenceTest() { - OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - var result = OpenApiDocument.Parse(OpenApi, "yaml"); + OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); + } + + public async Task InitializeAsync() + { + var result = (await OpenApiDocument.ParseAsync(OpenApi)); _openApiTagReference = new("user", result.OpenApiDocument) { Description = "Users operations" @@ -111,5 +115,9 @@ public async Task SerializeTagReferenceAsV31JsonWorks(bool produceTerseOutput) // Assert await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); } + public Task DisposeAsync() + { + return Task.CompletedTask; + } } } diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index ef18b4cfb..90f23b009 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -219,10 +219,10 @@ namespace Microsoft.OpenApi.Interfaces public interface IOpenApiReader { System.Threading.Tasks.Task ReadAsync(System.IO.TextReader input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default); - System.Threading.Tasks.Task ReadAsync(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, string format = null, System.Threading.CancellationToken cancellationToken = default); - T ReadFragment(System.IO.TextReader input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + System.Threading.Tasks.Task ReadAsync(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, System.Threading.CancellationToken cancellationToken = default); + Microsoft.OpenApi.Reader.ReadFragmentResult ReadFragment(System.Text.Json.Nodes.JsonNode input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement; - T ReadFragment(System.Text.Json.Nodes.JsonNode input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + System.Threading.Tasks.Task> ReadFragmentAsync(System.IO.TextReader input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken token = default) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement; } public interface IOpenApiReferenceable : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiSerializable @@ -575,13 +575,10 @@ namespace Microsoft.OpenApi.Models public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } public void SetReferenceHostDocument() { } public static string GenerateHashValue(Microsoft.OpenApi.Models.OpenApiDocument doc) { } - public static Microsoft.OpenApi.Reader.ReadResult Load(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } - public static Microsoft.OpenApi.Reader.ReadResult Load(System.IO.Stream stream, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } - public static Microsoft.OpenApi.Reader.ReadResult Load(System.IO.TextReader input, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } - public static System.Threading.Tasks.Task LoadAsync(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } - public static System.Threading.Tasks.Task LoadAsync(System.IO.TextReader input, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } - public static System.Threading.Tasks.Task LoadAsync(System.IO.Stream stream, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null, System.Threading.CancellationToken cancellationToken = default) { } - public static Microsoft.OpenApi.Reader.ReadResult Parse(string input, string? format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } + public static System.Threading.Tasks.Task LoadAsync(System.IO.TextReader input, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } + public static System.Threading.Tasks.Task LoadAsync(System.IO.Stream stream, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null, System.Threading.CancellationToken cancellationToken = default) { } + public static System.Threading.Tasks.Task LoadAsync(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null, System.Threading.CancellationToken cancellationToken = default) { } + public static System.Threading.Tasks.Task ParseAsync(string input, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null) { } } public class OpenApiEncoding : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable { @@ -1312,31 +1309,27 @@ namespace Microsoft.OpenApi.Reader { public OpenApiJsonReader() { } public System.Threading.Tasks.Task ReadAsync(System.IO.TextReader input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) { } - public System.Threading.Tasks.Task ReadAsync(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, string format = null, System.Threading.CancellationToken cancellationToken = default) { } - public T ReadFragment(System.IO.TextReader input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + public System.Threading.Tasks.Task ReadAsync(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, System.Threading.CancellationToken cancellationToken = default) { } + public Microsoft.OpenApi.Reader.ReadFragmentResult ReadFragment(System.Text.Json.Nodes.JsonNode input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } - public T ReadFragment(System.Text.Json.Nodes.JsonNode input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + public System.Threading.Tasks.Task> ReadFragmentAsync(System.IO.TextReader input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken token = default) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } } public static class OpenApiModelFactory { - public static string GetFormat(string url) { } - public static Microsoft.OpenApi.Reader.ReadResult Load(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } - public static Microsoft.OpenApi.Reader.ReadResult Load(System.IO.Stream stream, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } - public static Microsoft.OpenApi.Reader.ReadResult Load(System.IO.TextReader input, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } - public static T Load(string url, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + public static System.Threading.Tasks.Task GetFormatAsync(string url, System.Threading.CancellationToken token = default) { } + public static System.Threading.Tasks.Task LoadAsync(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) { } + public static System.Threading.Tasks.Task LoadAsync(System.IO.Stream input, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) { } + public static System.Threading.Tasks.Task LoadAsync(System.IO.TextReader input, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) { } + public static System.Threading.Tasks.Task> LoadAsync(System.IO.Stream input, Microsoft.OpenApi.OpenApiSpecVersion version, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } - public static T Load(System.IO.Stream input, Microsoft.OpenApi.OpenApiSpecVersion version, string format, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + public static System.Threading.Tasks.Task> LoadAsync(System.IO.TextReader input, Microsoft.OpenApi.OpenApiSpecVersion version, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } - public static T Load(System.IO.TextReader input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + public static System.Threading.Tasks.Task> LoadAsync(string url, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } - public static System.Threading.Tasks.Task LoadAsync(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } - public static System.Threading.Tasks.Task LoadAsync(System.IO.Stream input, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) { } - public static System.Threading.Tasks.Task LoadAsync(System.IO.TextReader input, string format, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken cancellationToken = default) { } - public static Microsoft.OpenApi.Reader.ReadResult Parse(string input, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } - public static T Parse(string input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) + public static System.Threading.Tasks.Task ParseAsync(string input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } + public static System.Threading.Tasks.Task> ParseAsync(string input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } - public static System.Threading.Tasks.Task ParseAsync(string input, System.IO.StringReader reader, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } } public static class OpenApiReaderRegistry { @@ -1381,6 +1374,13 @@ namespace Microsoft.OpenApi.Reader public void SetTempStorage(string key, object value, object scope = null) { } public void StartObject(string objectName) { } } + public class ReadFragmentResult + where T : Microsoft.OpenApi.Interfaces.IOpenApiElement + { + public ReadFragmentResult() { } + public T Element { get; set; } + public Microsoft.OpenApi.Reader.OpenApiDiagnostic OpenApiDiagnostic { get; set; } + } public class ReadResult { public ReadResult() { }