-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Validation] Support generated ValidatableTypeAttribute
for Blazor
#63115
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
Conversation
68644ff
to
f1f768f
Compare
src/Validation/gen/Parsers/ValidationsGenerator.AttributeParser.cs
Outdated
Show resolved
Hide resolved
src/Validation/gen/Parsers/ValidationsGenerator.AttributeParser.cs
Outdated
Show resolved
Hide resolved
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.
Pull Request Overview
This PR adds support for generated ValidatableTypeAttribute
to work alongside the existing framework version in Blazor scenarios. The main purpose is to enable validation generators to recognize and process both the framework-provided ValidatableTypeAttribute
and a generated version that can be provided by the Razor SDK.
Key Changes
- Modified the validation generator to process both framework and generated versions of
ValidatableTypeAttribute
- Added test snapshots demonstrating the generated code output for auto-generated attributes
- Created comprehensive test cases covering scenarios with both attribute types
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
ValidationsGenerator.cs | Modified to collect and combine validatable types from both framework and generated attributes |
ValidationsGenerator.AttributeParser.cs | Updated parameter names to follow discard pattern conventions |
ValidationsGenerator.AutoGeneratedAttribute.cs | Added test cases for discovering and using generated ValidatableTypeAttribute |
Snapshot files (3) | Test verification snapshots for generated code output |
src/Validation/gen/Parsers/ValidationsGenerator.AttributeParser.cs
Outdated
Show resolved
Hide resolved
src/Validation/gen/Parsers/ValidationsGenerator.AttributeParser.cs
Outdated
Show resolved
Hide resolved
src/Validation/gen/Parsers/ValidationsGenerator.AttributeParser.cs
Outdated
Show resolved
Hide resolved
...icrosoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.AutoGeneratedAttribute.cs
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,9 @@ | |||
//HintName: ValidatableTypeAttribute.g.cs | |||
// <auto-generated/> | |||
namespace TestApp; |
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.
This namespace looks incorrect. Is it because the source strings in the test code use file-scoped namespaces? 🤔
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 regenerated the tests. For some reason the tests were passing but nothing was happening. I even introduced a compilation issue on purpose to make them fail and were green.
I switched to use the old style namespaces and Program main and that got them "working" again
|
dotnet/sdk#50083 <- Complementary PR for emitting the attribute.
Yep, but that's not a big deal. Referencing the Razor SDK is not a problem and the embedded (I'm going to start calling it like that) is a thing only for .net 10.0, we plan to move out of it once the other attribute stabilizes. |
460cca1
to
c887ba4
Compare
The ValidationsGenerator now supports discovering types marked with ValidatableTypeAttribute from two sources:
Framework attribute: Microsoft.Extensions.Validation.ValidatableTypeAttribute (from Microsoft.Extensions.Validation package)
SDK-generated attribute: Microsoft.Extensions.Validation.Embedded.ValidatableTypeAttribute (embedded by Razor SDK in projects with .razor files)
This enables developers to use [ValidatableType] in Blazor projects without explicitly referencing the experimental validation framework attribute, as the Razor SDK automatically embeds the attribute as needed on the assembly