Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Serialization always results in a Success object. #166

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Ardalis.Result/PagedInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Ardalis.Result
using System.Text.Json.Serialization;

namespace Ardalis.Result
{
public class PagedInfo
{
Expand All @@ -11,9 +13,13 @@ public PagedInfo(long pageNumber, long pageSize, long totalPages, long totalReco
TotalRecords = totalRecords;
}

[JsonInclude]
public long PageNumber { get; private set; }
[JsonInclude]
public long PageSize { get; private set; }
[JsonInclude]
public long TotalPages { get; private set; }
[JsonInclude]
public long TotalRecords { get; private set; }

public PagedInfo SetPageNumber(long pageNumber)
Expand Down
7 changes: 5 additions & 2 deletions src/Ardalis.Result/PagedResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Ardalis.Result
using System.Text.Json.Serialization;

namespace Ardalis.Result
{
public class PagedResult<T> : Result<T>
{
Expand All @@ -7,6 +9,7 @@ public PagedResult(PagedInfo pagedInfo, T value) : base(value)
PagedInfo = pagedInfo;
}

public PagedInfo PagedInfo { get; }
[JsonInclude]
public PagedInfo PagedInfo { get; init; }
}
}
8 changes: 7 additions & 1 deletion src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ protected Result(ResultStatus status)
ValidationErrors = result.ValidationErrors,
};

public T Value { get; }
[JsonInclude]
public T Value { get; init; }

[JsonIgnore]
public Type ValueType => typeof(T);
[JsonInclude]
public ResultStatus Status { get; protected set; } = ResultStatus.Ok;
public bool IsSuccess => Status == ResultStatus.Ok;
[JsonInclude]
public string SuccessMessage { get; protected set; } = string.Empty;
[JsonInclude]
public string CorrelationId { get; protected set; } = string.Empty;
[JsonInclude]
public IEnumerable<string> Errors { get; protected set; } = new List<string>();
[JsonInclude]
public List<ValidationError> ValidationErrors { get; protected set; } = new List<ValidationError>();

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions tests/Ardalis.Result.UnitTests/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public void ShouldDeserializeResultOfReferenceType()

Assert.Equivalent(expected, result);
}


[Fact]
public void ShouldDeserializeCorrectResultType()
{
var obj = Result.NotFound("NotFound");

var a = JsonSerializer.Serialize(obj);
var b = JsonSerializer.Deserialize<Result>(a);
var c = JsonSerializer.Serialize(b);

Assert.Equivalent(a, c);
}

private class Foo
{
Expand Down
Loading