Skip to content

Add support for internal types in validation source generator (#65632)#66061

Open
XL1TTE wants to merge 5 commits intodotnet:mainfrom
XL1TTE:solve-issue-65632
Open

Add support for internal types in validation source generator (#65632)#66061
XL1TTE wants to merge 5 commits intodotnet:mainfrom
XL1TTE:solve-issue-65632

Conversation

@XL1TTE
Copy link
Copy Markdown

@XL1TTE XL1TTE commented Mar 29, 2026

Add support for internal types in validation source generator (#65632)

This change adds an opt-in mechanism for applications that keep request models internal for encapsulation purposes.
Added test coverage for internal types, internal properties, and verification that private/protected members remain excluded from validation.

Description

By default, the validation source generator only processes public types and members. When IncludeInternalTypes() call detected, ValidationsGenerator will start to include internal types and members to validation process.

Added Tests

Internal Types Validation

Test Name Description Expected Behavior
InternalType_NotValidated_ByDefault Internal class with [ValidatableType] attribute without configuration Internal type is not discovered for validation by default
InternalType_Validated_With_IncludeInternalTypes Internal class with [ValidatableType] attribute with IncludeInternalTypes() Internal type is discovered and validated when option is enabled

Internal Properties Validation

Test Name Description Expected Behavior
InternalProperty_NotValidated_ByDefault Public class with internal property marked [Required] Internal property is not validated by default
InternalProperty_Validated_With_IncludeInternalTypes Public class with internal property marked [Required] with IncludeInternalTypes() Internal property is validated when option is enabled

Private and Protected Properties Validation

Test Name Description Expected Behavior
PrivateProperty_Never_Validated Public class with private property marked [Required] with IncludeInternalTypes() Private property is never validated (even with IncludeInternalTypes())
ProtectedProperty_Never_Validated Public class with protected property marked [Required] with IncludeInternalTypes() Protected property is never validated (even with IncludeInternalTypes())

Internal Nested Types Validation

Test Name Description Expected Behavior
InternalNestedType_Validated_With_IncludeInternalType Public class with internal nested type property with IncludeInternalTypes() Internal nested type is validated when option is enabled

Configuration Syntax Validation

Test Name Description Expected Behavior
IncludeInternalTypes_BlockLambda_Supported Configuration using block lambda syntax { options.IncludeInternalTypes(); } Block lambda syntax works correctly
IncludeInternalTypes_ExpressionLambda_Supported Configuration using expression lambda syntax options => options.IncludeInternalTypes() Expression lambda syntax works correctly

Test Summary

  • Total Tests Added: 9
  • Key Assertions:
    • Default behavior excludes internal types and properties
    • IncludeInternalTypes() enables internal types and properties
    • Private and protected members remain excluded regardless of configuration
    • Both lambda syntax variations are supported

Usage:

builder.Services.AddValidation(options => options.IncludeInternalTypes());

Fixes #65632

…#65632)

Enable validation of internal request DTOs and properties through
a new IncludeInternalTypes() configuration option.

By default, the validation source generator only processes public types and members.
This change adds an opt-in mechanism for applications that keep request models internal
for encapsulation purposes.

Usage:
    builder.Services.AddValidation(options => options.IncludeInternalTypes());

Added test coverage for internal types, internal properties, and
verification that private/protected members remain excluded from validation.
@github-actions github-actions bot added the area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework label Mar 29, 2026
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 29, 2026
@XL1TTE
Copy link
Copy Markdown
Author

XL1TTE commented Mar 29, 2026

@dotnet-policy-service agree

Update DynamicallyAccessedMembers annotations on generated code to satisfy NativeAOT trimming requirements.

- **GeneratedValidatablePropertyInfo.ContainingType**: Changed from `PublicProperties|PublicConstructors` to `PublicProperties|NonPublicProperties` to match base class ValidatablePropertyInfo requirements
- **ValidationAttributeCache.CacheKey.ContainingType**: Added `PublicConstructors|NonPublicConstructors` to support `GetConstructors()` calls that access both public and non-public constructors.
- **ValidationAttributeCache.GetPropertyValidationAttributes**: Updated parameter annotation to include `PublicConstructors|NonPublicConstructors` for consistency with CacheKey.

Should fix trimming errors **IL2067**, **IL2072**, and **IL2075** that occurred when building NativeAOT and trimming test projects.
@XL1TTE XL1TTE force-pushed the solve-issue-65632 branch from 2c36e9c to d001b06 Compare March 29, 2026 11:55
@XL1TTE XL1TTE marked this pull request as ready for review March 29, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support internal request DTOs in built-in Minimal API validation

1 participant