-
Notifications
You must be signed in to change notification settings - Fork 266
Add server variable substitution logic. #1783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6322fde
Add server variable substitution logic.
calebkiage 7bd19b9
Add URL substitution enum value validation
calebkiage 8f43079
Add validation for server variable default value
calebkiage 4c299f3
Fix some failing tests
calebkiage 8f5e449
Simplify code
calebkiage a63aa29
add new extension function to public api
calebkiage be027b3
allow null enums
calebkiage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.OpenApi.Models; | ||
using Microsoft.OpenApi.Properties; | ||
|
||
namespace Microsoft.OpenApi.Extensions; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="OpenApiServer"/> serialization. | ||
/// </summary> | ||
public static class OpenApiServerExtensions | ||
{ | ||
/// <summary> | ||
/// Replaces URL variables in a server's URL | ||
/// </summary> | ||
/// <param name="server">The OpenAPI server object</param> | ||
/// <param name="values">The server variable values that will be used to replace the default values.</param> | ||
/// <returns>A URL with the provided variables substituted.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// 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 | ||
/// </exception> | ||
public static string ReplaceServerUrlVariables(this OpenApiServer server, IDictionary<string, string> 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; | ||
} | ||
|
||
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 (variable.Value.Enum is { Count: > 0 } e && !e.Contains(value)) | ||
{ | ||
throw new ArgumentException( | ||
string.Format(SRResource.ParseServerUrlValueNotValid, value, variable.Key), nameof(values)); | ||
} | ||
|
||
parsedUrl = parsedUrl.Replace($"{{{variable.Key}}}", value); | ||
} | ||
|
||
return parsedUrl; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
test/Microsoft.OpenApi.Tests/Extensions/OpenApiServerExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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<string, OpenApiServerVariable> | ||
{ | ||
{ "version", new OpenApiServerVariable { Default = "v1", Enum = ["v1", "v2"]} } | ||
} | ||
}; | ||
|
||
var url = variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"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<string, OpenApiServerVariable> | ||
{ | ||
{ "version", new OpenApiServerVariable { Default = "v1", Enum = ["v1", "v2"]} } | ||
} | ||
}; | ||
|
||
var url = variable.ReplaceServerUrlVariables(new Dictionary<string, string>(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<string, OpenApiServerVariable> | ||
{ | ||
{ "version", new OpenApiServerVariable { Enum = ["v1", "v2"]} } | ||
} | ||
}; | ||
|
||
Assert.Throws<ArgumentException>(() => | ||
{ | ||
variable.ReplaceServerUrlVariables(new Dictionary<string, string>(0)); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void ShouldFailIfProvidedValueIsNotInEnum() | ||
{ | ||
var variable = new OpenApiServer | ||
{ | ||
Url = "http://example.com/api/{version}", | ||
Description = string.Empty, | ||
Variables = new Dictionary<string, OpenApiServerVariable> | ||
{ | ||
{ "version", new OpenApiServerVariable { Enum = ["v1", "v2"]} } | ||
} | ||
}; | ||
|
||
Assert.Throws<ArgumentException>(() => | ||
{ | ||
variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"version", "v3"}}); | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.