-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiServerExtensions.cs
55 lines (49 loc) · 2.19 KB
/
OpenApiServerExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}
}