From ece311110dfe865822d2ed8740391d9c477bdef6 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 12 Aug 2024 21:49:49 +0000 Subject: [PATCH 01/10] Bump Microsoft.OData.Edm from 7.21.3 to 8.0.0 Bumps Microsoft.OData.Edm from 7.21.3 to 8.0.0. --- updated-dependencies: - dependency-name: Microsoft.OData.Edm dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index 05293a686..ba264d9c4 100644 --- a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj +++ b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj @@ -34,7 +34,7 @@ - + From af252a8673cd942adeb612282f7ad065b115845f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 13 Aug 2024 02:34:18 +0300 Subject: [PATCH 02/10] Bump Verify.Xunit from 26.1.6 to 26.2.0 (#1778) Bumps [Verify.Xunit](https://github.com/VerifyTests/Verify) from 26.1.6 to 26.2.0. - [Release notes](https://github.com/VerifyTests/Verify/releases) - [Commits](https://github.com/VerifyTests/Verify/compare/26.1.6...26.2.0) --- updated-dependencies: - dependency-name: Verify.Xunit dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index a4630d78c..5399d91d4 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -15,7 +15,7 @@ - + From abb047356a51e00ff72aedcf225236d17fb0b9eb Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 13 Aug 2024 02:34:43 +0300 Subject: [PATCH 03/10] Bump docker/build-push-action from 6.5.0 to 6.6.1 (#1777) Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 6.5.0 to 6.6.1. - [Release notes](https://github.com/docker/build-push-action/releases) - [Commits](https://github.com/docker/build-push-action/compare/v6.5.0...v6.6.1) --- updated-dependencies: - dependency-name: docker/build-push-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/docker.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index be1bd2a64..d6d023117 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,13 +30,13 @@ jobs: id: getversion - name: Push to GitHub Packages - Nightly if: ${{ github.ref == 'refs/heads/vnext' }} - uses: docker/build-push-action@v6.5.0 + uses: docker/build-push-action@v6.6.1 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly - name: Push to GitHub Packages - Release if: ${{ github.ref == 'refs/heads/master' }} - uses: docker/build-push-action@v6.5.0 + uses: docker/build-push-action@v6.6.1 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }} From bbd081d7b021a3da7c623e1a0cbc4d66c57a1c04 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:37:25 +0000 Subject: [PATCH 04/10] Bump Microsoft.Windows.Compatibility from 8.0.7 to 8.0.8 Bumps [Microsoft.Windows.Compatibility](https://github.com/dotnet/windowsdesktop) from 8.0.7 to 8.0.8. - [Release notes](https://github.com/dotnet/windowsdesktop/releases) - [Commits](https://github.com/dotnet/windowsdesktop/compare/v8.0.7...v8.0.8) --- 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj index bdf6181a4..301066766 100644 --- a/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj +++ b/src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj @@ -8,7 +8,7 @@ true - + From 0c82895022e6980bc1fc9063e7d08d5ef408ed30 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 19 Aug 2024 08:50:55 +0300 Subject: [PATCH 05/10] Bump Newtonsoft.Json from 13.0.1 to 13.0.3 (#1782) Bumps Newtonsoft.Json from 13.0.1 to 13.0.3. --- updated-dependencies: - dependency-name: Newtonsoft.Json dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../Microsoft.OpenApi.Hidi.Tests.csproj | 1 + 1 file changed, 1 insertion(+) 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 cef3a9174..c1147319f 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj +++ b/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj @@ -14,6 +14,7 @@ + From 19d6e4bcef17bbbd3ae0936bb99f91c045af9d81 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:28:35 +0000 Subject: [PATCH 06/10] Bump docker/build-push-action from 6.6.1 to 6.7.0 Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 6.6.1 to 6.7.0. - [Release notes](https://github.com/docker/build-push-action/releases) - [Commits](https://github.com/docker/build-push-action/compare/v6.6.1...v6.7.0) --- updated-dependencies: - dependency-name: docker/build-push-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/docker.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index d6d023117..ee951983c 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,13 +30,13 @@ jobs: id: getversion - name: Push to GitHub Packages - Nightly if: ${{ github.ref == 'refs/heads/vnext' }} - uses: docker/build-push-action@v6.6.1 + uses: docker/build-push-action@v6.7.0 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly - name: Push to GitHub Packages - Release if: ${{ github.ref == 'refs/heads/master' }} - uses: docker/build-push-action@v6.6.1 + uses: docker/build-push-action@v6.7.0 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }} From ca81fa90a4c6cc0385f05f7464478224518fcaf2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:56:55 +0000 Subject: [PATCH 07/10] Bump Microsoft.OData.Edm from 8.0.0 to 8.0.1 Bumps Microsoft.OData.Edm from 8.0.0 to 8.0.1. --- updated-dependencies: - dependency-name: Microsoft.OData.Edm dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index ba264d9c4..e635fa5a3 100644 --- a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj +++ b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj @@ -34,7 +34,7 @@ - + From e468649482600e1d659df86715c74f85baa3ff0f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:00:43 +0000 Subject: [PATCH 08/10] Bump Microsoft.NET.Test.Sdk from 17.10.0 to 17.11.0 Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.10.0 to 17.11.0. - [Release notes](https://github.com/microsoft/vstest/releases) - [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md) - [Commits](https://github.com/microsoft/vstest/compare/v17.10.0...v17.11.0) --- updated-dependencies: - dependency-name: Microsoft.NET.Test.Sdk dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .../Microsoft.OpenApi.Hidi.Tests.csproj | 2 +- .../Microsoft.OpenApi.Readers.Tests.csproj | 2 +- .../Microsoft.OpenApi.SmokeTests.csproj | 2 +- test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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 c1147319f..f5958e5b6 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj +++ b/test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj @@ -12,7 +12,7 @@ - + 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 8ffaa2cf1..3b6f7c76e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj +++ b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj @@ -17,7 +17,7 @@ - + diff --git a/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj b/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj index 8bcded29f..4c93a8354 100644 --- a/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj +++ b/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj @@ -10,7 +10,7 @@ - + diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index 5399d91d4..081f70390 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -11,7 +11,7 @@ - + From 42dda81f3c3fcb15455a7752e3a06942feb4173f Mon Sep 17 00:00:00 2001 From: Caleb Kiage <747955+calebkiage@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:41:49 +0300 Subject: [PATCH 09/10] Add server variable substitution logic. (#1783) * Add server variable substitution logic. --- .../Extensions/OpenApiServerExtensions.cs | 57 ++++++++++ .../Models/OpenApiDocument.cs | 10 +- .../Models/OpenApiServerVariable.cs | 5 +- .../Properties/SRResource.Designer.cs | 19 +++- .../Properties/SRResource.resx | 6 + .../Validations/Rules/OpenApiServerRules.cs | 23 ++++ .../V3Tests/OpenApiDocumentTests.cs | 65 +++++++++++ .../OpenApiServerExtensionsTests.cs | 104 ++++++++++++++++++ .../PublicApi/PublicApi.approved.txt | 4 + 9 files changed, 283 insertions(+), 10 deletions(-) create mode 100644 src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs create mode 100644 test/Microsoft.OpenApi.Tests/Extensions/OpenApiServerExtensionsTests.cs diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs b/src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs new file mode 100644 index 000000000..b885cb235 --- /dev/null +++ b/src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Properties; + +namespace Microsoft.OpenApi.Extensions; + +/// +/// Extension methods for serialization. +/// +public static class OpenApiServerExtensions +{ + /// + /// Replaces URL variables in a server's URL + /// + /// The OpenAPI server object + /// The server variable values that will be used to replace the default values. + /// A URL with the provided variables substituted. + /// + /// Thrown when: + /// 1. A substitution has no valid value in both the supplied dictionary and the default + /// 2. A substitution's value is not available in the enum provided + /// + public static string ReplaceServerUrlVariables(this OpenApiServer server, IDictionary values = null) + { + var parsedUrl = server.Url; + foreach (var variable in server.Variables) + { + // Try to get the value from the provided values + if (values is not { } v || !v.TryGetValue(variable.Key, out var value) || string.IsNullOrEmpty(value)) + { + // Fall back to the default value + value = variable.Value.Default; + } + + // Validate value + if (string.IsNullOrEmpty(value)) + { + // According to the spec, the variable's default value is required. + // This code path should be hit when a value isn't provided & a default value isn't available + throw new ArgumentException( + string.Format(SRResource.ParseServerUrlDefaultValueNotAvailable, variable.Key), nameof(server)); + } + + // If an enum is provided, the array should not be empty & the value should exist in the enum + if (variable.Value.Enum is {} e && (e.Count == 0 || !e.Contains(value))) + { + throw new ArgumentException( + string.Format(SRResource.ParseServerUrlValueNotValid, value, variable.Key), nameof(values)); + } + + parsedUrl = parsedUrl.Replace($"{{{variable.Key}}}", value); + } + + return parsedUrl; + } +} diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index 712bddb03..201b321f1 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -8,6 +8,7 @@ using System.Security.Cryptography; using System.Text; using Microsoft.OpenApi.Exceptions; +using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Services; using Microsoft.OpenApi.Writers; @@ -283,14 +284,7 @@ public void SerializeAsV2(IOpenApiWriter writer) private static string ParseServerUrl(OpenApiServer server) { - var parsedUrl = server.Url; - - var variables = server.Variables; - foreach (var variable in variables.Where(static x => !string.IsNullOrEmpty(x.Value.Default))) - { - parsedUrl = parsedUrl.Replace($"{{{variable.Key}}}", variable.Value.Default); - } - return parsedUrl; + return server.ReplaceServerUrlVariables(new Dictionary(0)); } private static void WriteHostInfoV2(IOpenApiWriter writer, IList servers) diff --git a/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs b/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs index fe5293cd4..4ab8bdcaa 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs @@ -26,7 +26,10 @@ public class OpenApiServerVariable : IOpenApiSerializable, IOpenApiExtensible /// /// An enumeration of string values to be used if the substitution options are from a limited set. /// - public List Enum { get; set; } = new(); + /// + /// If the server variable in the OpenAPI document has no enum member, this property will be null. + /// + public List Enum { get; set; } /// /// This object MAY be extended with Specification Extensions. diff --git a/src/Microsoft.OpenApi/Properties/SRResource.Designer.cs b/src/Microsoft.OpenApi/Properties/SRResource.Designer.cs index 1a9ab3014..511f300f7 100644 --- a/src/Microsoft.OpenApi/Properties/SRResource.Designer.cs +++ b/src/Microsoft.OpenApi/Properties/SRResource.Designer.cs @@ -1,7 +1,6 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -222,6 +221,24 @@ internal static string OpenApiWriterExceptionGenericError { } } + /// + /// Looks up a localized string similar to Invalid server variable '{0}'. A value was not provided and no default value was provided.. + /// + internal static string ParseServerUrlDefaultValueNotAvailable { + get { + return ResourceManager.GetString("ParseServerUrlDefaultValueNotAvailable", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Value '{0}' is not a valid value for variable '{1}'. If an enum is provided, it should not be empty and the value provided should exist in the enum. + /// + internal static string ParseServerUrlValueNotValid { + get { + return ResourceManager.GetString("ParseServerUrlValueNotValid", resourceCulture); + } + } + /// /// Looks up a localized string similar to The given primitive type '{0}' is not supported.. /// diff --git a/src/Microsoft.OpenApi/Properties/SRResource.resx b/src/Microsoft.OpenApi/Properties/SRResource.resx index f0bb497d3..0effa1d44 100644 --- a/src/Microsoft.OpenApi/Properties/SRResource.resx +++ b/src/Microsoft.OpenApi/Properties/SRResource.resx @@ -225,4 +225,10 @@ OpenAPI document must be added to an OpenApiWorkspace to be able to resolve external references. + + Invalid server variable '{0}'. A value was not provided and no default value was provided. + + + Value '{0}' is not a valid value for variable '{1}'. If an enum is provided, it should not be empty and the value provided should exist in the enum + diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiServerRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiServerRules.cs index dd11a661d..35d4b9a25 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiServerRules.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiServerRules.cs @@ -26,9 +26,32 @@ public static class OpenApiServerRules context.CreateError(nameof(ServerRequiredFields), String.Format(SRResource.Validation_FieldIsRequired, "url", "server")); } + + context.Exit(); + context.Enter("variables"); + foreach (var variable in server.Variables) + { + context.Enter(variable.Key); + ValidateServerVariableRequiredFields(context, variable.Key, variable.Value); + context.Exit(); + } context.Exit(); }); // add more rules + + /// + /// Validate required fields in server variable + /// + private static void ValidateServerVariableRequiredFields(IValidationContext context, string key, OpenApiServerVariable item) + { + context.Enter("default"); + if (string.IsNullOrEmpty(item.Default)) + { + context.CreateError("ServerVariableMustHaveDefaultValue", + String.Format(SRResource.Validation_FieldIsRequired, "default", key)); + } + context.Exit(); + } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs index d67c0054f..bb3db096f 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs @@ -1367,5 +1367,70 @@ public void ParseDocumetWithWrongReferenceTypeShouldReturnADiagnosticError() diagnostic.Errors.Should().BeEquivalentTo(new List { new( new OpenApiException("Invalid Reference Type 'Schema'.")) }); } + + [Fact] + public void ParseBasicDocumentWithServerVariableShouldSucceed() + { + var openApiDoc = new OpenApiStringReader().Read(""" + openapi : 3.0.0 + info: + title: The API + version: 0.9.1 + servers: + - url: http://www.example.org/api/{version} + description: The http endpoint + variables: + version: + default: v2 + enum: [v1, v2] + paths: {} + """, out var diagnostic); + + diagnostic.Should().BeEquivalentTo( + new OpenApiDiagnostic { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }); + + openApiDoc.Should().BeEquivalentTo( + new OpenApiDocument + { + Info = new() + { + Title = "The API", + Version = "0.9.1", + }, + Servers = + { + new OpenApiServer + { + Url = "http://www.example.org/api/{version}", + Description = "The http endpoint", + Variables = new Dictionary + { + {"version", new OpenApiServerVariable {Default = "v2", Enum = ["v1", "v2"]}} + } + } + }, + Paths = new() + }); + } + + [Fact] + public void ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail() + { + var openApiDoc = new OpenApiStringReader().Read(""" + openapi : 3.0.0 + info: + title: The API + version: 0.9.1 + servers: + - url: http://www.example.org/api/{version} + description: The http endpoint + variables: + version: + enum: [v1, v2] + paths: {} + """, out var diagnostic); + + diagnostic.Errors.Should().NotBeEmpty(); + } } } diff --git a/test/Microsoft.OpenApi.Tests/Extensions/OpenApiServerExtensionsTests.cs b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiServerExtensionsTests.cs new file mode 100644 index 000000000..b8f581541 --- /dev/null +++ b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiServerExtensionsTests.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using Microsoft.OpenApi.Extensions; +using Microsoft.OpenApi.Models; +using Xunit; + +namespace Microsoft.OpenApi.Tests.Extensions; + +public class OpenApiServerExtensionsTests +{ + [Fact] + public void ShouldSubstituteServerVariableWithProvidedValues() + { + var variable = new OpenApiServer + { + Url = "http://example.com/api/{version}", + Description = string.Empty, + Variables = new Dictionary + { + { "version", new OpenApiServerVariable { Default = "v1", Enum = ["v1", "v2"]} } + } + }; + + var url = variable.ReplaceServerUrlVariables(new Dictionary {{"version", "v2"}}); + + url.Should().Be("http://example.com/api/v2"); + } + + [Fact] + public void ShouldSubstituteServerVariableWithDefaultValues() + { + var variable = new OpenApiServer + { + Url = "http://example.com/api/{version}", + Description = string.Empty, + Variables = new Dictionary + { + { "version", new OpenApiServerVariable { Default = "v1", Enum = ["v1", "v2"]} } + } + }; + + var url = variable.ReplaceServerUrlVariables(new Dictionary(0)); + + url.Should().Be("http://example.com/api/v1"); + } + + [Fact] + public void ShouldFailIfNoValueIsAvailable() + { + var variable = new OpenApiServer + { + Url = "http://example.com/api/{version}", + Description = string.Empty, + Variables = new Dictionary + { + { "version", new OpenApiServerVariable { Enum = ["v1", "v2"]} } + } + }; + + Assert.Throws(() => + { + variable.ReplaceServerUrlVariables(new Dictionary(0)); + }); + } + + [Fact] + public void ShouldFailIfProvidedValueIsNotInEnum() + { + var variable = new OpenApiServer + { + Url = "http://example.com/api/{version}", + Description = string.Empty, + Variables = new Dictionary + { + { "version", new OpenApiServerVariable { Enum = ["v1", "v2"]} } + } + }; + + Assert.Throws(() => + { + variable.ReplaceServerUrlVariables(new Dictionary {{"version", "v3"}}); + }); + } + + [Fact] + public void ShouldFailIfEnumIsEmpty() + { + var variable = new OpenApiServer + { + Url = "http://example.com/api/{version}", + Description = string.Empty, + Variables = new Dictionary + { + { "version", new OpenApiServerVariable { Enum = []} } + } + }; + + Assert.Throws(() => + { + variable.ReplaceServerUrlVariables(new Dictionary {{"version", "v1"}}); + }); + } +} diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index fbfe564f3..9483e5f6e 100755 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -285,6 +285,10 @@ namespace Microsoft.OpenApi.Extensions public static void SerializeAsYaml(this T element, System.IO.Stream stream, Microsoft.OpenApi.OpenApiSpecVersion specVersion) where T : Microsoft.OpenApi.Interfaces.IOpenApiSerializable { } } + public static class OpenApiServerExtensions + { + public static string ReplaceServerUrlVariables(this Microsoft.OpenApi.Models.OpenApiServer server, System.Collections.Generic.IDictionary values = null) { } + } public static class OpenApiTypeMapper { public static System.Type MapOpenApiPrimitiveTypeToSimpleType(this Microsoft.OpenApi.Models.OpenApiSchema schema) { } From 3f701e950033ededdc6e675700e4f1d7e73af1af Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Fri, 23 Aug 2024 12:43:12 +0300 Subject: [PATCH 10/10] Update OData lib and bump Hidi version --- src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index e635fa5a3..309fec2de 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.7 + 1.4.8 OpenAPI.NET CLI tool for slicing OpenAPI documents true @@ -35,7 +35,7 @@ - +