Skip to content

Skip null elements in [ValidateEnumeratedItems] validation - #130720

Merged
tarekgh merged 2 commits into
dotnet:mainfrom
tarekgh:fix-validateenumerateditems-null-elements
Jul 16, 2026
Merged

tarekgh merged 2 commits into
dotnet:mainfrom
tarekgh:fix-validateenumerateditems-null-elements

Conversation

@tarekgh

@tarekgh tarekgh commented Jul 14, 2026

Copy link
Copy Markdown
Member

Summary

DataAnnotationValidateOptions<TOptions> threw ArgumentNullException when a property annotated with [ValidateEnumeratedItems] contained a null element. The null item was passed straight into the recursive validation helper, which reaches Validator.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 synchronous Validate path and the asynchronous ValidateAsync path (the latter had the same unguarded loop).

Why skip rather than report

  • It is consistent with what the validator already does one level up: a null property value is skipped today via if (value is null) continue;, so skipping a null element is the natural extension.
  • Every error this validator produces describes a failing member inside an item (for example ... members: 'Name' with the error: 'The Name field is required.'). A null element has no member and no ValidationResult, 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

  • Added sync tests that a null element among valid items is skipped, and that an invalid item after a null element is still reported with its true index (and the null index is not reported).
  • Added an async test for the null-skip behavior.
  • Verified the new tests fail without the fix (ArgumentNullException) and pass with it.
  • Full Microsoft.Extensions.Options.Tests suite passes: 141 tests on net (NetCoreAppCurrent) and 127 on net481, 0 failures.

Fixes #130682

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

Copy link
Copy Markdown
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.

@tarekgh tarekgh added this to the 11.0.0 milestone Jul 14, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

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 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 null elements during [ValidateEnumeratedItems] enumeration in DataAnnotationValidateOptions (sync).
  • Apply the same null-skip behavior in DataAnnotationValidateOptions.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 rosebyte 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, just one question, not blocking.

Copilot AI review requested due to automatic review settings July 15, 2026 16:19

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@tarekgh
tarekgh merged commit 5453829 into dotnet:main Jul 16, 2026
89 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Microsoft.Extensions.Options] ValidateEnumeratedItems throws for null collection elements

4 participants