Muokkaa

Jaa


Custom converters for serialization removed

ProblemDetails and ValidationProblemDetails previously used custom converters to support JSON serialization due to a lack of built-in support for the IgnoreNullValues option. Now that this option is supported by the System.Text.Json APIs, we've removed the custom converters from the framework in favor of the serialization provided by the framework.

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

Version introduced

ASP.NET Core 8.0 Preview 2

Previous behavior

Previously, you could add JsonStringEnumConverter to the serialization options as a custom converter, and deserialization resulted in a 400 status for ValidationProblemDetails.

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

Starting in .NET 8, the same code results in a null status for ValidationProblemDetails.

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

This change is a behavioral change.

Reason for change

Now that JsonSerializerOptions.IgnoreNullValues is supported by the System.Text.Json APIs, we've removed the custom converters in favor of the serialization provided by the framework.

Provide a JsonSerializerOptions with the correct details.

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

Affected APIs