Skip to content

Commit fb4b5d2

Browse files
Centralize config file search and schema version validation to avoid duplication
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
1 parent b976fa1 commit fb4b5d2

3 files changed

Lines changed: 60 additions & 64 deletions

File tree

DevProxy.Abstractions/Utils/ProxyUtils.cs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ public static void ValidateSchemaVersion(string schemaUrl, ILogger logger)
273273
return;
274274
}
275275

276+
var warning = GetSchemaVersionMismatchWarning(schemaUrl);
277+
if (warning is not null)
278+
{
279+
logger.LogWarning("{Warning}", warning);
280+
}
281+
}
282+
283+
/// <summary>
284+
/// Checks if the schema URL version matches the current Dev Proxy version.
285+
/// Returns a warning message if versions don't match, or null if they match
286+
/// or the schema URL cannot be parsed.
287+
/// </summary>
288+
public static string? GetSchemaVersionMismatchWarning(string schemaUrl)
289+
{
290+
if (string.IsNullOrWhiteSpace(schemaUrl))
291+
{
292+
return null;
293+
}
294+
276295
try
277296
{
278297
var uri = new Uri(schemaUrl);
@@ -285,18 +304,47 @@ public static void ValidateSchemaVersion(string schemaUrl, ILogger logger)
285304
if (CompareSemVer(currentVersion, schemaVersion) != 0)
286305
{
287306
var currentSchemaUrl = uri.ToString().Replace($"/v{schemaVersion}/", $"/v{currentVersion}/", StringComparison.OrdinalIgnoreCase);
288-
logger.LogWarning("The version of schema does not match the installed Dev Proxy version, the expected schema is {Schema}", currentSchemaUrl);
307+
return $"The version of schema does not match the installed Dev Proxy version, the expected schema is {currentSchemaUrl}";
289308
}
290309
}
291-
else
292-
{
293-
logger.LogDebug("Invalid schema {SchemaUrl}, skipping schema version validation.", schemaUrl);
294-
}
295310
}
296-
catch (Exception ex)
311+
catch
297312
{
298-
logger.LogWarning("Invalid schema {SchemaUrl}, skipping schema version validation. Error: {Error}", schemaUrl, ex.Message);
313+
// Ignore schema URL parsing errors
299314
}
315+
316+
return null;
317+
}
318+
319+
/// <summary>
320+
/// Returns the ordered list of config file paths to search.
321+
/// The first existing file in the list should be used.
322+
/// </summary>
323+
public static IEnumerable<string?> GetConfigFileCandidates(string? userConfigFile)
324+
{
325+
return [
326+
// config file specified by the user takes precedence
327+
// null if not specified
328+
userConfigFile,
329+
// current directory - JSON/JSONC files
330+
"devproxyrc.jsonc",
331+
"devproxyrc.json",
332+
// current directory - YAML files
333+
"devproxyrc.yaml",
334+
"devproxyrc.yml",
335+
// .devproxy subdirectory - JSON/JSONC files
336+
Path.Combine(".devproxy", "devproxyrc.jsonc"),
337+
Path.Combine(".devproxy", "devproxyrc.json"),
338+
// .devproxy subdirectory - YAML files
339+
Path.Combine(".devproxy", "devproxyrc.yaml"),
340+
Path.Combine(".devproxy", "devproxyrc.yml"),
341+
// app folder - JSON/JSONC files
342+
Path.Combine(AppFolder ?? "", "devproxyrc.jsonc"),
343+
Path.Combine(AppFolder ?? "", "devproxyrc.json"),
344+
// app folder - YAML files
345+
Path.Combine(AppFolder ?? "", "devproxyrc.yaml"),
346+
Path.Combine(AppFolder ?? "", "devproxyrc.yml")
347+
];
300348
}
301349

302350
/// <summary>

DevProxy/Commands/ConfigCommand.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,7 @@ private static string GetTargetFolderPath(string appFolder, string configId)
504504
return File.Exists(resolved) ? resolved : null;
505505
}
506506

507-
string?[] configFiles = [
508-
"devproxyrc.jsonc",
509-
"devproxyrc.json",
510-
Path.Combine(".devproxy", "devproxyrc.jsonc"),
511-
Path.Combine(".devproxy", "devproxyrc.json"),
512-
Path.Combine(ProxyUtils.AppFolder ?? "", "devproxyrc.jsonc"),
513-
Path.Combine(ProxyUtils.AppFolder ?? "", "devproxyrc.json")
514-
];
515-
516-
foreach (var configFile in configFiles)
507+
foreach (var configFile in ProxyUtils.GetConfigFileCandidates(null))
517508
{
518509
if (!string.IsNullOrEmpty(configFile) && File.Exists(configFile))
519510
{
@@ -626,29 +617,10 @@ private static async Task<int> ValidateConfigCoreAsync(
626617

627618
private static void ValidateSchemaVersion(string schemaUrl, List<ValidationMessage> warnings)
628619
{
629-
try
630-
{
631-
var uri = new Uri(schemaUrl);
632-
if (uri.Segments.Length > 2)
633-
{
634-
var schemaVersion = uri.Segments[^2]
635-
.TrimStart('v')
636-
.TrimEnd('/');
637-
var currentVersion = ProxyUtils.NormalizeVersion(ProxyUtils.ProductVersion);
638-
if (ProxyUtils.CompareSemVer(currentVersion, schemaVersion) != 0)
639-
{
640-
var currentSchemaUrl = uri.ToString().Replace(
641-
$"/v{schemaVersion}/",
642-
$"/v{currentVersion}/",
643-
StringComparison.OrdinalIgnoreCase);
644-
warnings.Add(new("$schema",
645-
$"Schema version does not match Dev Proxy version, expected schema: {currentSchemaUrl}"));
646-
}
647-
}
648-
}
649-
catch
620+
var warning = ProxyUtils.GetSchemaVersionMismatchWarning(schemaUrl);
621+
if (warning is not null)
650622
{
651-
// Ignore schema version parsing errors
623+
warnings.Add(new("$schema", warning));
652624
}
653625
}
654626

DevProxy/Extensions/ConfigurationManagerExtensions.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,7 @@ public static ConfigurationManager ConfigureDevProxyConfig(this ConfigurationMan
1616
configuration.Sources.Clear();
1717
_ = configuration.SetBasePath(Directory.GetCurrentDirectory());
1818

19-
string?[] configFiles = [
20-
// config file specified by the user takes precedence
21-
// null if not specified
22-
options.ConfigFile,
23-
// current directory - JSON/JSONC files
24-
"devproxyrc.jsonc",
25-
"devproxyrc.json",
26-
// current directory - YAML files
27-
"devproxyrc.yaml",
28-
"devproxyrc.yml",
29-
// .devproxy subdirectory - JSON/JSONC files
30-
Path.Combine(".devproxy", "devproxyrc.jsonc"),
31-
Path.Combine(".devproxy", "devproxyrc.json"),
32-
// .devproxy subdirectory - YAML files
33-
Path.Combine(".devproxy", "devproxyrc.yaml"),
34-
Path.Combine(".devproxy", "devproxyrc.yml"),
35-
// app folder - JSON/JSONC files
36-
Path.Combine(ProxyUtils.AppFolder ?? "", "devproxyrc.jsonc"),
37-
Path.Combine(ProxyUtils.AppFolder ?? "", "devproxyrc.json"),
38-
// app folder - YAML files
39-
Path.Combine(ProxyUtils.AppFolder ?? "", "devproxyrc.yaml"),
40-
Path.Combine(ProxyUtils.AppFolder ?? "", "devproxyrc.yml")
41-
];
42-
43-
foreach (var configFile in configFiles)
19+
foreach (var configFile in ProxyUtils.GetConfigFileCandidates(options.ConfigFile))
4420
{
4521
if (!string.IsNullOrEmpty(configFile) && File.Exists(configFile))
4622
{

0 commit comments

Comments
 (0)