Repository navigation
Fix array type unwrapping in validation source generator - #67743
Conversation
|
Thanks for your PR, @Vladik29w. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
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
IArrayTypeSymbolto itsElementTypeduring 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
left a comment
There was a problem hiding this comment.
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 🎉
Fix array type unwrapping in validation source generator
Fixes array type unwrapping for IValidatableObject in source generator.
Description
The Validation source generator previously failed to recognize models implementing
IValidatableObjectorIAsyncValidatableObjectwhen they were passed as arrays (either as endpoint parameters or nested properties). This occurred because the generator evaluated theIArrayTypeSymboldirectly 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