-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Make Options source gen support Validation attributes having constructor with params Parameter #91915
Make Options source gen support Validation attributes having constructor with params Parameter #91915
Conversation
…tor with params Parameter
Tagging subscribers to this area: @dotnet/area-extensions-options Issue DetailsThis change is to support the validation attributes having constructor with This fix need to be ported to rc2.
|
{ | ||
validationAttr.ConstructorArguments.Add(GetArgumentExpression(constructorArgument.Type!, constructorArgument.Value)); | ||
TypedConstant argument = arguments[i]; | ||
if (argument.Kind == TypedConstantKind.Array) |
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.
Probably too much of a niche scenario to consider here, but could this logic be tripped up by something like the following?
new MyValidationAttribute(new int[] { 1, 2, 3 });
public class MyValidationAttribute(params int[][] values) : ValidationAttribute
{
}
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 can consider that in .NET 9.0 as I want to limit the changes, we are porting to .NET 8.0.
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.
That's actually not a valid attribute parameter type if you want to apply the attribute (could be interesting for testing, but I gather this generator only observes applied attributes).
https://sharplab.io/#v2:D4AQTAjAsAUCDMACciCyBPAagQwDYEsATbAF3wHsA7AQRJICd8AjAVxIFNEAuRWh5tpwDesRGORIMOAsTJU+jVhwAUAB2z1sAWwDOifJRIBtALqnEANzwt2OgJSIhiAL6xXMWEal4ipCjTpFQWVKdgB3fUNzJwgAGkQwePhnOxNYBGQwNHQAFXRVdlgRGHcgA===
error CS0181: Attribute constructor parameter 'values' has type 'int[][]', which is not a valid attribute parameter type
Attribute's parameters need to be represented in metadata and can only take a very limited set of types.
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.
Good point. I did recall that it's possible to nest arrays in attribute declarations, but it looks like it's only possible for object[]
parameters:
I think that case would work fine with the logic as-is, but maybe a test is in order?
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.
I see this was brought up here as well: #91934 (comment)
@dotnet-policy-service rerun |
/backport to release/8.0 |
Started backporting to release/8.0: https://github.com/dotnet/runtime/actions/runs/6161762714 |
This change is to support the validation attributes having constructor with
params
keyword parameter likeDeniedValuesAttribute
andAllowedValuesAttribute
.This fix need to be ported to rc2.