Skip to content

Commit

Permalink
Fix crash when unable to serialize test parameter (#448) (#450)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
TonEnfer and delatrie authored Mar 6, 2024
1 parent b8a1786 commit 846b97c
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions Allure.Net.Commons/Functions/FormatFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@
namespace Allure.Net.Commons.Functions;

/// <summary>
/// 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.
/// </summary>
public static class FormatFunctions
{
private static readonly JsonSerializerSettings SerializerSettings = new()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Error = (_, args) => { args.ErrorContext.Handled = true; }
};

/// <summary>
/// Formats a given value into a string. This is a shorthand for
/// <see cref="Format(object?, IReadOnlyDictionary{Type, ITypeFormatter})"/>
/// with empty formatters dictionary.
/// Formats a given value into a string. This is a shorthand for
/// <see cref="Format(object?, IReadOnlyDictionary{Type, ITypeFormatter})" />
/// with empty formatters dictionary.
/// </summary>
public static string Format(object? value)
{
return Format(value, new Dictionary<Type, ITypeFormatter>());
}

/// <summary>
/// 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
/// </summary>
public static string Format(
object? value,
Expand All @@ -37,6 +47,18 @@ IReadOnlyDictionary<Type, ITypeFormatter> 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();
}
}
}
}

0 comments on commit 846b97c

Please sign in to comment.