My ASP.NET Core 7 action correctly interprets the type discriminator when deserializing a polymorphic request body.
But it doesn't include the discriminator when serializing a polymorphic response.
Interestingly, the System.Text.Json.JsonSerializer correctly includes the discriminator if I serialize it myself.
So what do I need to do to get ASP.NET Core to include the discriminator in the response?
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
[JsonDerivedType(typeof(Foo), "Foo")]
[JsonDerivedType(typeof(Bar), "Bar")]
public abstract class Base { }
public class Foo : Base
{
public string? Name { get; set; }
}
public class Bar : Base
{
public string? Name { get; set; }
}
public class Controller : ControllerBase
{
[HttpPost("")]
public Base Action(Base request)
{
return request;
}
}
A request with a discriminator like this is deserialized correctly:
{
"$type": "Bar"
}
But the response from the code above doesn't include the expected discriminator...
{
"name": null
}
As written in the suggested duplicate - the type which ASP.NET Core resolves for
Actionwould be the derived one which does not have any polymorphism info, so it will not add any polymorphic serialization data.To provide appropriate workaround in your case - you can resolve the issue by either manually copying corresponding attributes to the derived types:
Or by extending the type info provider to copy the needed info from the base one. Simple implementation to get you started (can be improved to automatically analyze the inheritance tree with reflection and copy needed data, for example):
And set it as
JsonSerializerOptions.TypeInfoResolver. For example:Also check out this github issue.