Skip to content

Fix array type unwrapping in validation source generator - #67743

Merged
Youssef1313 merged 2 commits into
dotnet:mainfrom
Vladik29w:fix-validation-arrays
Jul 12, 2026
Merged

Youssef1313 merged 2 commits into
dotnet:mainfrom
Vladik29w:fix-validation-arrays

Conversation

@Vladik29w

Copy link
Copy Markdown
Contributor

Fix array type unwrapping in validation source generator

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Fixes array type unwrapping for IValidatableObject in source generator.

Description

The Validation source generator previously failed to recognize models implementing IValidatableObject or IAsyncValidatableObject when they were passed as arrays (either as endpoint parameters or nested properties). This occurred because the generator evaluated the IArrayTypeSymbol directly instead of unwrapping it to inspect its underlying element type.

This change updates the symbol processing logic to extract the element type from arrays, allowing the generator to discover and generate validation code for array elements. Added tests to cover direct array parameters, nested arrays, and empty array edge cases.

Fixes #67412

Copilot AI review requested due to automatic review settings July 11, 2026 17:49
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 11, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @Vladik29w. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

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

Fixes the validation source generator’s type-discovery logic so array-typed models (and array-typed nested properties) are unwrapped to their element type when determining whether IValidatableObject / IAsyncValidatableObject is implemented.

Changes:

  • Update generator symbol processing to unwrap IArrayTypeSymbol to its ElementType during type discovery.
  • Extend generator tests to cover endpoint parameters typed as arrays, nested array properties, and empty-array payloads.
  • Update verified snapshot output to reflect newly discovered validatable types.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Validation/gen/Extensions/ITypeSymbolExtensions.cs Unwraps array types to ensure the generator inspects array element types for validatability.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.IValidatableObject.cs Adds test coverage for array parameters and nested array properties (including empty array behavior).
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateIValidatableObject_WithoutPropertyValidations#ValidatableInfoResolver.g.verified.cs Updates snapshot to include the additional discovered validatable type info.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

@Youssef1313 Youssef1313 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I think there might be more bugs in this unwrapping logic (e.g, if the enumerable/array element is a nullable value type that itself needs to be unwrapped (but was unwrapped before the the array check).

But given this is an already-existing different bug, we can take this PR as a targeted fix for the array case specifically.

Thanks for your contribution 🎉

@Youssef1313
Youssef1313 merged commit f82669a into dotnet:main Jul 12, 2026
25 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

ValidationsGenerator doesn't unwrap array types

3 participants