Repository navigation
ValidationsGenerator: Allow validating internal types - #67399
Conversation
There was a problem hiding this comment.
Pull request overview
Enables the validation source generator to include endpoint parameter models that are internal, addressing scenarios where validation was previously skipped due to type visibility.
Changes:
- Update the type parser to treat
internaltypes as eligible for validation generation. - Add a generator test that validates an
internalendpoint model type (including aninternalnested model) produces expected validation errors. - Add the corresponding verified snapshot for the new test.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs | Broadens eligible model types from public to public + internal during type extraction. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ComplexType.cs | Adds a regression test to ensure internal model types are validated end-to-end. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.ValidatesInternalTypes#ValidatableInfoResolver.g.verified.cs | New verified snapshot for generated output for the added internal-types test. |
| // Skip types that are not accessible from generated code | ||
| if (typeSymbol.DeclaredAccessibility is not Accessibility.Public) | ||
| if (typeSymbol.DeclaredAccessibility is not (Accessibility.Public or Accessibility.Internal)) | ||
| { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
@copilot Add a test for file-local types. If it's failing make the change to fix it.
There was a problem hiding this comment.
Added a SkipsFileLocalTypes test in ValidationsGenerator.ComplexType.cs. Since file-local types have DeclaredAccessibility = Internal in Roslyn (but IsFileLocal = true), the original code was treating them like regular internal types and would have generated code referencing them — which fails to compile because the generated file is separate from the declaring file.
The fix in ValidationsGenerator.TypesParser.cs adds an IsFileLocal check (before the accessibility check, since file-local types always have Internal accessibility) that causes the generator to skip these types. The snapshot confirms the resolver correctly produces no entry for the file-local type. Commits: the fix and test in a19d74a's follow-up commits.
Co-authored-by: Youssef1313 <[email protected]>
Co-authored-by: Youssef1313 <[email protected]>
|
/ba-g unrelated quarantine timeout. |
This is part of #64435
It allows us to validate internal types. Additionally, it fixes a related edge case where C#
file-local types (which haveInternalaccessibility in Roslyn butIsFileLocal = true) were incorrectly included in validation generation, which would produce generated code that fails to compile since file-local types cannot be referenced from a different file.Description
publictopublic+internalduring type extraction in the source generator.file-local types by checkingINamedTypeSymbol.IsFileLocalbefore the accessibility check, since file-local types always haveInternalaccessibility but cannot be referenced from generated code.internaltypes andfile-local types, along with their verified snapshots.