Skip to content

Commit

Permalink
Merge pull request #67 from MrDave1999/fix/issue-65
Browse files Browse the repository at this point in the history
fix!: Incorrect status is obtained when deserializing the result object
  • Loading branch information
MrDave1999 authored Mar 16, 2024
2 parents f4085aa + e55804a commit a221c5e
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 37 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="FluentValidation" Version="10.0.4" />
<PackageVersion Include="System.Text.Json" Version="4.6.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
<PackageVersion Include="Bogus" Version="34.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageVersion Include="FluentAssertions" Version="6.10.0" />
<PackageVersion Include="NUnit" Version="3.13.3" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.3.0" />
Expand Down
1 change: 1 addition & 0 deletions samples/SimpleResults.Example.AspNetCore/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using System.Net.Mime;
global using System.Text.Json.Serialization;
global using Bogus;
global using Microsoft.AspNetCore.Mvc;
global using Swashbuckle.AspNetCore.Annotations;
Expand Down
5 changes: 5 additions & 0 deletions samples/SimpleResults.Example.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
// Add filter for all controllers.
options.Filters.Add<TranslateResultToActionResultAttribute>();
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter<ResultStatus>());
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = (context) => context.ModelState.BadRequest();
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public async Task Post_WhenOrderIsCreated_ShouldReturnsHttpStatusCodeCreated(str
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Created);
}

[TestCase(Routes.Order.ManualValidation)]
Expand Down Expand Up @@ -95,5 +96,6 @@ public async Task Post_WhenModelIsInvalid_ShouldReturnsHttpStatusCodeBadRequest(
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEquivalentTo(expectedErrors);
result.Status.Should().Be(ResultStatus.Invalid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public async Task Post_WhenPersonIsCreated_ShouldReturnsHttpStatusCodeCreated(st
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Created);
}

[TestCase(Routes.Person.WebApi)]
Expand All @@ -52,5 +53,6 @@ public async Task Post_WhenPropertiesAreEmpty_ShouldReturnsHttpStatusCodeBadRequ
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().NotBeNullOrEmpty();
result.Status.Should().Be(ResultStatus.Invalid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public async Task Post_WhenUserIsCreated_ShouldReturnsHttpStatusCodeCreated(stri
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Created);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -46,5 +47,6 @@ public async Task Post_WhenNameIsEmpty_ShouldReturnsHttpStatusCodeBadRequest(str
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Invalid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public async Task Delete_WhenUserIsDeleted_ShouldReturnsHttpStatusCodeOk(string
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Ok);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -44,5 +45,6 @@ public async Task Delete_WhenUserIsNotFound_ShouldReturnsHttpStatusCodeNotFound(
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.NotFound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ public async Task Get_WhenFileNameIsEmpty_ShouldReturnsHttpStatusCodeBadRequest(
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Invalid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public async Task Get_WhenMessageIsBan_ShouldReturnsHttpStatusCodeForbidden(stri
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Forbidden);
}

[TestCase(Routes.Message.WebApi)]
Expand All @@ -42,6 +43,7 @@ public async Task Get_WhenMessageIsError_ShouldReturnsHttpStatusCodeServerError(
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.CriticalError);
}

[TestCase(Routes.Message.WebApi)]
Expand All @@ -63,5 +65,6 @@ public async Task Get_WhenMessageIsFalseIdentity_ShouldReturnsHttpStatusCodeUnau
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Unauthorized);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task Get_WhenPaginatedListIsObtained_ShouldReturnsHttpStatusCodeOk(
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Ok);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -48,6 +49,7 @@ public async Task Get_WhenNoResultsFound_ShouldReturnsHttpStatusCodeUnprocessabl
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Failure);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -72,5 +74,6 @@ public async Task Get_WhenPageNumberIsZero_ShouldReturnsHttpStatusCodeBadRequest
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Invalid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task Get_WhenPersonsAreObtained_ShouldReturnsHttpStatusCodeOk(strin
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Ok);
}

[TestCase(Routes.Person.WebApi)]
Expand All @@ -48,5 +49,6 @@ public async Task Get_WhenThereAreNoPersons_ShouldReturnsHttpStatusCodeUnprocess
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Failure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task Get_WhenUserIsObtained_ShouldReturnsHttpStatusCodeOk(string re
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Ok);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -46,5 +47,6 @@ public async Task Get_WhenUserIsNotFound_ShouldReturnsHttpStatusCodeNotFound(str
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.NotFound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public async Task Get_WhenUsersAreObtained_ShouldReturnsHttpStatusCodeOk(string
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Ok);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -46,5 +47,6 @@ public async Task Get_WhenThereAreNoUsers_ShouldReturnsHttpStatusCodeUnprocessab
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Failure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task Put_WhenUserIsUpdated_ShouldReturnsHttpStatusCodeOk(string req
result.IsSuccess.Should().BeTrue();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Ok);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -46,6 +47,7 @@ public async Task Put_WhenUserIsNotFound_ShouldReturnsHttpStatusCodeNotFound(str
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.NotFound);
}

[TestCase(Routes.User.WebApi)]
Expand All @@ -68,5 +70,6 @@ public async Task Put_WhenNameIsEmpty_ShouldReturnsHttpStatusCodeBadRequest(stri
result.IsSuccess.Should().BeFalse();
result.Message.Should().NotBeNullOrEmpty();
result.Errors.Should().BeEmpty();
result.Status.Should().Be(ResultStatus.Invalid);
}
}
20 changes: 10 additions & 10 deletions src/Core/ResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ namespace SimpleResults;
/// </summary>
public abstract class ResultBase
{
/// <summary>
/// Gets the current status of a result.
/// </summary>
/// <value>
/// The current status of a result.
/// Its default value is a <see cref="ResultStatus.Failure"/>.
/// </value>
[JsonConverter(typeof(JsonStringEnumConverter<ResultStatus>))]
public ResultStatus Status { get; init; } = ResultStatus.Failure;

/// <summary>
/// A value indicating that the result was successful.
/// </summary>
Expand Down Expand Up @@ -39,14 +49,4 @@ public abstract class ResultBase
/// Its default value is never a null value.
/// </value>
public IEnumerable<string> Errors { get; init; } = Enumerable.Empty<string>();

/// <summary>
/// Gets the current status of a result.
/// </summary>
/// <value>
/// The current status of a result.
/// Its default value is a <see cref="ResultStatus.Failure"/>.
/// </value>
[JsonIgnore]
public ResultStatus Status { get; init; } = ResultStatus.Failure;
}
12 changes: 6 additions & 6 deletions tests/Core/Json/SystemTextJson.ListedResultOfT.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void ListedResultOfT_ShouldSerializeResultOfValueType()
1,
2
],
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.Success}}",
"errors": []
Expand Down Expand Up @@ -51,6 +52,7 @@ public void ListedResultOfT_ShouldSerializeResultOfReferenceType()
"name": "Alice"
}
],
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.Success}}",
"errors": []
Expand Down Expand Up @@ -78,6 +80,7 @@ public void ListedResultOfT_ShouldDeserializeResultOfValueType()
1,
2
],
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.Success}}",
"errors": []
Expand All @@ -88,9 +91,7 @@ public void ListedResultOfT_ShouldDeserializeResultOfValueType()
var actual = JsonSerializer.Deserialize<ListedResult<int>>(json, options);

// Assert
actual
.Should()
.BeEquivalentTo(expectedResult, o => o.Excluding(r => r.Status));
actual.Should().BeEquivalentTo(expectedResult);
}

[Test]
Expand All @@ -115,6 +116,7 @@ public void ListedResultOfT_ShouldDeserializeResultOfReferenceType()
"name": "Alice"
}
],
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.Success}}",
"errors": []
Expand All @@ -125,8 +127,6 @@ public void ListedResultOfT_ShouldDeserializeResultOfReferenceType()
var actual = JsonSerializer.Deserialize<ListedResult<Person>>(json, options);

// Assert
actual
.Should()
.BeEquivalentTo(expectedResult, o => o.Excluding(r => r.Status));
actual.Should().BeEquivalentTo(expectedResult);
}
}
12 changes: 6 additions & 6 deletions tests/Core/Json/SystemTextJson.PagedResultOfT.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void PagedResultOfT_ShouldSerializeResultOfValueType()
"hasPrevious": false,
"hasNext": true
},
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.ObtainedResources}}",
"errors": []
Expand Down Expand Up @@ -70,6 +71,7 @@ public void PagedResultOfT_ShouldSerializeResultOfReferenceType()
"hasPrevious": false,
"hasNext": true
},
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.ObtainedResources}}",
"errors": []
Expand Down Expand Up @@ -107,6 +109,7 @@ public void PagedResultOfT_ShouldDeserializeResultOfValueType()
"hasPrevious": false,
"hasNext": true
},
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.ObtainedResources}}",
"errors": []
Expand All @@ -117,9 +120,7 @@ public void PagedResultOfT_ShouldDeserializeResultOfValueType()
var actual = JsonSerializer.Deserialize<PagedResult<int>>(json, options);

// Assert
actual
.Should()
.BeEquivalentTo(expectedResult, o => o.Excluding(r => r.Status));
actual.Should().BeEquivalentTo(expectedResult);
}

[Test]
Expand Down Expand Up @@ -153,6 +154,7 @@ public void PagedResultOfT_ShouldDeserializeResultOfReferenceType()
"hasPrevious": false,
"hasNext": true
},
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.ObtainedResources}}",
"errors": []
Expand All @@ -163,8 +165,6 @@ public void PagedResultOfT_ShouldDeserializeResultOfReferenceType()
var actual = JsonSerializer.Deserialize<PagedResult<Person>>(json, options);

// Assert
actual
.Should()
.BeEquivalentTo(expectedResult, o => o.Excluding(r => r.Status));
actual.Should().BeEquivalentTo(expectedResult);
}
}
6 changes: 3 additions & 3 deletions tests/Core/Json/SystemTextJson.Result.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public void Result_ShouldSerializeResultWithoutData()
var expectedJson =
$$"""
{
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.Success}}",
"errors": []
Expand All @@ -33,6 +34,7 @@ public void Result_ShouldDeserializeResultWithoutData()
var json =
$$"""
{
"status": "Ok",
"success": true,
"message": "{{ResponseMessages.Success}}",
"errors": []
Expand All @@ -43,8 +45,6 @@ public void Result_ShouldDeserializeResultWithoutData()
var actual = JsonSerializer.Deserialize<Result>(json, options);

// Assert
actual
.Should()
.BeEquivalentTo(expectedResult, o => o.Excluding(r => r.Status));
actual.Should().BeEquivalentTo(expectedResult);
}
}
Loading

0 comments on commit a221c5e

Please sign in to comment.