dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.96k stars 4.65k forks source link

Complex validation attribute constructor support in the Options validator source generator #91966

Open tarekgh opened 1 year ago

tarekgh commented 1 year ago

Options source generator is parsing the code validation attributes in the source and then emits generated code which instantiate the attribute with the same parameters and properties used in the source code. Currently the source generator support all basic parameter types including the array and params parameters. There are more complex scenarios which can include array of arrays like:

using System;
public class MyValidationAttribute : Attribute {
    public MyValidationAttribute(params object[] values) { }
}

[MyValidationAttribute(new object[] { new object [] { new object[] { new int[] {1, 2, 3}}}})]
public class MyType
{
}

This issue is tracking to look at such complex scenarios and support it.

https://github.com/dotnet/runtime/pull/91934#discussion_r1323326105 https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp.Workspaces/CodeGeneration/CSharpSyntaxGenerator.cs,838c28d0fc10cd04

CC @ericstj

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations See info in area-owners.md if you want to be subscribed.

Issue Details
Options source generator is parsing the code validation attributes in the source and then emits generated code which instantiate the attribute with the same parameters and properties used in the source code. Currently the source generator support all basic parameter types including the array and params parameters. There are more complex scenarios which can include array of arrays like: ```C# using System; public class MyValidationAttribute : Attribute { public MyValidationAttribute(params object[] values) { } } [MyValidationAttribute(new object[] { new object [] { new object[] { new int[] {1, 2, 3}}}})] public class MyType { } ``` This issue is tracking to look at such complex scenarios and support it. https://github.com/dotnet/runtime/pull/91934#discussion_r1323326105 https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp.Workspaces/CodeGeneration/CSharpSyntaxGenerator.cs,838c28d0fc10cd04 CC @ericstj
Author: tarekgh
Assignees: -
Labels: `area-System.ComponentModel.DataAnnotations`
Milestone: -
ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-extensions-options See info in area-owners.md if you want to be subscribed.

Issue Details
Options source generator is parsing the code validation attributes in the source and then emits generated code which instantiate the attribute with the same parameters and properties used in the source code. Currently the source generator support all basic parameter types including the array and params parameters. There are more complex scenarios which can include array of arrays like: ```C# using System; public class MyValidationAttribute : Attribute { public MyValidationAttribute(params object[] values) { } } [MyValidationAttribute(new object[] { new object [] { new object[] { new int[] {1, 2, 3}}}})] public class MyType { } ``` This issue is tracking to look at such complex scenarios and support it. https://github.com/dotnet/runtime/pull/91934#discussion_r1323326105 https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp.Workspaces/CodeGeneration/CSharpSyntaxGenerator.cs,838c28d0fc10cd04 CC @ericstj
Author: tarekgh
Assignees: tarekgh
Labels: `untriaged`, `area-Extensions-Options`, `source-generator`
Milestone: 9.0.0
ericstj commented 1 year ago

In solving this we should look at all possible parameters that might appear in attribute usage to ensure we can synthesize source for them. I think it might make sense to look at Roslyn's CSharpSyntaxGenerator for some inspiration here - I think the place to look for reference implementation might be https://github.com/dotnet/roslyn/blob/1b7a6f807cb8ce709048debae6b771f4705a697a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs#L3393

ericstj commented 1 month ago

@tarekgh co we need to address this in 9.0? I don't think we've heard feedback on it.