From ea362b0882650ec88b5347103ddee641ac52fec4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 07:02:55 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20Add=20Lenient=20System.Text=20StringEnum?= =?UTF-8?q?Converters=20for=20non-nullable=20Verwendungszweck+Gasqualit?= =?UTF-8?q?=C3=A4t=20(never=20really=20worked=20before!);=20Bump=20MSTest.?= =?UTF-8?q?TestAdapter=20from=203.5.2=20to=203.6.0=20(#527)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Bump MSTest.TestAdapter from 3.5.2 to 3.6.0 Bumps [MSTest.TestAdapter](https://github.com/microsoft/testfx) from 3.5.2 to 3.6.0. - [Release notes](https://github.com/microsoft/testfx/releases) - [Changelog](https://github.com/microsoft/testfx/blob/main/docs/Changelog.md) - [Commits](https://github.com/microsoft/testfx/compare/v3.5.2...v3.6.0) --- updated-dependencies: - dependency-name: MSTest.TestAdapter dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] * fix method name * shit never worked before! was für eine ätzende kombination von verschiedenen fehlern * more test coverage --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: konstantin Co-authored-by: Konstantin --- .../GasqualitaetStringEnumConverter.cs | 51 ++++- .../LenientJsonSerializerOptionsGenerator.cs | 2 + .../VerwendungszweckStringEnumConverter.cs | 58 ++++- BO4ETestProject/TestBO4E.csproj | 192 ++++++++-------- BO4ETestProject/TestStringEnumConverter.cs | 158 +++++++++----- .../TestBO4E.Extensions.csproj | 206 +++++++++--------- TestBO4E.Reporting/TestBO4E.Reporting.csproj | 62 +++--- 7 files changed, 444 insertions(+), 285 deletions(-) diff --git a/BO4E/meta/LenientConverters/GasqualitaetStringEnumConverter.cs b/BO4E/meta/LenientConverters/GasqualitaetStringEnumConverter.cs index b317e0e8..06fe4fe1 100644 --- a/BO4E/meta/LenientConverters/GasqualitaetStringEnumConverter.cs +++ b/BO4E/meta/LenientConverters/GasqualitaetStringEnumConverter.cs @@ -4,9 +4,10 @@ namespace BO4E.meta.LenientConverters; /// -/// converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS) +/// converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS). +/// For non-nullable see . /// -public class SystemTextGasqualitaetStringEnumConverter +public class SystemTextNullableGasqualitaetStringEnumConverter : System.Text.Json.Serialization.JsonConverter { /// @@ -57,6 +58,50 @@ System.Text.Json.JsonSerializerOptions options } } +/// +/// converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS) for non-nullable Gasqualitaet. +/// +/// +public class SystemTextGasqualitaetStringEnumConverter + : System.Text.Json.Serialization.JsonConverter +{ + /// + public override Gasqualitaet Read( + ref System.Text.Json.Utf8JsonReader reader, + Type typeToConvert, + System.Text.Json.JsonSerializerOptions options + ) + { + if (reader.TokenType == System.Text.Json.JsonTokenType.Number) + { + var integerValue = reader.GetInt64(); + return (Gasqualitaet)Enum.ToObject(typeof(Gasqualitaet), integerValue); + } + string enumString = reader.GetString(); + + return enumString?.ToUpper() switch + { + "HGAS" => Gasqualitaet.H_GAS, + "LGAS" => Gasqualitaet.L_GAS, + _ => Enum.TryParse(enumString?.ToUpper(), out Gasqualitaet result) + ? result + : throw new System.Text.Json.JsonException( + $"Invalid value for {typeToConvert}: {enumString}" + ), + }; + } + + /// + public override void Write( + System.Text.Json.Utf8JsonWriter writer, + Gasqualitaet value, + System.Text.Json.JsonSerializerOptions options + ) + { + writer.WriteStringValue(value.ToString()); + } +} + /// /// Converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS) /// @@ -70,7 +115,7 @@ System.Text.Json.JsonSerializerOptions options /// }; /// /// -/// +/// public class NewtonsoftGasqualitaetStringEnumConverter : Newtonsoft.Json.JsonConverter { diff --git a/BO4E/meta/LenientConverters/LenientJsonSerializerOptionsGenerator.cs b/BO4E/meta/LenientConverters/LenientJsonSerializerOptionsGenerator.cs index 35f6e5b5..f8a1deab 100644 --- a/BO4E/meta/LenientConverters/LenientJsonSerializerOptionsGenerator.cs +++ b/BO4E/meta/LenientConverters/LenientJsonSerializerOptionsGenerator.cs @@ -43,7 +43,9 @@ HashSet userPropertiesWhiteList settings.Converters.Add(new AutoNumberToStringConverter()); settings.Converters.Add(new VertragsConverter()); settings.Converters.Add(new EnergiemengeConverter()); + settings.Converters.Add(new SystemTextNullableVerwendungszweckStringEnumConverter()); settings.Converters.Add(new SystemTextVerwendungszweckStringEnumConverter()); + settings.Converters.Add(new SystemTextNullableGasqualitaetStringEnumConverter()); settings.Converters.Add(new SystemTextGasqualitaetStringEnumConverter()); settings.Converters.Add(new LenientSystemTextJsonStringToBoolConverter()); settings.Converters.Add(new StringNullableEnumConverter()); diff --git a/BO4E/meta/LenientConverters/VerwendungszweckStringEnumConverter.cs b/BO4E/meta/LenientConverters/VerwendungszweckStringEnumConverter.cs index f714c716..5edf3b9a 100644 --- a/BO4E/meta/LenientConverters/VerwendungszweckStringEnumConverter.cs +++ b/BO4E/meta/LenientConverters/VerwendungszweckStringEnumConverter.cs @@ -4,9 +4,10 @@ namespace BO4E.meta.LenientConverters; /// -/// Converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member . +/// Converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member for nullable Verwendungszweck. +/// for non-nullable Verwendungszweck. /// -public class SystemTextVerwendungszweckStringEnumConverter +public class SystemTextNullableVerwendungszweckStringEnumConverter : System.Text.Json.Serialization.JsonConverter { /// @@ -65,6 +66,57 @@ System.Text.Json.JsonSerializerOptions options } } +/// +/// Converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member for non-nullable Verwendungszweck. +/// See for nullable Verwendungszweck. +/// +public class SystemTextVerwendungszweckStringEnumConverter + : System.Text.Json.Serialization.JsonConverter +{ + /// + public override Verwendungszweck Read( + ref System.Text.Json.Utf8JsonReader reader, + Type typeToConvert, + System.Text.Json.JsonSerializerOptions options + ) + { + if (reader.TokenType == System.Text.Json.JsonTokenType.Number) + { + var integerValue = reader.GetInt64(); + return (Verwendungszweck)Enum.ToObject(typeof(Verwendungszweck), integerValue); + } + + if (reader.TokenType == System.Text.Json.JsonTokenType.String) + { + string enumString = reader.GetString(); + + return enumString switch + { + "MEHRMINDERMBENGENABRECHNUNG" => Verwendungszweck.MEHRMINDERMENGENABRECHNUNG, + _ => Enum.TryParse(enumString, true, out var result) + ? result + : throw new System.Text.Json.JsonException( + $"Invalid value for {typeToConvert.Name}: {enumString}" + ), + }; + } + + throw new System.Text.Json.JsonException( + $"Unexpected token parsing {typeToConvert.Name}. Expected String or Number, got {reader.TokenType}." + ); + } + + /// + public override void Write( + System.Text.Json.Utf8JsonWriter writer, + Verwendungszweck value, + System.Text.Json.JsonSerializerOptions options + ) + { + writer.WriteStringValue(value.ToString()); + } +} + /// /// converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member /// @@ -78,7 +130,7 @@ System.Text.Json.JsonSerializerOptions options /// }; /// /// -/// +/// public class NewtonsoftVerwendungszweckStringEnumConverter : Newtonsoft.Json.JsonConverter { diff --git a/BO4ETestProject/TestBO4E.csproj b/BO4ETestProject/TestBO4E.csproj index 9a02ee63..7b77302a 100644 --- a/BO4ETestProject/TestBO4E.csproj +++ b/BO4ETestProject/TestBO4E.csproj @@ -1,96 +1,96 @@ - - - - net8.0 - - false - - - - Library - - - - TestBO4E-dotnet - - 12 - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - - - - - - - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - - + + + + net8.0 + + false + + + + Library + + + + TestBO4E-dotnet + + 12 + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + + diff --git a/BO4ETestProject/TestStringEnumConverter.cs b/BO4ETestProject/TestStringEnumConverter.cs index 0289252d..8e984c75 100644 --- a/BO4ETestProject/TestStringEnumConverter.cs +++ b/BO4ETestProject/TestStringEnumConverter.cs @@ -107,19 +107,26 @@ public class ClassWithNullableGasqualitaet public class ClassWithNonNullableGasqualitaet { - public Gasqualitaet? Foo { get; set; } + public Gasqualitaet Foo { get; set; } } [TestMethod] - [DataRow("HGAS", Gasqualitaet.H_GAS)] - [DataRow("h_GAS", Gasqualitaet.H_GAS)] - [DataRow("H_GAS", Gasqualitaet.H_GAS)] - [DataRow("LGAS", Gasqualitaet.L_GAS)] - [DataRow("L_GAS", Gasqualitaet.L_GAS)] - [DataRow(null, null)] + [DataRow("HGAS", Gasqualitaet.H_GAS, false)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("LGAS", Gasqualitaet.L_GAS, false)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, false)] + [DataRow(null, null, false)] + [DataRow("HGAS", Gasqualitaet.H_GAS, true)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("LGAS", Gasqualitaet.L_GAS, true)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, true)] + [DataRow(null, null, true)] public void Test_System_Text_Gasqualitaet_Legacy_Converter_With_Nullable_Gasqualitaet( string? jsonValue, - Gasqualitaet? expectedGasqualitaet + Gasqualitaet? expectedGasqualitaet, + bool useMostLenient ) { string jsonString; @@ -132,10 +139,19 @@ public void Test_System_Text_Gasqualitaet_Legacy_Converter_With_Nullable_Gasqual jsonString = "{\"Foo\": null}"; } - var settings = new JsonSerializerOptions + JsonSerializerOptions settings; + if (useMostLenient) { - Converters = { new SystemTextGasqualitaetStringEnumConverter() }, - }; + settings = LenientParsing.MOST_LENIENT.GetJsonSerializerOptions(); + } + else + { + settings = new JsonSerializerOptions + { + Converters = { new SystemTextNullableGasqualitaetStringEnumConverter() }, + }; + } + var actual = System.Text.Json.JsonSerializer.Deserialize( jsonString, settings @@ -148,14 +164,20 @@ public void Test_System_Text_Gasqualitaet_Legacy_Converter_With_Nullable_Gasqual } [TestMethod] - [DataRow("HGAS", Gasqualitaet.H_GAS)] - [DataRow("h_GAS", Gasqualitaet.H_GAS)] - [DataRow("H_GAS", Gasqualitaet.H_GAS)] - [DataRow("LGAS", Gasqualitaet.L_GAS)] - [DataRow("L_GAS", Gasqualitaet.L_GAS)] + [DataRow("HGAS", Gasqualitaet.H_GAS, false)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("LGAS", Gasqualitaet.L_GAS, false)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, false)] + [DataRow("HGAS", Gasqualitaet.H_GAS, true)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("LGAS", Gasqualitaet.L_GAS, true)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, true)] public void Test_System_Text_Gasqualitaet_Legacy_Converter_With_Non_Nullable_Gasqualitaet( string? jsonValue, - Gasqualitaet expectedGasqualitaet + Gasqualitaet expectedGasqualitaet, + bool useMostLenient ) { string jsonString; @@ -167,10 +189,19 @@ Gasqualitaet expectedGasqualitaet { jsonString = "{\"Foo\": null}"; } - var settings = new JsonSerializerOptions + + JsonSerializerOptions settings; + if (useMostLenient) { - Converters = { new SystemTextGasqualitaetStringEnumConverter() }, - }; + settings = LenientParsing.MOST_LENIENT.GetJsonSerializerOptions(); + } + else + { + settings = new JsonSerializerOptions + { + Converters = { new SystemTextGasqualitaetStringEnumConverter() }, + }; + } var actual = System.Text.Json.JsonSerializer.Deserialize( jsonString, settings @@ -183,15 +214,22 @@ Gasqualitaet expectedGasqualitaet } [TestMethod] - [DataRow("HGAS", Gasqualitaet.H_GAS)] - [DataRow("H_GAS", Gasqualitaet.H_GAS)] - [DataRow("h_GAS", Gasqualitaet.H_GAS)] - [DataRow("LGAS", Gasqualitaet.L_GAS)] - [DataRow("L_GAS", Gasqualitaet.L_GAS)] - [DataRow(null, null)] + [DataRow("HGAS", Gasqualitaet.H_GAS, false)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("LGAS", Gasqualitaet.L_GAS, false)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, false)] + [DataRow(null, null, false)] + [DataRow("HGAS", Gasqualitaet.H_GAS, true)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("LGAS", Gasqualitaet.L_GAS, true)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, true)] + [DataRow(null, null, true)] public void Test_Newtonsoft_Gasqualitaet_Legacy_Converter_With_Nullable_Gasqualitaet( string? jsonValue, - Gasqualitaet? expectedGasqualitaet + Gasqualitaet? expectedGasqualitaet, + bool useMostLenient ) { string jsonString; @@ -204,14 +242,22 @@ public void Test_Newtonsoft_Gasqualitaet_Legacy_Converter_With_Nullable_Gasquali jsonString = "{\"Foo\": null}"; } - var settings = new Newtonsoft.Json.JsonSerializerSettings() + JsonSerializerSettings settings; + if (useMostLenient) { - Converters = + settings = LenientParsing.MOST_LENIENT.GetJsonSerializerSettings(); + } + else + { + settings = new Newtonsoft.Json.JsonSerializerSettings() { - new NewtonsoftGasqualitaetStringEnumConverter(), - new StringEnumConverter(), - }, - }; + Converters = + { + new NewtonsoftGasqualitaetStringEnumConverter(), + new StringEnumConverter(), + }, + }; + } var actual = Newtonsoft.Json.JsonConvert.DeserializeObject( jsonString, settings @@ -224,14 +270,20 @@ public void Test_Newtonsoft_Gasqualitaet_Legacy_Converter_With_Nullable_Gasquali } [TestMethod] - [DataRow("HGAS", Gasqualitaet.H_GAS)] - [DataRow("H_GAS", Gasqualitaet.H_GAS)] - [DataRow("h_GAS", Gasqualitaet.H_GAS)] - [DataRow("LGAS", Gasqualitaet.L_GAS)] - [DataRow("L_GAS", Gasqualitaet.L_GAS)] + [DataRow("HGAS", Gasqualitaet.H_GAS, true)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, true)] + [DataRow("LGAS", Gasqualitaet.L_GAS, true)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, true)] + [DataRow("HGAS", Gasqualitaet.H_GAS, false)] + [DataRow("H_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("h_GAS", Gasqualitaet.H_GAS, false)] + [DataRow("LGAS", Gasqualitaet.L_GAS, false)] + [DataRow("L_GAS", Gasqualitaet.L_GAS, false)] public void Test_Newtonsoft_Gasqualitaet_Legacy_Converter_With_Non_Nullable_Gasqualitaet( string? jsonValue, - Gasqualitaet expectedGasqualitaet + Gasqualitaet expectedGasqualitaet, + bool useMostLenient ) { string jsonString; @@ -243,15 +295,23 @@ Gasqualitaet expectedGasqualitaet { jsonString = "{\"Foo\": null}"; } - - var settings = new Newtonsoft.Json.JsonSerializerSettings() + JsonSerializerSettings settings; + if (useMostLenient) { - Converters = + settings = LenientParsing.MOST_LENIENT.GetJsonSerializerSettings(); + } + else + { + settings = new Newtonsoft.Json.JsonSerializerSettings() { - new NewtonsoftGasqualitaetStringEnumConverter(), - new StringEnumConverter(), - }, - }; + Converters = + { + new NewtonsoftGasqualitaetStringEnumConverter(), + new StringEnumConverter(), + }, + }; + } + var actual = Newtonsoft.Json.JsonConvert.DeserializeObject( jsonString, @@ -348,7 +408,7 @@ public void Test_System_Text_Verwendungszweck_Legacy_Converter_With_Nullable_Ver var settings = new JsonSerializerOptions { - Converters = { new SystemTextVerwendungszweckStringEnumConverter() }, + Converters = { new SystemTextNullableVerwendungszweckStringEnumConverter() }, }; var actual = System.Text.Json.JsonSerializer.Deserialize( jsonString, @@ -364,7 +424,7 @@ public void Test_System_Text_Verwendungszweck_Legacy_Converter_With_Nullable_Ver [TestMethod] [DataRow("MEHRMINDERMENGENABRECHNUNG", Verwendungszweck.MEHRMINDERMENGENABRECHNUNG)] [DataRow("MEHRMINDERMBENGENABRECHNUNG", Verwendungszweck.MEHRMINDERMENGENABRECHNUNG)] - public void Test_System_Text_Gasqualitaet_Legacy_Converter_With_Non_Nullable_Gasqualitaet( + public void Test_System_Text_Verwendungszweck_Legacy_Converter_With_Non_Nullable_Verwendungszweck( string? jsonValue, Verwendungszweck expectedVerwendungszweck ) @@ -477,7 +537,7 @@ Verwendungszweck expectedVerwendungszweck { Converters = { - new SystemTextVerwendungszweckStringEnumConverter(), + new SystemTextNullableVerwendungszweckStringEnumConverter(), new JsonStringEnumConverter(), }, }; diff --git a/TestBO4E.Extensions/TestBO4E.Extensions.csproj b/TestBO4E.Extensions/TestBO4E.Extensions.csproj index 9ce15d4a..3180594f 100644 --- a/TestBO4E.Extensions/TestBO4E.Extensions.csproj +++ b/TestBO4E.Extensions/TestBO4E.Extensions.csproj @@ -1,103 +1,103 @@ - - - - net8.0 - TestBO4E_dotnet_Extensions - - false - - 12 - - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - - - - - - - - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - - + + + + net8.0 + TestBO4E_dotnet_Extensions + + false + + 12 + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + + diff --git a/TestBO4E.Reporting/TestBO4E.Reporting.csproj b/TestBO4E.Reporting/TestBO4E.Reporting.csproj index f8ba7e84..bce8058f 100644 --- a/TestBO4E.Reporting/TestBO4E.Reporting.csproj +++ b/TestBO4E.Reporting/TestBO4E.Reporting.csproj @@ -1,31 +1,31 @@ - - - - net8.0 - TestBO4E_dotnet_Reporting - - false - - 12 - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - - - - - - - - - + + + + net8.0 + TestBO4E_dotnet_Reporting + + false + + 12 + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + +