From 47f447a1e78a44ebcae6b0596d9c3bc58b84dee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Tue, 6 Aug 2024 14:12:02 +0200 Subject: [PATCH] Make string to enum conversion case insensitive All other enum parsing performed in the ConfigurationReader is also case insensitive. Fixes #431 --- .../Settings/Configuration/StringArgumentValue.cs | 2 +- .../StringArgumentValueTests.cs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs index 2ba8dcc..15e3fec 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs @@ -51,7 +51,7 @@ public StringArgumentValue(string providedValue) } if (toTypeInfo.IsEnum) - return Enum.Parse(toType, argumentValue); + return Enum.Parse(toType, argumentValue, ignoreCase: true); var convertor = ExtendedTypeConversions .Where(t => t.Key.GetTypeInfo().IsAssignableFrom(toTypeInfo)) diff --git a/test/Serilog.Settings.Configuration.Tests/StringArgumentValueTests.cs b/test/Serilog.Settings.Configuration.Tests/StringArgumentValueTests.cs index 3a312ba..be172cd 100644 --- a/test/Serilog.Settings.Configuration.Tests/StringArgumentValueTests.cs +++ b/test/Serilog.Settings.Configuration.Tests/StringArgumentValueTests.cs @@ -222,10 +222,12 @@ public void ReferencingUndeclaredLevelSwitchThrows() Assert.Contains("\"LevelSwitches\":{\"$mySwitch\":", ex.Message); } - [Fact] - public void StringValuesConvertToEnumByName() + [Theory] + [InlineData("Information")] + [InlineData("information")] + public void StringValuesConvertToEnumByName(string level) { - var value = new StringArgumentValue(nameof(LogEventLevel.Information)); + var value = new StringArgumentValue(level); var actual = value.ConvertTo(typeof(LogEventLevel), new());