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

Annotations Polymorphism does not work with default S.T.J. discriminator name #3202

Open
pinkfloydx33 opened this issue Dec 16, 2024 · 0 comments
Labels
help-wanted A change up for grabs for contributions from the community

Comments

@pinkfloydx33
Copy link

pinkfloydx33 commented Dec 16, 2024

The JsonPolymorphicAttribute.TypeDiscriminatorPropertyName is nullable. When it's not provided, System.Text.Json defaults to "$type" per the documentation:

Gets or sets a custom type discriminator property name for the polymorhic type. Uses the default '$type' property name if left unset.

When using EnableAnnotations, the built-in AnnotationsDiscriminatorNameSelector method will return a null value if it finds a JsonPolymorphicAttribute where the user hasn't explicitly specified the value (hoping to leverage the defaults). This in turn causes the polymorphism/discriminator/etc. to be completely absent from the generated specification.

#if NET7_0_OR_GREATER
var jsonPolymorphicAttributes = baseType.GetCustomAttributes(false)
.OfType<JsonPolymorphicAttribute>()
.FirstOrDefault();
if (jsonPolymorphicAttributes != null)
{
return jsonPolymorphicAttributes.TypeDiscriminatorPropertyName;
}
#endif

I believe that the correct behavior would be to coalesce to "$type" IIF the JsonPolymorphicAttribute exists:

             if (jsonPolymorphicAttributes != null) 
             { 
                 return jsonPolymorphicAttributes.TypeDiscriminatorPropertyName ?? "$type";
             } 
@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

No branches or pull requests

2 participants