Skip to content

Commit

Permalink
Merge pull request #11 from RobinTTY/feature/allow-mocking-of-nordige…
Browse files Browse the repository at this point in the history
…n-api-response

Allow mocking of NordigenApiResponse. Resolves #10
  • Loading branch information
RobinTTY authored Feb 20, 2024
2 parents dbe0724 + 429a23d commit 574a712
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@

namespace RobinTTY.NordigenApiClient.JsonConverters;

internal class SingleOrArrayConverter<TCollection, TItem> : JsonConverter<TCollection>
where TCollection : class, ICollection<TItem>, new()
internal class SingleOrArrayConverter<TEnumerable, TItem> : JsonConverter<TEnumerable>
where TEnumerable : IEnumerable<TItem>
{
public override TCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override TEnumerable? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.Null:
return null;
return (TEnumerable) Enumerable.Empty<TItem>();
case JsonTokenType.StartArray:
var list = new TCollection();
var list = new List<TItem>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray) break;
var listItem = JsonSerializer.Deserialize<TItem>(ref reader, options);
if (listItem != null) list.Add(listItem);
}
return list;
return (TEnumerable) (IEnumerable<TItem>) list;
default:
var item = JsonSerializer.Deserialize<TItem>(ref reader, options);
return item != null ? new TCollection {item} : null;
return item != null
? (TEnumerable) (IEnumerable<TItem>) new List<TItem> {item}
: (TEnumerable) Enumerable.Empty<TItem>();
}
}

public override void Write(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, TEnumerable value, JsonSerializerOptions options)
{
if (value.Count == 1)
if (value.Count() == 1)
{
JsonSerializer.Serialize(writer, value.First(), options);
}
else
{
writer.WriteStartArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class NordigenApiResponse<TResult, TError> where TResult : class where TE
/// <param name="isSuccess">Indicates whether the HTTP response was successful.</param>
/// <param name="result">The result returned by the API. Null if the the HTTP response was not successful.</param>
/// <param name="apiError">The error returned by the API. Null if the HTTP response was successful.</param>
internal NordigenApiResponse(HttpStatusCode statusCode, bool isSuccess, TResult? result, TError? apiError)
public NordigenApiResponse(HttpStatusCode statusCode, bool isSuccess, TResult? result, TError? apiError)
{
StatusCode = statusCode;
IsSuccess = isSuccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ public class Transaction
/// Array of the report exchange rate.
/// </summary>
[JsonPropertyName("currencyExchange")]
[JsonConverter(typeof(SingleOrArrayConverter<List<CurrencyExchange>, CurrencyExchange>))]
public List<CurrencyExchange>? CurrencyExchange { get; }
[JsonConverter(typeof(SingleOrArrayConverter<IEnumerable<CurrencyExchange>, CurrencyExchange>))]
public IEnumerable<CurrencyExchange>? CurrencyExchange { get; }

/// <summary>
/// The identification of the transaction as used for reference by the financial institution.
Expand Down Expand Up @@ -345,7 +345,7 @@ public Transaction(string? transactionId, string? debtorName, MinimalBankAccount
DateTime? valueDateTime, string? remittanceInformationStructured,
IEnumerable<string>? remittanceInformationStructuredArray, string? additionalInformation,
string? additionalInformationStructured, Balance? balanceAfterTransaction, string? checkId,
List<CurrencyExchange>? currencyExchange, string? entryReference, string? internalTransactionId,
IEnumerable<CurrencyExchange>? currencyExchange, string? entryReference, string? internalTransactionId,
string? merchantCategoryCode, DateTime? bookingDateTime)
{
TransactionId = transactionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageTags>Nordigen; API; client</PackageTags>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/release-notes.txt"))</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>6.1.2</Version>
<Version>6.1.3</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/RobinTTY.NordigenApiClient/release-notes.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Fixed CurrencyExchange deserialization.
- Fixed CurrencyExchange deserialization caused by inconsistent API behavior
- Allow mocking of NordigenApiResponse

0 comments on commit 574a712

Please sign in to comment.