Skip to content

Revert #67075: hidden-member validation fix is incomplete and regresses behavior #67711

Closed

Description

@javiercn

Summary

I'm proposing we revert #67075 ("Fixed AmbiguousMatchException in DataAnnotationsValidator for Hidden Members"), which merged in commit 3b00cfcae37cd1866de43205059039c1576404ff. After a closer look, the change does not fully fix the original bug (#27095), introduces a silent behavior regression, and adds a resolution policy that is inconsistent with how the rest of the codebase already handles new-hiding. Reverting restores the original, loud AmbiguousMatchException behavior, which we're comfortable keeping as-is.

What changed

The PR modified TryGetValidatableProperty in src/Components/Forms/src/EditContextDataAnnotationsExtensions.cs to resolve the property in two steps: DeclaredOnly first, then FlattenHierarchy as a fallback.

propertyInfo = cacheKey.ModelType.GetProperty(
    cacheKey.FieldName,
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

if (propertyInfo is null)
{
    propertyInfo = cacheKey.ModelType.GetProperty(
        cacheKey.FieldName,
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
}

Why revert

1. It doesn't fully close the bug

BindingFlags.FlattenHierarchy does not flatten instance members; it only pulls public/protected static members up from base types. For instance properties the fallback behaves like an ordinary inherited lookup that walks base types, so the original AmbiguousMatchException is still reachable when the hiding happens below the runtime type:

class A     { public object Foo { get; set; } }
class B : A { public new string Foo { get; set; } }  // hides at B
class C : B { /* does NOT redeclare Foo */ }
  • typeof(C).GetProperty("Foo", DeclaredOnly) returns null (C declares nothing)
  • fallback GetProperty("Foo", FlattenHierarchy) walks B and A, finds two Foo, and throws AmbiguousMatchException

This is the exact exception the PR set out to fix. It was called out during review (#67075 (comment)) but not resolved before merge. The test models all redeclare the property on the leaf type, so step 2 (the fallback) is never exercised for a hiding scenario, and the coverage misses precisely where the remaining bug lives.

2. It regresses a previously loud failure into a silent one

Before this change, the unsupported scenario failed loudly with AmbiguousMatchException. After it, resolution is silent and depends on which level of the hierarchy the runtime type resolves to:

  • If a base property has [Required] but the derived new property has no attributes, validation now silently uses only the most-derived declaration and the base attributes are dropped. The PR's SkipsValidationWhenDerivedShadowHasNoAttributes test enshrines this as expected behavior, but it was never a designed decision — it is just what the reflection call happens to do.
  • The inverse (attributes only on the base) resolves differently again.

The net effect is "does my [Range]/[Required] apply?" becomes reference-type-dependent with no diagnostic, which is harder to diagnose than the original crash.

3. It's inconsistent with existing framework behavior

new-hiding is already handled by the shared PropertyHelper.GetVisibleProperties (src/Shared/PropertyHelper/PropertyHelper.cs), which walks from the runtime type down to the declaring type ("most-derived wins") and correctly handles multi-level hierarchies. MVC model metadata builds on it (GetMetadataForProperties_ExcludesHiddenProperties), and Microsoft.Extensions.Validation has its own tested resolver (TryFindProperty). Because OnFieldChanged has two code paths (the Validation-stack path and this reflection path), the same model can now validate differently depending on whether the app opted into the new validation stack. This PR adds a third, subtly different, and incomplete resolution policy.

Proposed action

  1. Revert the changes in 3b00cfcae37cd1866de43205059039c1576404ff (both EditContextDataAnnotationsExtensions.cs and EditContextDataAnnotationsExtensionsTest.cs).
  2. Leave DataAnnotationsValidator throws AmbiguousMatchException when there are hidden members #27095 closed. It was opened in October 2020, moved to the backlog the same day, and has seen very little community engagement in the nearly five years since. A supported workaround (drop the new keyword or rename the shadowing property) was provided early in the thread and still applies. Given the low demand and the availability of a workaround, this doesn't meet the bar for a dedicated fix right now.
  3. Keep the current AmbiguousMatchException behavior as-is after the revert. We are intentionally not introducing a new targeted exception or silent-resolution policy; the existing loud failure is acceptable and is the clearest signal for the unsupported new-hiding shape.
  4. Add a short comment on the closed DataAnnotationsValidator throws AmbiguousMatchException when there are hidden members #27095 explaining the revert and the workaround (draft below), so anyone who lands on the issue understands the current state.

Suggested comment to post on the closed #27095

Following up here: the fix that referenced this issue (#67075) has been reverted because it did not fully resolve the AmbiguousMatchException (it's still reachable when the new-hiding happens on an intermediate base type, not the runtime type) and it introduced a silent, reference-type-dependent change in which validation attributes apply.

We're leaving this issue closed for now. The AmbiguousMatchException behavior is back to what it was before the PR. The supported workaround remains: avoid new-hiding a validated property — rename the property or drop the new keyword so there's a single declaration. If this scenario becomes a broader pain point, we can reconsider a complete fix (most-derived-wins resolution consistent with PropertyHelper.GetVisibleProperties).

Links

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

Metadata

Metadata

Labels

area-blazor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions