Skip to content

Commit 4c299f3

Browse files
committed
Fix some failing tests
Update error message for server variable
1 parent 8f43079 commit 4c299f3

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiServerExtensions.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static string ReplaceServerUrlVariables(this OpenApiServer server, IDicti
3030
values.TryGetValue(variable.Key, out var actualValue);
3131
// Fall back to the default value
3232
if (string.IsNullOrEmpty(actualValue)) { actualValue = variable.Value.Default; }
33+
3334
if (string.IsNullOrEmpty(actualValue))
3435
{
3536
// According to the spec, the variable's default value is required.
@@ -38,13 +39,15 @@ public static string ReplaceServerUrlVariables(this OpenApiServer server, IDicti
3839
string.Format(SRResource.ParseServerUrlDefaultValueNotAvailable, variable.Key), nameof(server));
3940
}
4041

41-
if (variable.Value.Enum?.Contains(actualValue) != true)
42+
if (variable.Value.Enum is { Count: > 0 } e && !e.Contains(actualValue))
4243
{
4344
throw new ArgumentException(
4445
string.Format(SRResource.ParseServerUrlValueNotValid, actualValue, variable.Key), nameof(values));
4546
}
47+
4648
parsedUrl = parsedUrl.Replace($"{{{variable.Key}}}", actualValue);
4749
}
50+
4851
return parsedUrl;
4952
}
5053
}

src/Microsoft.OpenApi/Validations/Rules/OpenApiServerRules.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,24 @@ public static class OpenApiServerRules
3232
foreach (var variable in server.Variables)
3333
{
3434
context.Enter(variable.Key);
35-
ValidateServerVariableRequiredFields(context, variable.Value);
35+
ValidateServerVariableRequiredFields(context, variable.Key, variable.Value);
3636
context.Exit();
3737
}
38+
context.Exit();
3839
});
3940

4041
// add more rules
4142

4243
/// <summary>
4344
/// Validate required fields in server variable
4445
/// </summary>
45-
private static void ValidateServerVariableRequiredFields(IValidationContext context, OpenApiServerVariable item)
46+
private static void ValidateServerVariableRequiredFields(IValidationContext context, string key, OpenApiServerVariable item)
4647
{
4748
context.Enter("default");
4849
if (string.IsNullOrEmpty(item.Default))
4950
{
5051
context.CreateError("ServerVariableMustHaveDefaultValue",
51-
String.Format(SRResource.Validation_FieldIsRequired, "default", "variables"));
52+
String.Format(SRResource.Validation_FieldIsRequired, "default", key));
5253
}
5354
context.Exit();
5455
}

0 commit comments

Comments
 (0)