Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7bd19b9

Browse files
committedAug 19, 2024·
Add URL substitution enum value validation
1 parent 6322fde commit 7bd19b9

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed
 

Diff for: ‎src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public static class OpenApiServerExtensions
1717
/// <param name="values">The server variable values that will be used to replace the default values.</param>
1818
/// <returns>A URL with the provided variables substituted.</returns>
1919
/// <exception cref="ArgumentException">
20-
/// If a substitution has no value in the supplied dictionary or the default
20+
/// Thrown when:
21+
/// 1. A substitution has no valid value in both the supplied dictionary and the default
22+
/// 2. A substitution's value is not available in the enum provided
2123
/// </exception>
2224
public static string ReplaceServerUrlVariables(this OpenApiServer server, IDictionary<string, string> values)
2325
{
@@ -35,6 +37,12 @@ public static string ReplaceServerUrlVariables(this OpenApiServer server, IDicti
3537
throw new ArgumentException(
3638
string.Format(SRResource.ParseServerUrlDefaultValueNotAvailable, variable.Key), nameof(server));
3739
}
40+
41+
if (variable.Value.Enum?.Contains(actualValue) != true)
42+
{
43+
throw new ArgumentException(
44+
string.Format(SRResource.ParseServerUrlValueNotValid, actualValue, variable.Key), nameof(values));
45+
}
3846
parsedUrl = parsedUrl.Replace($"{{{variable.Key}}}", actualValue);
3947
}
4048
return parsedUrl;

Diff for: ‎src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎src/Microsoft.OpenApi/Properties/SRResource.resx

+3
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,7 @@
228228
<data name="ParseServerUrlDefaultValueNotAvailable" xml:space="preserve">
229229
<value>Invalid server variable '{0}'. A value was not provided and no default value was provided.</value>
230230
</data>
231+
<data name="ParseServerUrlValueNotValid" xml:space="preserve">
232+
<value>Value '{0}' is not a valid value for variable '{1}'.</value>
233+
</data>
231234
</root>

Diff for: ‎test/Microsoft.OpenApi.Tests/Extensions/OpenApiServerExtensionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void ShouldFailIfProvidedValueIsNotInEnum()
7979

8080
Assert.Throws<ArgumentException>(() =>
8181
{
82-
variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"version", "v2"}});
82+
variable.ReplaceServerUrlVariables(new Dictionary<string, string> {{"version", "v3"}});
8383
});
8484
}
8585
}

0 commit comments

Comments
 (0)
Please sign in to comment.