From 846b97cbf1d3d8fd68cd4575637d745a7aaecef1 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 6 Mar 2024 11:03:27 +0500 Subject: [PATCH] Fix crash when unable to serialize test parameter (#448) (#450) * Fixed a bug that caused a test to crash when there was a test parameter serialization error #448 * Fixed a bug that caused a test to crash when there was a test parameter serialization error #448 * Fixed a bug that caused a test to crash when there was a test parameter serialization error #448 * Fixed a bug that caused a test to crash when there was a test parameter serialization error #448 The serializer skips fields that contain loop references and fields that could not be serialized * Fixed a bug that caused a test to crash when there was a test parameter serialization error #448 Re-format code * Update Allure.Net.Commons/Functions/FormatFunctions.cs Co-authored-by: Maxim <17935127+delatrie@users.noreply.github.com> * Update FormatFunctions.cs * Fixed a bug that caused a test to crash when there was a test parameter serialization error #448 Fixed formatting during serialization Serialization parameters are moved to a private static field to improve performance --------- Co-authored-by: Maxim <17935127+delatrie@users.noreply.github.com> --- .../Functions/FormatFunctions.cs | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/Allure.Net.Commons/Functions/FormatFunctions.cs b/Allure.Net.Commons/Functions/FormatFunctions.cs index 8e360396..420ac8f3 100644 --- a/Allure.Net.Commons/Functions/FormatFunctions.cs +++ b/Allure.Net.Commons/Functions/FormatFunctions.cs @@ -7,15 +7,21 @@ namespace Allure.Net.Commons.Functions; /// -/// A set of functions to help with value-to-string conversion of test and -/// step arguments. +/// A set of functions to help with value-to-string conversion of test and +/// step arguments. /// public static class FormatFunctions { + private static readonly JsonSerializerSettings SerializerSettings = new() + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + Error = (_, args) => { args.ErrorContext.Handled = true; } + }; + /// - /// Formats a given value into a string. This is a shorthand for - /// - /// with empty formatters dictionary. + /// Formats a given value into a string. This is a shorthand for + /// + /// with empty formatters dictionary. /// public static string Format(object? value) { @@ -23,9 +29,13 @@ public static string Format(object? value) } /// - /// Formats a given value into a string. If the type of the value matches - /// a formater in the formatters dictionary, the formatter is used to - /// produce the result. Otherwise, the value is formatted as a JSON string. + /// Formats a given value into a string. If the type of the value matches + /// a formater in the formatters dictionary, the formatter is used to + /// produce the result. + /// Otherwise, the value is formatted as a JSON string or object.ToString() + /// if JSON serialization failed. + /// The serializer skips fields that contain loop references + /// and fields that could not be serialized /// public static string Format( object? value, @@ -37,6 +47,18 @@ IReadOnlyDictionary formatters return formatter.Format(value); } - return JsonConvert.SerializeObject(value); + if (value is null) + { + return "null"; + } + + try + { + return JsonConvert.SerializeObject(value, SerializerSettings); + } + catch + { + return value.ToString(); + } } -} +} \ No newline at end of file