Skip to content

Skip indexer properties in validation source generator#65432

Open
adityamandaleeka wants to merge 2 commits intodotnet:mainfrom
adityamandaleeka:skipindexers
Open

Skip indexer properties in validation source generator#65432
adityamandaleeka wants to merge 2 commits intodotnet:mainfrom
adityamandaleeka:skipindexers

Conversation

@adityamandaleeka
Copy link
Member

Fixes #65424

The validation source generator crashes at runtime when encountering types with indexers (e.g. JsonElement, Dictionary<K,V>) used as minimal API endpoint parameters with AddValidation() enabled.

The source generator discovers validatable members by iterating typeSymbol.GetMembers().OfType<IPropertySymbol>(), but Roslyn exposes C# indexers (this[int index]) as IPropertySymbol with Name = "this[]". The generator emits these as ValidatablePropertyInfo entries. At runtime, ValidatablePropertyInfo.ValidateAsync calls DeclaringType.GetProperty("this[]"), which returns null because reflection names indexers "Item", causing:

System.InvalidOperationException: Property 'this[]' not found on type 'JsonElement'.

Confirmed this by decompiling the generated assembly, which showed:

new GeneratedValidatablePropertyInfo(
    typeof(JsonElement), typeof(JsonElement), "this[]", "this[]")

Fix

Added member.IsIndexer to the existing skip checks in ExtractValidatableMembers. Indexers represent parameterized access patterns, not data properties — they aren't meaningful targets for data annotation validation.

Testing

  • Added SkipsIndexerPropertiesOnTypes test verifying JsonElement works as both a direct parameter and a property on another type, and that validation still runs on non-indexer properties of the containing type.
  • Updated CanValidateParameters snapshot — Dictionary<string, TestService> was previously emitted solely due to its indexer, which would crash at runtime. It's now correctly excluded.
  • All 27 generator tests pass.

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 15, 2026
@adityamandaleeka adityamandaleeka marked this pull request as ready for review February 15, 2026 06:09
Copilot AI review requested due to automatic review settings February 15, 2026 06:09
@adityamandaleeka adityamandaleeka removed the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 15, 2026
@adityamandaleeka adityamandaleeka added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Feb 15, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a runtime crash in the minimal API validation source generator by ensuring C# indexer properties (e.g., this[int]) are not treated as validatable members, preventing invalid generated ValidatablePropertyInfo entries for types like JsonElement and Dictionary<TKey,TValue>.

Changes:

  • Skip indexer properties during validatable member discovery (IPropertySymbol.IsIndexer).
  • Add a generator test covering JsonElement as a parameter and as a property on another type.
  • Update generator snapshot baselines to reflect the new exclusion behavior (notably removing the previously-generated Dictionary<,> entry driven by its indexer).

Reviewed changes

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

File Description
src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs Filters out indexer properties during member extraction to avoid generating invalid property metadata.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ComplexType.cs Adds a regression test validating behavior with JsonElement and ensuring other properties still validate.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.SkipsIndexerPropertiesOnTypes#ValidatableInfoResolver.g.verified.cs New snapshot verifying generated output excludes indexer-driven members and only includes meaningful properties.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateParameters#ValidatableInfoResolver.g.verified.cs Updates snapshot to remove previously-generated Dictionary<string, TestService> validatable info that came solely from an indexer.

@oroztocil oroztocil added the feature-validation Issues related to model validation in minimal and controller-based APIs label Feb 15, 2026
@mikekistler mikekistler requested a review from halter73 February 17, 2026 12:17
Copy link
Member

@DeagleGross DeagleGross left a comment

Choose a reason for hiding this comment

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

Nice fix!

@adityamandaleeka
Copy link
Member Author

http2 CI failure is unrelated. Opened #65454 to fix that flakiness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validations in minimal APIs fails on several types from System.Text.Json

3 participants