aspnet / Announcements

Subscribe to this repo to be notified about major changes in ASP.NET Core and Entity Framework Core
Other
1.66k stars 80 forks source link

[Breaking change]: `ValidationProblemDetails` and `ProblemDetails` no longer use custom converters #504

Open captainsafia opened 1 year ago

captainsafia commented 1 year ago

Description

Prior to .NET 8 Preview 2, ValidationProblemDetails and ProblemDetails type used custom converters to support JSON serialization due to a lack of support for the IgnoreNullValues option. Now that this option is supported by the S.T.J APIs, we've removed the custom converters in the framework in favor of the serialization provided by the framework.

As a result of this, the properties in the ValidationProblemDetails and ProblemDetails types no longer indiscriminately assume lowercase type names. Developers must specific a JsonNamingPolicy to get the correct behavior.

Version

.NET 8 Preview 2

Previous behavior

string content = "{\"status\":400,\"detail\":\"HTTP egress is not enabled.\"}";
using MemoryStream stream = new();
using StreamWriter writer = new(stream);
writer.Write(content);
writer.Flush();
stream.Position = 0;

JsonSerializerOptions options = new();
options.Converters.Add(new JsonStringEnumConverter());

ValidationProblemDetails? details = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>(stream, options);
Console.WriteLine(details.Status); // 400

New behavior

string content = "{\"status\":400,\"detail\":\"HTTP egress is not enabled.\"}";
using MemoryStream stream = new();
using StreamWriter writer = new(stream);
writer.Write(content);
writer.Flush();
stream.Position = 0;

JsonSerializerOptions options = new();
options.Converters.Add(new JsonStringEnumConverter());

ValidationProblemDetails? details = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>(stream, options);
Console.WriteLine(details.Status); // null

Type of breaking change

Reason for change

Now that IgnoreNullValues is supported by the S.T.J APIs, we've removed the custom converters in the framework in favor of the serialization provided by the framework.

Recommended action

Provide a JSON serializer options with the correct details.

JsonSerializerOptions options = new()
{
   PropertyNameCaseInsensitive = true
};
ValidationProblemDetails? details = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>(stream, options);

Affected APIs