Skip to content

Commit

Permalink
v18.2.0 - Json deserialization fails for PageOf<T> as a record
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Apr 11, 2024
1 parent c811ec8 commit 171f854
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
54 changes: 54 additions & 0 deletions InterlockLedger.Commons.NUnit.Tests/System/PageOfTJsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ******************************************************************************************************************************

#nullable enable

using System.Diagnostics;

using static System.ObjectExtensions;
using static Test.Helpers;

namespace System;

[TestFixture]
public class PageOfTJsonTests
{
[Test]
public void BidirectionalJsonOfPageOfT() {
var page = new PageOf<LimitedRange>([new LimitedRange(1, 10), new LimitedRange(100, 3)], 1, 5, 2, lastToFirst: true);
string json = JsonSerializer.Serialize(page, JsonSerializerOptions.Default);
TestContext.WriteLine(json);
var resultPage = JsonSerializer.Deserialize<PageOf<LimitedRange>>(json);
Assert.That(resultPage, Is.EqualTo(page));
}

}
4 changes: 2 additions & 2 deletions InterlockLedger.Commons/InterlockLedger.Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<Product>InterlockLedger</Product>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/interlockledger/interlockledger-commons.git</RepositoryUrl>
<Version>18.1.0</Version>
<Version>18.2.0</Version>
<PackageId>InterlockLedger.Commons</PackageId>
<PackageReleaseNotes>Fix initialization error in LimitedRange</PackageReleaseNotes>
<PackageReleaseNotes>Json deserialization fails for PageOf&lt;T&gt; as a record</PackageReleaseNotes>
<PackageIcon>il2.png</PackageIcon>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Commons/Types/System/LimitedRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public LimitedRange(ulong start, ushort count) {

public override int GetHashCode() => HashCode.Combine(End, Start, InvalidityCause);
public override bool Equals(object? obj) => obj is LimitedRange limitedRange && Equals(limitedRange);
public bool Equals(LimitedRange other) => End == other.End && Start == other.Start;
public bool Equals(LimitedRange other) => End == other.End && Start == other.Start && string.Equals(InvalidityCause, other.InvalidityCause, StringComparison.Ordinal);
public static bool operator ==(LimitedRange left, LimitedRange right) => left.Equals(right);
public static bool operator !=(LimitedRange left, LimitedRange right) => !(left == right);

Expand Down
25 changes: 23 additions & 2 deletions InterlockLedger.Commons/Types/System/PageOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,34 @@
//
// ******************************************************************************************************************************


namespace System;

#pragma warning disable CA1000 // Do not declare static members on generic types
public record PageOf<T>(IEnumerable<T> Items, ushort Page, byte PageSize, ushort TotalNumberOfPages, bool LastToFirst)
public class PageOf<T>(IEnumerable<T> items, ushort page, byte pageSize, ushort totalNumberOfPages, bool lastToFirst) : IEquatable<PageOf<T>>
{
public IEnumerable<T> Items { get; set; } = items;
public ushort Page { get; set; } = page;
public byte PageSize { get; set; } = pageSize;
public ushort TotalNumberOfPages { get; set; } = totalNumberOfPages;
public bool LastToFirst { get; set; } = lastToFirst;

public PageOf() : this([], 0, 0, 0, false) { }
public PageOf(IEnumerable<T> items, bool lastToFirst) : this(items.Required(), 0, 0, (ushort)(items.SafeAny() ? 1 : 0), lastToFirst) {
}

public static PageOf<T> Empty { get; } = new PageOf<T>([], 0, 0, 0, false);
public static PageOf<T> Empty { get; } = new PageOf<T>();

public override bool Equals(object? obj) => Equals(obj as PageOf<T>);
public bool Equals(PageOf<T>? other) =>
other is not null &&
other.Page == Page &&
other.PageSize == PageSize &&
other.LastToFirst == LastToFirst &&
other.TotalNumberOfPages == TotalNumberOfPages &&
other.Items.SequenceEqual(Items);
public override int GetHashCode() => HashCode.Combine(Items, Page, PageSize, TotalNumberOfPages, LastToFirst);

public static bool operator ==(PageOf<T>? left, PageOf<T>? right) => EqualityComparer<PageOf<T>>.Default.Equals(left, right);
public static bool operator !=(PageOf<T>? left, PageOf<T>? right) => !(left == right);
}

0 comments on commit 171f854

Please sign in to comment.