Skip non-public properties in validations generator#63076
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the validation generator to skip non-public properties and types during code generation, addressing issue #62757. The changes ensure that only publicly accessible members are included in the generated validation code.
Key Changes
- Modified the type parser to skip non-public types and properties in validation generation
- Updated test cases to verify that only public properties are included in generated validation code
- Added comprehensive test coverage for accessibility scenarios
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
ValidationsGenerator.TypesParser.cs |
Added accessibility checks to skip non-public types and properties during validation generation |
ValidationsGenerator.ComplexType.cs |
Added new test case to verify non-public properties are skipped while public properties remain validated |
ValidationsGeneratorTests.SkipsClassesWithNonAccessibleTypes#ValidatableInfoResolver.g.verified.cs |
New snapshot showing generated code only includes public properties |
ValidationsGeneratorTests.CanValidateParameters#ValidatableInfoResolver.g.verified.cs |
Updated snapshot removing non-public properties from Dictionary validation |
| return false; | ||
| } | ||
|
|
||
| // Skip types that are not accessible from generated code |
There was a problem hiding this comment.
Consider adding a comment explaining why non-public types are skipped, as this is a significant behavioral change that affects which types get validation generation.
| // Skip types that are not accessible from generated code | |
| // Skip types that are not accessible from generated code | |
| // Only public types are included for validation generation because generated code can only access public types. | |
| // This is a significant behavioral decision: non-public types will not have validation generated. |
| continue; | ||
| } | ||
|
|
||
| // Skip properties that are not accessible from generated code |
There was a problem hiding this comment.
Consider adding a comment explaining why non-public properties are skipped, similar to the type-level check, to maintain consistency in documentation.
| // Skip properties that are not accessible from generated code | |
| // Skip properties that are not accessible from generated code | |
| // Only public properties can be accessed by generated code, so non-public properties are skipped. |
| continue; | ||
| } | ||
|
|
||
| // Skip properties that are not accessible from generated code |
There was a problem hiding this comment.
Consider adding a comment explaining why non-public properties are skipped, to maintain consistency with the other accessibility checks in this file.
| // Skip properties that are not accessible from generated code | |
| // Skip properties that are not accessible from generated code | |
| // Non-public properties are skipped because they cannot be accessed by the generated code. |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Non-public properties are validated at run-time still? Is there test coverage for that? |
I was specifically refering to types that are resolved from the DI container but not annotated with the app.MapGet("/", (CatalogDbContext dbContext) => ...);In the example above, there's no way to determine at compile-time that In this new model, we'll still create a |
Closes #62757.