1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2026-04-07 02:01:12 +00:00

Updated Enum converter to only warn once per type for null/empty value for non-nullable enum property

This commit is contained in:
Jkorf 2026-03-30 11:34:02 +02:00
parent d6b680d42e
commit acca3468f3
3 changed files with 42 additions and 10 deletions

View File

@ -1,11 +1,15 @@
using CryptoExchange.Net.Attributes; using CryptoExchange.Net.Attributes;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Converters.SystemTextJson; using CryptoExchange.Net.Converters.SystemTextJson;
using System.Text.Json; using CryptoExchange.Net.Objects;
using CryptoExchange.Net.SharedApis;
using CryptoExchange.Net.Testing;
using Newtonsoft.Json.Linq;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.SharedApis;
namespace CryptoExchange.Net.UnitTests namespace CryptoExchange.Net.UnitTests
{ {
@ -185,6 +189,30 @@ namespace CryptoExchange.Net.UnitTests
Assert.That(result == expected); Assert.That(result == expected);
} }
[Test]
public void TestEnumConverterParseNullOnNonNullableOnlyLogsOnce()
{
LibraryHelpers.StaticLogger = new TraceLogger();
var listener = new EnumValueTraceListener();
Trace.Listeners.Add(listener);
try
{
Assert.Throws<Exception>(() =>
{
var result = JsonSerializer.Deserialize<NotNullableSTJEnumObject>("{\"Value\": null}", SerializerOptions.WithConverters(new SerializationContext()));
});
Assert.DoesNotThrow(() =>
{
var result2 = JsonSerializer.Deserialize<NotNullableSTJEnumObject>("{\"Value\": null}", SerializerOptions.WithConverters(new SerializationContext()));
});
}
finally
{
Trace.Listeners.Remove(listener);
}
}
[TestCase("1", true)] [TestCase("1", true)]
[TestCase("true", true)] [TestCase("true", true)]
[TestCase("yes", true)] [TestCase("yes", true)]

View File

@ -120,13 +120,14 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
/// <inheritdoc /> /// <inheritdoc />
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
var t = ReadNullable(ref reader, typeToConvert, options, out var isEmptyString); var t = ReadNullable(ref reader, typeToConvert, options, out var isEmptyStringOrNull);
if (t != null) if (t != null)
return t.Value; return t.Value;
if (isEmptyString && !_unknownValuesWarned.Contains(null)) if (isEmptyStringOrNull && !_unknownValuesWarned.Contains(null))
{ {
// We received an empty string and have no mapping for it, and the property isn't nullable // We received an empty string and have no mapping for it, and the property isn't nullable
_unknownValuesWarned.Add(null!);
LibraryHelpers.StaticLogger?.LogWarning($"Received null or empty enum value, but property type is not a nullable enum. EnumType: {typeof(T).FullName}. If you think {typeof(T).FullName} should be nullable please open an issue on the Github repo"); LibraryHelpers.StaticLogger?.LogWarning($"Received null or empty enum value, but property type is not a nullable enum. EnumType: {typeof(T).FullName}. If you think {typeof(T).FullName} should be nullable please open an issue on the Github repo");
} }
@ -149,9 +150,9 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
return (T)_undefinedEnumValue; return (T)_undefinedEnumValue;
} }
private T? ReadNullable(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, out bool isEmptyString) private T? ReadNullable(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, out bool isEmptyStringOrNull)
{ {
isEmptyString = false; isEmptyStringOrNull = false;
var enumType = typeof(T); var enumType = typeof(T);
if (_mappingToEnum == null) if (_mappingToEnum == null)
CreateMapping(); CreateMapping();
@ -167,13 +168,16 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
}; };
if (stringValue is null) if (stringValue is null)
{
isEmptyStringOrNull = true;
return null; return null;
}
if (!GetValue(enumType, stringValue, out var result)) if (!GetValue(enumType, stringValue, out var result))
{ {
if (string.IsNullOrWhiteSpace(stringValue)) if (string.IsNullOrWhiteSpace(stringValue))
{ {
isEmptyString = true; isEmptyStringOrNull = true;
} }
else else
{ {

View File

@ -13,7 +13,7 @@ namespace CryptoExchange.Net.Testing
if (message.Contains("Cannot map")) if (message.Contains("Cannot map"))
throw new Exception("Enum value error: " + message); throw new Exception("Enum value error: " + message);
if (message.Contains("Received null enum value")) if (message.Contains("Received null or empty enum value"))
throw new Exception("Enum null error: " + message); throw new Exception("Enum null error: " + message);
} }
@ -25,7 +25,7 @@ namespace CryptoExchange.Net.Testing
if (message.Contains("Cannot map")) if (message.Contains("Cannot map"))
throw new Exception("Enum value error: " + message); throw new Exception("Enum value error: " + message);
if (message.Contains("Received null enum value")) if (message.Contains("Received null or empty enum value"))
throw new Exception("Enum null error: " + message); throw new Exception("Enum null error: " + message);
} }
} }