From 54aa6907f92bc0506eb5160bf2937ef0df336c77 Mon Sep 17 00:00:00 2001 From: Jkorf Date: Thu, 6 Mar 2025 16:13:03 +0100 Subject: [PATCH] Some small fixes --- .../Authentication/ApiCredentials.cs | 13 +------------ .../Converters/SystemTextJson/EnumConverter.cs | 2 +- .../SystemTextJson/SerializationModel.cs | 2 +- .../Testing/Comparers/SystemTextJsonComparer.cs | 17 +++++++++++++---- .../Testing/SocketSubscriptionValidator.cs | 10 ++++++++-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/CryptoExchange.Net/Authentication/ApiCredentials.cs b/CryptoExchange.Net/Authentication/ApiCredentials.cs index 10a9187..3542b3b 100644 --- a/CryptoExchange.Net/Authentication/ApiCredentials.cs +++ b/CryptoExchange.Net/Authentication/ApiCredentials.cs @@ -35,20 +35,9 @@ namespace CryptoExchange.Net.Authentication /// /// The api key / label used for identification /// The api secret or private key used for signing - /// The type of credentials - public ApiCredentials(string key, string secret, ApiCredentialsType credentialType = ApiCredentialsType.Hmac) - : this(key, secret, null, credentialType) - { - } - - /// - /// Create Api credentials providing an api key, secret and pass for authentication - /// - /// The api key / label used for identification - /// The api secret or private key used for signing /// The api pass for the key. Not always needed /// The type of credentials - public ApiCredentials(string key, string secret, string? pass, ApiCredentialsType credentialType = ApiCredentialsType.Hmac) + public ApiCredentials(string key, string secret, string? pass = null, ApiCredentialsType credentialType = ApiCredentialsType.Hmac) { if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret)) throw new ArgumentException("Key and secret can't be null/empty"); diff --git a/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs b/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs index f2f511e..9c7a645 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs @@ -29,7 +29,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// /// /// - public static string? GetString(T enumValue) where T : struct, Enum + public static string GetString(T enumValue) where T : struct, Enum => EnumConverter.GetString(enumValue); /// diff --git a/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs b/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs index 7596e61..987d9c6 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs @@ -7,7 +7,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// /// Attribute to mark a model as json serializable. Used for AOT compilation. /// - [AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Enum)] + [AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Enum | System.AttributeTargets.Interface)] public class SerializationModelAttribute : Attribute { } diff --git a/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs b/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs index 32243a3..12b3695 100644 --- a/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs +++ b/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs @@ -131,7 +131,7 @@ namespace CryptoExchange.Net.Testing.Comparers } } } - else + else if (jsonObject.ValueKind == JsonValueKind.Object) { foreach (var item in jsonObject.EnumerateObject()) { @@ -144,6 +144,10 @@ namespace CryptoExchange.Net.Testing.Comparers //} } } + else + { + //? + } Debug.WriteLine($"Successfully validated {method}"); } @@ -366,8 +370,8 @@ namespace CryptoExchange.Net.Testing.Comparers var stringValue = jsonValue.GetString(); if (objectValue is decimal dec) { - if (decimal.Parse(stringValue!) != dec) - throw new Exception($"{method}: {property} not equal: {jsonValue.GetDecimal()} vs {dec}"); + if (decimal.Parse(stringValue!, CultureInfo.InvariantCulture) != dec) + throw new Exception($"{method}: {property} not equal: {stringValue} vs {dec}"); } else if (objectValue is DateTime time) { @@ -402,7 +406,12 @@ namespace CryptoExchange.Net.Testing.Comparers { // TODO enum comparing } - else if (value != Convert.ToInt64(objectValue)) + else if(objectValue is decimal dec) + { + if (dec != value) + throw new Exception($"{method}: {property} not equal: {dec} vs {value}"); + } + else if (value != Convert.ToInt64(objectValue, CultureInfo.InvariantCulture)) { throw new Exception($"{method}: {property} not equal: {value} vs {Convert.ToInt64(objectValue)}"); } diff --git a/CryptoExchange.Net/Testing/SocketSubscriptionValidator.cs b/CryptoExchange.Net/Testing/SocketSubscriptionValidator.cs index b856a6c..9434e04 100644 --- a/CryptoExchange.Net/Testing/SocketSubscriptionValidator.cs +++ b/CryptoExchange.Net/Testing/SocketSubscriptionValidator.cs @@ -120,7 +120,8 @@ namespace CryptoExchange.Net.Testing { // |x| values are used to replace parts or response messages overrideKey = item.Value.ToString(); - overrideValue = lastMessageJson.GetProperty(item.Name).GetString(); + var prop = lastMessageJson.GetProperty(item.Name); + overrideValue = prop.ValueKind == JsonValueKind.String ? prop.GetString() : prop.GetInt64().ToString(); } else if (item.Value.ToString() == "-999") { @@ -128,10 +129,15 @@ namespace CryptoExchange.Net.Testing overrideKey = item.Value.ToString(); overrideValue = lastMessageJson.GetProperty(item.Name).GetDecimal().ToString(); } - else if (lastMessageJson.GetProperty(item.Name).GetString() != item.Value.ToString() && ignoreProperties?.Contains(item.Name) != true) + else if (lastMessageJson.GetProperty(item.Name).ValueKind == JsonValueKind.String && lastMessageJson.GetProperty(item.Name).GetString() != item.Value.ToString() && ignoreProperties?.Contains(item.Name) != true) { throw new Exception($"{name} Expected {item.Name} to be {item.Value}, but was {lastMessageJson.GetProperty(item.Name).GetString()}"); } + else + { + // TODO check arrays and sub-objects + + } } // TODO check arrays and sub-objects