Skip to content

ValidationsGenerator: Allow validating internal types - #67399

Merged
Youssef1313 merged 3 commits into
mainfrom
dev/ygerges/internal-type-val
Jun 25, 2026
Merged

Youssef1313 merged 3 commits into
mainfrom
dev/ygerges/internal-type-val

Conversation

@Youssef1313

@Youssef1313 Youssef1313 commented Jun 24, 2026 •

Copy link
Copy Markdown
Member

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 have Internal accessibility in Roslyn but IsFileLocal = 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

  • Broaden eligible model types from public to public + internal during type extraction in the source generator.
  • Skip file-local types by checking INamedTypeSymbol.IsFileLocal before the accessibility check, since file-local types always have Internal accessibility but cannot be referenced from generated code.
  • Add regression tests for both internal types and file-local types, along with their verified snapshots.

Copilot AI review requested due to automatic review settings June 24, 2026 16:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 internal types as eligible for validation generation.
  • Add a generator test that validates an internal endpoint model type (including an internal nested 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.

Comment on lines 78 to 82
// 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;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Add a test for file-local types. If it's failing make the change to fix it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Youssef1313

Copy link
Copy Markdown
Member Author

/ba-g unrelated quarantine timeout.

@Youssef1313
Youssef1313 merged commit 4c23531 into main Jun 25, 2026
23 of 25 checks passed
@Youssef1313
Youssef1313 deleted the dev/ygerges/internal-type-val branch June 25, 2026 07:00
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants