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