From fcd419993a2fd1616f4720937db127589c4c5afb Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 19 May 2021 23:09:43 +0200 Subject: [PATCH] Handling type validation on config edits --- Kyoo/Controllers/ConfigurationManager.cs | 7 ++++--- Kyoo/Program.cs | 7 ++++++- Kyoo/Views/ConfigurationApi.cs | 4 ++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Kyoo/Controllers/ConfigurationManager.cs b/Kyoo/Controllers/ConfigurationManager.cs index 38710017..42bdf433 100644 --- a/Kyoo/Controllers/ConfigurationManager.cs +++ b/Kyoo/Controllers/ConfigurationManager.cs @@ -41,14 +41,15 @@ namespace Kyoo.Controllers path = path.Replace("__", ":"); if (!_references.TryGetValue(path, out Type type)) throw new ItemNotFoundException($"No configuration exists for the name: {path}"); + value = JObject.FromObject(value).ToObject(type); + if (value == null) + throw new ArgumentException("Invalid value format."); ExpandoObject config = ToObject(_configuration); IDictionary configDic = config; - // TODO validate the type configDic[path] = value; JObject obj = JObject.FromObject(config); - // TODO allow path to change - await using StreamWriter writer = new("settings.json"); + await using StreamWriter writer = new(Program.JsonConfigPath); await writer.WriteAsync(obj.ToString()); } diff --git a/Kyoo/Program.cs b/Kyoo/Program.cs index 16b6d098..32d763fd 100644 --- a/Kyoo/Program.cs +++ b/Kyoo/Program.cs @@ -15,6 +15,11 @@ namespace Kyoo /// public static class Program { + /// + /// The path of the json configuration of the application. + /// + public const string JsonConfigPath = "./settings.json"; + /// /// Main function of the program /// @@ -66,7 +71,7 @@ namespace Kyoo /// The modified configuration builder private static IConfigurationBuilder SetupConfig(IConfigurationBuilder builder, string[] args) { - return builder.AddJsonFile("./settings.json", false, true) + return builder.AddJsonFile(JsonConfigPath, false, true) .AddEnvironmentVariables() .AddCommandLine(args); } diff --git a/Kyoo/Views/ConfigurationApi.cs b/Kyoo/Views/ConfigurationApi.cs index 71697551..e8829e3b 100644 --- a/Kyoo/Views/ConfigurationApi.cs +++ b/Kyoo/Views/ConfigurationApi.cs @@ -87,6 +87,10 @@ namespace Kyoo.Api { return NotFound(); } + catch (ArgumentException) + { + return BadRequest(); + } } } } \ No newline at end of file