Skip to content
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

Type disciminator is missing with OkResult #58832

Open
1 task done
vanillajonathan opened this issue Nov 7, 2024 · 1 comment
Open
1 task done

Type disciminator is missing with OkResult #58832

vanillajonathan opened this issue Nov 7, 2024 · 1 comment
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@vanillajonathan
Copy link
Contributor

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 7, 2024
@martincostello martincostello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Nov 7, 2024
@mikekistler mikekistler self-assigned this Nov 9, 2024
@mikekistler
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

3 participants