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.
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.
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.
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.
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.
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
paramskeyword parameter likeDeniedValuesAttributeandAllowedValuesAttribute.This fix need to be ported to rc2.