Custom serialization for ValidationProblemDetails to allign paths in custom and mvc error messages (#19111)

* Rowrked #18771 with a more brute force solution

* Fixes class name. Adds unit test verifying behaviour.

* Corrected test name.

---------

Co-authored-by: Kenn Jacobsen <kja@umbraco.dk>
Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
Sven Geusens
2025-04-23 10:17:18 +02:00
committed by GitHub
parent d568e981ab
commit 85b58f2015
3 changed files with 105 additions and 0 deletions

View File

@@ -52,6 +52,17 @@ public class BackOfficeSerializationTests
}
}
[Test]
public void Will_Serialize_ValidationProblemDetails_To_Casing_Aligned_With_Mvc()
{
var objectToSerialize = new TestValueWithValidationProblemDetail();
var json = JsonSerializer.Serialize(objectToSerialize, jsonOptions.JsonSerializerOptions);
var expectedJson = "{\"problemDetails\":{\"type\":\"Test type\",\"title\":\"Test title\",\"status\":400,\"detail\":\"Test detail\",\"instance\":\"Test instance\",\"errors\":[{\"$.testError1\":[\"Test error 1a\",\"Test error 1b\"]},{\"$.testError2\":[\"Test error 2a\"]},{\"$.testError3.testError3a\":[\"Test error 3b\"]}],\"traceId\":\"traceValue\"}}";
Assert.AreEqual(expectedJson, json);
}
private static NestedJsonTestValue CreateNestedObject(int levels)
{
var root = new NestedJsonTestValue { Level = 1 };
@@ -77,4 +88,27 @@ public class BackOfficeSerializationTests
public NestedJsonTestValue? Inner { get; set; }
}
private class TestValueWithValidationProblemDetail
{
public ValidationProblemDetails ProblemDetails { get; set; } = new()
{
Title = "Test title",
Detail = "Test detail",
Status = 400,
Type = "Test type",
Instance = "Test instance",
Extensions =
{
["traceId"] = "traceValue",
["someOtherExtension"] = "someOtherExtensionValue",
},
Errors =
{
["TestError1"] = ["Test error 1a", "Test error 1b"],
["TestError2"] = ["Test error 2a"],
["TestError3.TestError3a"] = ["Test error 3b"],
}
};
}
}