Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: json deserialization issue of ValidationResult #1935

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/FluentValidation.Tests/JsonSerializationTests.cs
@@ -0,0 +1,33 @@
namespace FluentValidation.Tests {
using System.Text.Json;
using Results;
using Xunit;

public class JsonSerializationTests {
[Fact]
public void Consecutive_Serialize_Deserialization() {
var validationResult = new ValidationResult {
Errors = {
new ValidationFailure("MyProperty1", "Invalid MyProperty1"),
new ValidationFailure("MyProperty2", "Invalid MyProperty2"),
new ValidationFailure("MyProperty3", "Invalid MyProperty3")
}
};
// System.Text.Json
var serialized1 = JsonSerializer.Serialize(validationResult);
var deserialized1 = JsonSerializer.Deserialize<ValidationResult>(serialized1);

// Newtonsoft.Json
var serialized2 = Newtonsoft.Json.JsonConvert.SerializeObject(validationResult);
var deserialized2 = Newtonsoft.Json.JsonConvert.DeserializeObject<ValidationResult>(serialized2);

Assert.NotNull(deserialized1);
Assert.Equal(deserialized1.IsValid, deserialized2.IsValid);
Assert.Equal(deserialized1.Errors.Count, deserialized2.Errors.Count);
for (var i = 0; i < deserialized1.Errors.Count; i++) {
Assert.Equal(deserialized1.Errors[i].PropertyName, deserialized2.Errors[i].PropertyName);
Assert.Equal(deserialized1.Errors[i].ErrorMessage, deserialized2.Errors[i].ErrorMessage);
}
}
}
}
1 change: 1 addition & 0 deletions src/FluentValidation/FluentValidation.csproj
Expand Up @@ -24,5 +24,6 @@ Full release notes can be found at https://github.com/FluentValidation/FluentVal
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="System.Text.Json" Version="6.0.3" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/FluentValidation/Results/ValidationFailure.cs
Expand Up @@ -19,6 +19,7 @@
namespace FluentValidation.Results {
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

/// <summary>
/// Defines a validation failure
Expand All @@ -38,6 +39,7 @@ public class ValidationFailure {
/// <summary>
/// Creates a new ValidationFailure.
/// </summary>
[JsonConstructor]
public ValidationFailure(string propertyName, string errorMessage, object attemptedValue) {
PropertyName = propertyName;
ErrorMessage = errorMessage;
Expand Down
10 changes: 10 additions & 0 deletions src/FluentValidation/Results/ValidationResult.cs
Expand Up @@ -20,6 +20,7 @@ namespace FluentValidation.Results {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

/// <summary>
/// The result of running a validator
Expand Down Expand Up @@ -47,6 +48,15 @@ public class ValidationResult {
_errors = new List<ValidationFailure>();
}

/// <summary>
/// Helps System.Text.Json to deserialize jsons correctly. This constructor cannot be called for other purposes.
/// </summary>
[Obsolete]
[JsonConstructor]
public ValidationResult(List<ValidationFailure> errors , bool isValid) {
_errors = errors;
}

/// <summary>
/// Creates a new ValidationResult from a collection of failures
/// </summary>
Expand Down