99using Newtonsoft . Json . Schema ;
1010using System . Collections . ObjectModel ;
1111using System . Reflection ;
12+ using System . Text . Encodings . Web ;
1213using System . Text . Json ;
1314using System . Text . Json . Serialization ;
1415using System . Text . RegularExpressions ;
@@ -44,6 +45,7 @@ public static class ProxyUtils
4445 public static JsonSerializerOptions JsonSerializerOptions { get ; } = new ( )
4546 {
4647 DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
48+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping ,
4749 PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
4850 PropertyNameCaseInsensitive = true ,
4951 ReadCommentHandling = JsonCommentHandling . Skip ,
@@ -273,6 +275,25 @@ public static void ValidateSchemaVersion(string schemaUrl, ILogger logger)
273275 return ;
274276 }
275277
278+ var warning = GetSchemaVersionMismatchWarning ( schemaUrl ) ;
279+ if ( warning is not null )
280+ {
281+ logger . LogWarning ( "{Warning}" , warning ) ;
282+ }
283+ }
284+
285+ /// <summary>
286+ /// Checks if the schema URL version matches the current Dev Proxy version.
287+ /// Returns a warning message if versions don't match, or null if they match
288+ /// or the schema URL cannot be parsed.
289+ /// </summary>
290+ public static string ? GetSchemaVersionMismatchWarning ( string schemaUrl )
291+ {
292+ if ( string . IsNullOrWhiteSpace ( schemaUrl ) )
293+ {
294+ return null ;
295+ }
296+
276297 try
277298 {
278299 var uri = new Uri ( schemaUrl ) ;
@@ -285,18 +306,47 @@ public static void ValidateSchemaVersion(string schemaUrl, ILogger logger)
285306 if ( CompareSemVer ( currentVersion , schemaVersion ) != 0 )
286307 {
287308 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 ) ;
309+ return $ "The version of schema does not match the installed Dev Proxy version, the expected schema is { currentSchemaUrl } " ;
289310 }
290311 }
291- else
292- {
293- logger . LogDebug ( "Invalid schema {SchemaUrl}, skipping schema version validation." , schemaUrl ) ;
294- }
295312 }
296- catch ( Exception ex )
313+ catch
297314 {
298- logger . LogWarning ( "Invalid schema {SchemaUrl}, skipping schema version validation. Error: {Error}" , schemaUrl , ex . Message ) ;
315+ return $ "The $ schema value ' { schemaUrl } ' is not a valid URL. Schema version could not be validated." ;
299316 }
317+
318+ return null ;
319+ }
320+
321+ /// <summary>
322+ /// Returns the ordered list of config file paths to search.
323+ /// The first existing file in the list should be used.
324+ /// </summary>
325+ public static IEnumerable < string ? > GetConfigFileCandidates ( string ? userConfigFile )
326+ {
327+ return [
328+ // config file specified by the user takes precedence
329+ // null if not specified
330+ userConfigFile ,
331+ // current directory - JSON/JSONC files
332+ "devproxyrc.jsonc" ,
333+ "devproxyrc.json" ,
334+ // current directory - YAML files
335+ "devproxyrc.yaml" ,
336+ "devproxyrc.yml" ,
337+ // .devproxy subdirectory - JSON/JSONC files
338+ Path . Combine ( ".devproxy" , "devproxyrc.jsonc" ) ,
339+ Path . Combine ( ".devproxy" , "devproxyrc.json" ) ,
340+ // .devproxy subdirectory - YAML files
341+ Path . Combine ( ".devproxy" , "devproxyrc.yaml" ) ,
342+ Path . Combine ( ".devproxy" , "devproxyrc.yml" ) ,
343+ // app folder - JSON/JSONC files
344+ Path . Combine ( AppFolder ?? "" , "devproxyrc.jsonc" ) ,
345+ Path . Combine ( AppFolder ?? "" , "devproxyrc.json" ) ,
346+ // app folder - YAML files
347+ Path . Combine ( AppFolder ?? "" , "devproxyrc.yaml" ) ,
348+ Path . Combine ( AppFolder ?? "" , "devproxyrc.yml" )
349+ ] ;
300350 }
301351
302352 /// <summary>
0 commit comments