Open
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I am using polymorphic JSON which works when returning the class but not when returning Ok(data)
because then the $type
type discriminator is missing.
Works:
[HttpGet(Name = "Pet")]
public ActionResult<BaseModel> Get()
{
return new Cat() { CatName = "Mizzy" };
}
Does not work:
[HttpGet(Name = "Pet")]
public ActionResult<BaseModel> Get()
{
return Ok(new Cat() { CatName = "Mizzy" });
}
Also does not work:
[HttpGet(Name = "Pet")]
public ActionResult<BaseModel> Get()
{
return Ok(new Cat() { CatName = "Mizzy" } as BaseModel);
}
Expected Behavior
When using the Ok
method to return a OkObjectResult
it should output a type discriminator.
Steps To Reproduce
using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Serialization;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class ExampleController : ControllerBase
{
[HttpGet(Name = "GetPet")]
public ActionResult<BaseModel> Get()
{
return Ok(new Cat() { CatName = "Mizzy" } as BaseModel);
}
}
[JsonPolymorphic]
[JsonDerivedType(typeof(Cat), "cat")]
[JsonDerivedType(typeof(Dog), "dog")]
public class BaseModel
{
public string Name { get; set; }
}
public class Cat : BaseModel
{
public string CatName { get; set; }
}
class Dog : BaseModel
{
public string DogName { get; set; }
}
}
Exceptions (if any)
No response
.NET Version
8.0.10
Anything else?
No response