Skip to content

Fixed AmbiguousMatchException in DataAnnotationsValidator for Hidden Members#67075

Merged
ilonatommy merged 16 commits into
dotnet:mainfrom
PreethikaSelvam:gh27095BugFix
Jul 9, 2026
Merged

Fixed AmbiguousMatchException in DataAnnotationsValidator for Hidden Members#67075
ilonatommy merged 16 commits into
dotnet:mainfrom
PreethikaSelvam:gh27095BugFix

Conversation

@PreethikaSelvam

@PreethikaSelvam PreethikaSelvam commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Bug Description

This PR fixes an issue where the Blazor DataAnnotationsValidator throws an AmbiguousMatchException when validating models that contain hidden properties (i.e., derived class properties declared using new to hide base class members).

Fixes #27095

Problem

When a model includes properties with the same name in both base and derived classes, validation fails with: System.Reflection.AmbiguousMatchException. This breaks form validation scenarios in Blazor when inheritance with member hiding is used.

Root Cause

The issue originates from the use of: Type.GetProperty(string name). This reflection call returns multiple matches when both base and derived types define a property with the same name, resulting in ambiguity and an exception.

Solution Description

Added handling for AmbiguousMatchException. On exception, retried property resolution using:
BindingFlags.DeclaredOnly. This ensures that only properties declared on the current (derived) type are considered, avoiding ambiguity

Output Screenshots

Before Fix

GH27095BeforeFix.1.mp4

After Fix

GH27095AfterFix.mp4

Testing

Added 3 new unit test cases covering:

  • Validation of models with hidden properties
  • Ensuring no exception occurs and correct property resolution
  • Manual testing performed to validate real-world scenarios
image

Impact

  • Fixes runtime validation failure for inherited models with hidden members
  • Improves robustness of DataAnnotationsValidator
  • Ensures consistent behavior across reflection-based validation scenarios

PR Checklist

  • Small and focused — Fixes a specific validation issue
  • Tests added — 3 new unit tests included
  • Code style matches — Follows existing patterns
  • Local build passes
  • Local tests pass (14/14)
  • No breaking changes
  • Issue linked (if applicable) — #27095
  • Public API updates — Not applicable
  • XML documentation

@PreethikaSelvam PreethikaSelvam requested a review from a team as a code owner June 8, 2026 11:19
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Jun 8, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 8, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Comment thread src/Components/Forms/src/EditContextDataAnnotationsExtensions.cs Outdated
@ilonatommy ilonatommy enabled auto-merge (squash) July 7, 2026 13:58
@ilonatommy

Copy link
Copy Markdown
Member

The CI is always running on PR branch merged to main. In case there would be unresolved conflicts, there would be an information about it on the PR. Merging with main when there are no conflicts and when you're not aware of any recent changes that could influence your feature prolongs the time-to-merge of the PR.

auto-merge was automatically disabled July 8, 2026 05:08

Head branch was pushed to by a user without write access

@ilonatommy ilonatommy enabled auto-merge (squash) July 8, 2026 08:52
@ilonatommy

Copy link
Copy Markdown
Member

/ba-g failures not connected

@ilonatommy ilonatommy merged commit 3b00cfc into dotnet:main Jul 9, 2026
23 of 25 checks passed
{
propertyInfo = cacheKey.ModelType.GetProperty(
cacheKey.FieldName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

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.

FlattenHierarchy is only relevant for statics IIRC.

And this code can still throw if the previous call returned null and we get into here with some shadowing member in a base type.

Copilot AI added a commit that referenced this pull request Jul 9, 2026
… Hidden Members (#67075)"

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
javiercn added a commit that referenced this pull request Jul 10, 2026
… Hidden Members (#67075)"

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
javiercn added a commit that referenced this pull request Jul 10, 2026
… Hidden Members (#67075)" (#67712)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components 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.

DataAnnotationsValidator throws AmbiguousMatchException when there are hidden members

3 participants