From acf67415b8d5faeec5b0d9934c22d0de9e773dd1 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 11 Apr 2023 11:08:15 -0400 Subject: [PATCH 1/5] - fixes a bug where the base path would be forcibly set to the description --- .../V2/OpenApiDocumentDeserializer.cs | 6 ++++ .../V2Tests/OpenApiServerTests.cs | 29 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs b/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs index 02e868412..fa3aa7224 100644 --- a/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs +++ b/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs @@ -139,6 +139,12 @@ private static void MakeServers(IList servers, ParsingContext con var schemes = context.GetFromTempStorage>("schemes"); Uri defaultUrl = rootNode.Context.BaseUrl; + // so we don't default to the document path when a host is provided + if (string.IsNullOrEmpty(basePath) && !string.IsNullOrEmpty(host)) + { + basePath = "/"; + } + // If nothing is provided, don't create a server if (host == null && basePath == null && schemes == null) { diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs index c87b491ab..1b917fde7 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs @@ -74,6 +74,31 @@ public void JustHostNoDefault() Assert.Equal("//www.foo.com", server.Url); } + [Fact] + public void NoBasePath() + { + var input = @" +swagger: 2.0 +info: + title: test + version: 1.0.0 +host: www.foo.com +schemes: + - http +paths: {} +"; + var reader = new OpenApiStringReader(new OpenApiReaderSettings() + { + BaseUrl = new Uri("https://www.foo.com/spec.yaml") + }); + + var doc = reader.Read(input, out var diagnostic); + + var server = doc.Servers.First(); + Assert.Equal(1, doc.Servers.Count); + Assert.Equal("http://www.foo.com", server.Url); + } + [Fact] public void JustBasePathNoDefault() { @@ -203,14 +228,14 @@ public void JustHostWithCustomHostWithApi() "; var reader = new OpenApiStringReader(new OpenApiReaderSettings() { - BaseUrl = new Uri("https://dev.bing.com/api") + BaseUrl = new Uri("https://dev.bing.com/api/description.yaml") }); var doc = reader.Read(input, out var diagnostic); var server = doc.Servers.First(); Assert.Equal(1, doc.Servers.Count); - Assert.Equal("https://prod.bing.com/api", server.Url); + Assert.Equal("https://prod.bing.com", server.Url); } [Fact] From d6fbdacdef9425074a10e2fdb7b79a0d8556b367 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 11 Apr 2023 21:57:16 +0000 Subject: [PATCH 2/5] Bump Microsoft.Windows.Compatibility from 7.0.0 to 7.0.1 Bumps [Microsoft.Windows.Compatibility](https://github.com/dotnet/runtime) from 7.0.0 to 7.0.1. - [Release notes](https://github.com/dotnet/runtime/releases) - [Commits](https://github.com/dotnet/runtime/compare/v7.0.0...v7.0.1) --- updated-dependencies: - dependency-name: Microsoft.Windows.Compatibility dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .../Microsoft.OpenApi.Workbench.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj index c7ab75af4..88f12fcb9 100644 --- a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj +++ b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj @@ -1,4 +1,4 @@ - + net7.0-windows WinExe @@ -10,7 +10,7 @@ all - + From b5cfd7ecb7b8e6c8ecda60982a59c1fc29960f13 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Wed, 12 Apr 2023 12:09:03 +0300 Subject: [PATCH 3/5] Store already cloned objects in a dictionary to avoid circular dependency during cloning --- .../Helpers/DictionaryCloneHelper.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs b/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs index 279e4639d..a87765896 100644 --- a/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs +++ b/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs @@ -21,14 +21,26 @@ internal class DictionaryCloneHelper internal static Dictionary Clone(IDictionary dictionary) { if (dictionary is null) return null; + var clonedDictionary = new Dictionary(dictionary.Keys.Count); + var clonedObjects = new Dictionary(); - foreach (var kvp in dictionary) + foreach (var keyValuePair in dictionary) { - // Create instance of the specified type using the constructor matching the specified parameter types. - clonedDictionary[kvp.Key] = (U)Activator.CreateInstance(kvp.Value.GetType(), kvp.Value); - } - + // If the object has already been cloned, use the cloned object instead of cloning it again + if (clonedObjects.TryGetValue(keyValuePair.Value, out var clonedValue)) + { + clonedDictionary[keyValuePair.Key] = (U)clonedValue; + } + else + { + // Create instance of the specified type using the constructor matching the specified parameter types. + clonedDictionary[keyValuePair.Key] = (U)Activator.CreateInstance(keyValuePair.Value.GetType(), keyValuePair.Value); + + // Add the cloned object to the dictionary of cloned objects + clonedObjects.Add(keyValuePair.Value, clonedDictionary[keyValuePair.Key]); + } + } return clonedDictionary; } From e739083b7f975ef603eab17f7b36f2262d310803 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Wed, 12 Apr 2023 12:25:52 +0300 Subject: [PATCH 4/5] Adds a static class modifier --- src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs b/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs index a87765896..1af7bc8c4 100644 --- a/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs +++ b/src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs @@ -9,7 +9,7 @@ namespace Microsoft.OpenApi.Helpers /// /// Helper class for deep cloning dictionaries. /// - internal class DictionaryCloneHelper + internal static class DictionaryCloneHelper { /// /// Deep clone key value pairs in a dictionary. From 178bb469ca3ea509142ac9c24d17bf3afc25a597 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Wed, 12 Apr 2023 15:40:30 +0300 Subject: [PATCH 5/5] Bump lib versions --- src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj | 2 +- src/Microsoft.OpenApi/Microsoft.OpenApi.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj index 69597d978..b3c482215 100644 --- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj +++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj @@ -10,7 +10,7 @@ Microsoft Microsoft.OpenApi.Readers Microsoft.OpenApi.Readers - 1.6.4-preview2 + 1.6.4-preview3 OpenAPI.NET Readers for JSON and YAML documents © Microsoft Corporation. All rights reserved. OpenAPI .NET diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index c996acbaf..4a291f120 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -11,7 +11,7 @@ Microsoft Microsoft.OpenApi Microsoft.OpenApi - 1.6.4-preview2 + 1.6.4-preview3 .NET models with JSON and YAML writers for OpenAPI specification © Microsoft Corporation. All rights reserved. OpenAPI .NET