-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Don't consider classes that do not have any attributes or base classes #57750
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
Don't consider classes that do not have any attributes or base classes #57750
Conversation
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsThe generator will ignore any class without a matching attribute or base class. We can thus not ever consider them for binding, short circuiting in the vast majority of cases.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
if (syntaxNode is ClassDeclarationSyntax cds) | ||
if (syntaxNode is ClassDeclarationSyntax { AttributeLists.Count: > 0, BaseList.Types.Count: > 0 } cds) | ||
{ | ||
(ClassDeclarationSyntaxList ??= new List<ClassDeclarationSyntax>()).Add(cds); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) I wonder if this property should be renamed. Before it held all classes, so the name made more sense. But now it only holds "potential context classes", so maybe a rename is in order?
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
if (syntaxNode is ClassDeclarationSyntax cds) | ||
if (syntaxNode is ClassDeclarationSyntax { AttributeLists.Count: > 0, BaseList.Types.Count: > 0 } cds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we further constrain it to partial
classes or would that loose a valuable diagnostic in the case the user forgot to mark the class partial
? I guess we're already losing a diagnostic here when the user forgot to derive from JsonSerializerContext
. Do we care?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I considered that. It does seem odd that it only warns on missing partial
.
Aside: is there a reason as a user that I can't just put an attribute on my serialization class, and have it generate the whole thing for me. Seems weird I have to make an empty partial class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: is there a reason as a user that I can't just put an attribute on my serialization class, and have it generate the whole thing for me.
What name and namespace would we generate for the class that derives from JsonSerializerContext?
There is also the scenario of when you want to serialize a Type in an assembly that you don't own/develop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What name and namespace would we generate for the class that derives from JsonSerializerContext?
I mean, you could supply it in the attribute, or derive it.
There is also the scenario of when you want to serialize a Type in an assembly that you don't own/develop.
Assembly attribute? But I digress. I just found the experience a little odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We welcome the feedback. If you have an idea how we can improve the experience, we'd like to hear it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually used to have assembly level attributes for interacting with the generator and then changed course to the partial class. Some of that discussion is here: #51945 (comment)
@steveharter - can this be merged? |
/backport to release/6.0 |
Started backporting to release/6.0: https://github.com/dotnet/runtime/actions/runs/1162083606 |
@ericstj we're also backporting this to RC1, right? |
The generator will ignore any class without a matching attribute or base class. We can thus not ever consider them for binding, short circuiting in the vast majority of cases.