Repository navigation
Expand file tree
/
Copy pathOpenAPIOverrides.cs
More file actions
106 lines (93 loc) · 4.48 KB
/
Copy pathOpenAPIOverrides.cs
File metadata and controls
106 lines (93 loc) · 4.48 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System.Text.Json;
using CheckCloudSupport.Extensions;
namespace CheckCloudSupport.OpenAPI;
/// <summary>
/// Contains helper methods to lookup override paths.
/// </summary>
public static class OpenAPIOverrides
{
private static List<Override>? overrides = null;
private static List<CloudExclusion>? cloudExclusions = null;
/// <summary>
/// Loads overrides and exclusions from JSON files.
/// </summary>
/// <param name="overridesFile">The path to a JSON file containing API overrides.</param>
/// <param name="excludesFile">The path to a JSON file containing cloud exclusions.</param>
public static void Initialize(string? overridesFile, string? excludesFile)
{
LoadOverridesFromJson(overridesFile);
LoadCloudExclusionsFromJson(excludesFile);
}
/// <summary>
/// Returns override API path for a given API path, if one exists.
/// </summary>
/// <param name="originalPath">The original API path to check for an override for.</param>
/// <param name="method">The HTTP method to check for an override for.</param>
/// <returns>The override path.</returns>
public static string CheckForOverride(string originalPath, HttpMethod? method)
{
var apiOverride = overrides?.SingleOrDefault(o => PathsMatch(o.ApiPath, originalPath));
if (!string.IsNullOrEmpty(apiOverride?.Operation) &&
method != null &&
string.Compare(apiOverride.Operation, method.Method, StringComparison.OrdinalIgnoreCase) != 0)
{
return originalPath;
}
return apiOverride == null ? originalPath : apiOverride.OverridePath ?? originalPath;
}
/// <summary>
/// Checks if a cloud is excluded for a given API path and method.
/// </summary>
/// <param name="apiPath">The API path to check for an exclusion for.</param>
/// <param name="method">The HTTP method to check for an exclusion for.</param>
/// <param name="cloud">The cloud to check.</param>
/// <returns>True if the given cloud is excluded.</returns>
public static bool CheckIfCloudExcluded(string apiPath, HttpMethod? method, string cloud)
{
apiPath = apiPath.Replace("\\", "/");
return cloudExclusions?.Any(e => string.Compare(e.ApiPath, apiPath, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(e.Operation, method?.Method, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(e.Cloud, cloud, StringComparison.InvariantCultureIgnoreCase) == 0) ?? false;
}
/// <summary>
/// Checks if a cloud is excluded for a given API doc file. This is used to exclude entire
/// files of APIs that don't function in certain clouds, even if the individual API paths aren't known.
/// </summary>
/// <param name="fileName">The name of the API doc file to check for an exclusion for.</param>
/// <param name="cloud">The cloud to check.</param>
/// <returns>True if the given cloud is excluded for the specified file.</returns>
public static bool CheckIfCloudExcludedForFile(string fileName, string cloud)
{
return cloudExclusions?.Any(e => string.Compare(e.FileName, fileName, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(e.Cloud, cloud, StringComparison.InvariantCultureIgnoreCase) == 0) ?? false;
}
private static bool PathsMatch(string? overridePath, string apiPath)
{
if (overridePath == null)
{
return false;
}
var overrideSegments = overridePath.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var apiSegments = apiPath.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return overrideSegments.Length == apiSegments.Length &&
overrideSegments.Zip(apiSegments).All(s => s.First.IsEqualIgnoringCase(s.Second));
}
private static void LoadOverridesFromJson(string? jsonFile)
{
if (!string.IsNullOrEmpty(jsonFile))
{
var json = File.ReadAllText(jsonFile);
overrides = JsonSerializer.Deserialize<List<Override>>(json);
}
}
private static void LoadCloudExclusionsFromJson(string? jsonFile)
{
if (!string.IsNullOrEmpty(jsonFile))
{
var json = File.ReadAllText(jsonFile);
cloudExclusions = JsonSerializer.Deserialize<List<CloudExclusion>>(json);
}
}
}