Repository navigation
Skip null elements in [ValidateEnumeratedItems] validation - #130720
Merged
tarekgh merged 2 commits intoJul 16, 2026
Merged
Conversation
DataAnnotationValidateOptions threw ArgumentNullException when a collection annotated with [ValidateEnumeratedItems] contained a null element, aborting the whole validation. Null elements are now skipped (consistent with the existing null-property skip), while the item index still advances so later items keep their true position. Applies to both the sync and async validation paths. Fixes dotnet#130682
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-options |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes recursive data-annotations options validation to skip null elements when a property annotated with [ValidateEnumeratedItems] contains null entries, preventing validation from throwing and allowing later elements to continue being validated (including correct index reporting). This applies to both the sync and async validation paths.
Changes:
- Skip
nullelements during[ValidateEnumeratedItems]enumeration inDataAnnotationValidateOptions(sync). - Apply the same
null-skip behavior inDataAnnotationValidateOptions.ValidateAsync(async). - Add regression tests covering null-element skip behavior and index preservation in failure paths.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs | Skip null items while iterating [ValidateEnumeratedItems] collections, preserving index progression. |
| src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.Async.cs | Mirror the sync fix in the async recursive validation loop. |
| src/libraries/Microsoft.Extensions.Options/tests/Microsoft.Extensions.Options.Tests/OptionsBuilderTest.cs | Add sync + async regression tests for null-element handling and correct index reporting. |
rosebyte
approved these changes
Jul 15, 2026
rosebyte
left a comment
Member
There was a problem hiding this comment.
LGTM, just one question, not blocking.
svick
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DataAnnotationValidateOptions<TOptions>threwArgumentNullExceptionwhen a property annotated with[ValidateEnumeratedItems]contained a null element. The null item was passed straight into the recursive validation helper, which reachesValidator.TryValidateObject/new ValidationContext(...)and rejects a null instance, aborting the entire validation before any result is returned.This skips null elements in the enumerated-items loop, while still advancing the index so the remaining items keep their real positions in reported error paths (for example
Parent.Children[2]). The change is applied to both the synchronousValidatepath and the asynchronousValidateAsyncpath (the latter had the same unguarded loop).Why skip rather than report
if (value is null) continue;, so skipping a null element is the natural extension.... members: 'Name' with the error: 'The Name field is required.'). A null element has no member and noValidationResult, so reporting it would require inventing a synthetic message with empty member names that does not match any other output. There is also no per-item[Required]semantic that would make a null element invalid on its own.Enforcing item non-nullability would be a separate, deliberate validation rule rather than part of this fix.
Testing
ArgumentNullException) and pass with it.Microsoft.Extensions.Options.Testssuite passes: 141 tests on net (NetCoreAppCurrent) and 127 on net481, 0 failures.Fixes #130682