-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Type disciminator is missing with OkResult #58832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you for filing this issue and providing clear and complete information on how to reproduce it. I have reproduced it and done some investigation. I believe that this is happening because when the result value is wrapped in "Ok" the type information is lost / not conveyed to System.Text.Json, which treats is as just a regular object rather than a Cat. I will continue investigating and post here when I have something concrete. |
I've spoken with the engineering team and learned a bit more. In particular, there seem to be a couple workarounds for this problem. The first is to explicitly set the [HttpGet("fido")]
public ActionResult<BaseModel> GetFido()
{
var result = Ok(new Dog() { DogName = "Fido" });
result.DeclaredType = typeof(BaseModel);
return result;
} Another workaround is to change the return type of the method to an [HttpGet("snoopy")]
public Ok<BaseModel> GetSnoopy()
{
return TypedResults.Ok(new Dog() { DogName = "Snoopy" } as BaseModel);
} Both of these approaches generate a response body with the Would one of these approaches work for you? |
None of them are pretty, it would be great if it "just worked" without having to use any workarounds. The workarounds are not intuitive if users have to talk to the engineering team to figure it out. But until then I could do: [HttpGet("mizzy")]
public ActionResult<BaseModel> GetMizzy()
{
var pet = new Cat() { CatName = "Mizzy" };
return Ok(pet) { DeclaredType = typeof(BaseModel) };
} Maybe the |
The root cause is that the The following code is where we convert the
So in the case of returning an |
Is it feasible for ASP.NET to somehow change this in a way that polymorphism just works, or is setting the |
Any updates on this? Or at least, whether it's something that might be addressed, or is unlikely to change :-) |
I thought I would try out this solution for my instance of running into this issue, and I put this method in a derived base class for controllers (Please excuse the code quality :D):
What I had not realized was that this did not require me to use Ok<BaseType>, if the variable I passed as parameter already had the base type declared; that is, it simply made my code work without any other changes. 🤷♂️ I don't know if this causes issues in other cases, but it looks like it may be a fairly simple fix otherwise? |
Thanks for the gentle poke on this. It had slipped off the radar. I added it to our Backlog milestone as something to consider as resources become available. |
Is there an existing issue for this?
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:
Does not work:
Also does not work:
Expected Behavior
When using the
Ok
method to return aOkObjectResult
it should output a type discriminator.Steps To Reproduce
Exceptions (if any)
No response
.NET Version
8.0.10
Anything else?
No response
The text was updated successfully, but these errors were encountered: