Skip to content

Commit

Permalink
Fix Serialization always results in a Success object. (#166)
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Smith <[email protected]>
  • Loading branch information
MischaWeerwag and ardalis authored Feb 28, 2024
1 parent 9b845a2 commit 9bfa4f8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
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

0 comments on commit 9bfa4f8

Please sign in to comment.