Skip to content

Commit

Permalink
Refactor CurrencyExchange fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Feb 19, 2024
1 parent 6131c69 commit 4fcc657
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
Binary file removed NordigenClientModified.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,48 @@ namespace RobinTTY.NordigenApiClient.Tests.Serialization;
internal class TransactionTests
{
/// <summary>
/// Tests the correct deserialization of transactions.
/// Tests the correct deserialization of transactions with a single embedded currency exchange object.
/// </summary>
[Test]
public void DeserializeTransaction()
public void DeserializeTransactionWithSingleCurrencyExchange()
{
const string json =
"{ \"transactionId\": \"AB123456789\", \"entryReference\": \"123456789\", \"bookingDate\": \"2023-03-20\", \"bookingDateTime\": \"2023-03-20T00:00:00+00:00\", \"transactionAmount\": { \"amount\": \"-33.06\", \"currency\": \"GBP\" }, \"currencyExchange\": { \"sourceCurrency\": \"USD\", \"exchangeRate\": \"1.20961887\", \"unitCurrency\": \"USD\", \"targetCurrency\": \"GBP\", \"instructedAmount\": { \"amount\": \"-33.06\", \"currency\": \"GBP\" } }, \"remittanceInformationUnstructured\": \"my reference here\", \"additionalInformation\": \"123456789\", \"proprietaryBankTransactionCode\": \"OTHER_PURCHASE\", \"merchantCategoryCode\": \"5045\", \"internalTransactionId\": \"abcdef\" }";

var options = new JsonSerializerOptions
{
Converters = {new CultureSpecificDecimalConverter()}
};
var transaction = JsonSerializer.Deserialize<Transaction>(json, options);

Assert.Multiple(() =>
{
Assert.That(transaction!.CurrencyExchange, Is.Not.Null);
Assert.That(transaction.CurrencyExchange!.First().ExchangeRate, Is.EqualTo(1.20961887));
Assert.That(transaction.CurrencyExchange!.First().InstructedAmount!.Amount, Is.EqualTo(-33.06));
Assert.That(transaction.CurrencyExchange!.First().InstructedAmount!.Currency, Is.EqualTo("GBP"));
Assert.That(transaction.CurrencyExchange!.First().SourceCurrency, Is.EqualTo("USD"));
Assert.That(transaction.CurrencyExchange!.First().TargetCurrency, Is.EqualTo("GBP"));
Assert.That(transaction.CurrencyExchange!.First().UnitCurrency, Is.EqualTo("USD"));
Assert.That(transaction.CurrencyExchange!.First().QuotationDate, Is.Null);
});
}

/// <summary>
/// Tests the correct deserialization of transactions with multiple embedded currency exchange objects.
/// </summary>
[Test]
public void DeserializeTransactionWithMultipleCurrencyExchange()
{
const string json =
"{ \"transactionId\": \"AB123456789\", \"entryReference\": \"123456789\", \"bookingDate\": \"2023-03-20\", \"bookingDateTime\": \"2023-03-20T00:00:00+00:00\", \"transactionAmount\": { \"amount\": \"-33.06\", \"currency\": \"GBP\" }, \"currencyExchange\":[{\"sourceCurrency\":\"USD\",\"exchangeRate\":\"1.20961887\",\"unitCurrency\":\"USD\",\"targetCurrency\":\"GBP\"}], \"remittanceInformationUnstructured\": \"my reference here\", \"additionalInformation\": \"123456789\", \"proprietaryBankTransactionCode\": \"OTHER_PURCHASE\", \"merchantCategoryCode\": \"5045\", \"internalTransactionId\": \"abcdef\" }";

// We need the culture specific decimal converter here, since it it accepting strings
var options = new JsonSerializerOptions
{
Converters = {new JsonWebTokenConverter(), new GuidConverter(), new CultureSpecificDecimalConverter()}
Converters = { new CultureSpecificDecimalConverter() }
};
var transaction = JsonSerializer.Deserialize<Transaction>(json, options);

Assert.Multiple(() =>
{
Assert.That(transaction!.CurrencyExchange, Is.Not.Null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace RobinTTY.NordigenApiClient.JsonConverters;

internal class SingleOrArrayConverter<TCollection, TItem> : JsonConverter<TCollection> where TCollection : class, ICollection<TItem>, new()
internal class SingleOrArrayConverter<TCollection, TItem> : JsonConverter<TCollection>
where TCollection : class, ICollection<TItem>, new()
{
public override TCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand All @@ -15,27 +16,21 @@ namespace RobinTTY.NordigenApiClient.JsonConverters;
var list = new TCollection();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
break;
if (reader.TokenType == JsonTokenType.EndArray) break;
var listItem = JsonSerializer.Deserialize<TItem>(ref reader, options);
if (listItem != null)
{
list.Add(listItem);
}
if (listItem != null) list.Add(listItem);
}
return list;
default:
var item = JsonSerializer.Deserialize<TItem>(ref reader, options);
return item != null ? new TCollection { item } : null;
return item != null ? new TCollection {item} : null;
}
}

public override void Write(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options)
{
if (value.Count == 1)
{
JsonSerializer.Serialize(writer, value.First(), options);
}
else
{
writer.WriteStartArray();
Expand All @@ -44,4 +39,4 @@ public override void Write(Utf8JsonWriter writer, TCollection value, JsonSeriali
writer.WriteEndArray();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace RobinTTY.NordigenApiClient.Models.Responses;
/// <summary>
/// Pair representing an amount and the currency the amount is denominated in.
/// </summary>
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public class AmountCurrencyPair
{
/// <summary>
Expand All @@ -31,4 +30,4 @@ public AmountCurrencyPair(decimal amount, string currency)
Amount = amount;
Currency = currency;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace RobinTTY.NordigenApiClient.Models.Responses;
/// <summary>
/// Detailed information about a currency exchange.
/// </summary>
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public class CurrencyExchange
{
/// <summary>
Expand Down

0 comments on commit 4fcc657

Please sign in to comment.