Announcements icon indicating copy to clipboard operation
Announcements copied to clipboard

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

Open captainsafia opened this issue 2 years ago • 0 comments

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

  • [ ] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • [ ] Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • [X] Behavioral change: Existing binaries may behave differently at run time.

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

captainsafia avatar Apr 10 '23 20:04 captainsafia