Skip to content

Typed results (such as Created<T>) do not emit type discriminator from attribute-based polymorphic serialization introduced in .NET 7 #45357

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

Closed
1 task done
dkokotov opened this issue Nov 30, 2022 · 2 comments
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Comments

@dkokotov
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If I have a type hierarchy with two classes A and B, where B is derived from A and A contains JsonPolymorphic and JsonDerivedType annotations, and I then have a request delegate that returned Created<A>, the serialized response does not contain the type discriminator property.

Expected Behavior

I would expect the response to include the type discriminator property as described in the docs for polymorphic serialization.

However, it does not.

Steps To Reproduce

[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(B), typeDiscriminator: "b")]
[JsonDerivedType(typeof(A), typeDiscriminator: "a")]
public record A {
    public string SomeProp { get; init; }
}

public record B {
    public string SomeOtherProp { get; init; }
}

// in Program.cs
builder.Map("/try", () => return TypedResults.Created<A>("/newone", new B { SomeProp = "foo", SomeOtherProp = "bar" }));
// this will serialize to { "SomeProp": "foo", "SomeOtherProp": "bar" } 
// but I would expect it to be { "type": "b", "SomeProp": "foo", "SomeOtherProp": "bar" } 

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

I believe this is caused by the considerations discussed in #43894 and #41724 to support backwards-compatible polymorphic serialization. Presumably due to those changes, ultimately the serialization code from Created<T> calls JsonSerializer.Serialize explicitly with the runtime type of the value passed to Created<T>. but the documentation for the type discriminator feature explicitly requires the base type to be used.

@javiercn javiercn added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 30, 2022
@dkokotov
Copy link
Author

dkokotov commented Dec 1, 2022

I think this issue got closed accidentally because I mentioned it in a PR where I implemented a workaround (in a private repo). It looks like when that PR got merged Github for some reason decided to close this issue as well.

@dkokotov dkokotov reopened this Dec 1, 2022
@captainsafia
Copy link
Member

captainsafia commented Dec 1, 2022

Triage: @brunolins16 has been doing some investigation in this area after reviewing issue #44852. Closing this one as a dupe.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

No branches or pull requests

3 participants