-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Data Annotations on record fails in unittest #49011
Comments
Tagging subscribers to this area: @ajcvickers Issue DetailsCurrently we are setting up a new project and like to use the new records introduced in C# 9. Now the DataAnnotation is triggered when calling the Controller, but when i try to simulate this in a unittest (see code below) it will never return any errors.
Currently we try different solutions but nothing works...
This results in an error when i using this with mvc api.
As someone comment on stackoverflow to use targets in dataannotation.
Results in a error: The only workaround that is possible is write the complete record
I think this happens because the constructor of a record are params and properties at the same time.
|
So why doesn't the validation work if it's just on the parameter? public record FooRecord([Required] string BarProperty){ } |
Hi, I tried but this results to an error.
|
Funny part, this works with my mvc api. but when I create a unittest it doesn't detect an error.
|
We have a similar use case and are trying to find a good workaround. The current plan is to just use classes instead of records for now. Here are some relevant links I can dump here: Basically, In the 2nd link @pranavkm says this would be a feature request to add similar support to Validator. That would be one solution. Another I wonder if MVC can be configured to not throw on property attributes. Then I could write something like [Required][property: Required] string Bar as a workaround. It's ugly but for my use case it is in a Source Generator so would largely be hidden. |
Actually after reading more into it - I don't think adding constructor attribute support will be enough. It looks like Validator.TryValidateObject does not recurse through inner property values. |
Support for records is not something we plan to add to the validation attributes. The bottom line here is that the validation attributes are what they are. It's old and not very good code. I would not recommend it going forward. At some point there probably needs to be a new validation library that is easier to evolve and maintain, but there are currently no concrete plans for this. |
Currently we are setting up a new project and like to use the new records introduced in C# 9.
We encounter a problem with DataAnnotations inside the record (constructor) not being triggered during the unittest.
Now the DataAnnotation is triggered when calling the Controller, but when i try to simulate this in a unittest (see code below) it will never return any errors.
Currently we try different solutions but nothing works...
This results in an error when i using this with mvc api.
System.InvalidOperationException: Record type '*******.FooRecord' has validation metadata defined on property 'BarProperty' that will be ignored. 'BarProperty' is a parameter in the record primary constructor and validation metadata must be associated with the constructor parameter.
As someone comment on stackoverflow to use targets in dataannotation.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/#attribute-targets
Results in a error:
Record type '*******.FooRecord' has validation metadata defined on property 'BarProperty' that will be ignored. 'BarProperty' is a parameter in the record primary constructor and validation metadata must be associated with the constructor parameter.
The only workaround that is possible is write the complete record
I think this happens because the constructor of a record are params and properties at the same time.
I'm hoping if someone knows why this happens and maybe know how to solve this using the shorthand syntax:
The text was updated successfully, but these errors were encountered: