Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Components/Web/src/Forms/DisplayName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Components.Rendering;

namespace Microsoft.AspNetCore.Components.Forms
{
/// <summary>
/// Displays the display name for a specified field, reading from <see cref="DisplayAttribute"/>
/// or <see cref="DisplayNameAttribute"/> if present, or falling back to the property name.
/// </summary>
/// <typeparam name="TValue">The type of the field.</typeparam>
public class DisplayName<TValue> : IComponent
{

private RenderHandle _renderHandle;
private Expression<Func<TValue>>? _previousFieldAccessor;
private string? _displayName;

/// <summary>
/// Specifies the field for which the display name should be shown.
/// </summary>
[Parameter, EditorRequired]
public Expression<Func<TValue>>? For { get; set; }
/// <inheritdoc />
void IComponent.Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}

/// <inheritdoc />
Task IComponent.SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);

if (For is null)
{
throw new InvalidOperationException($"{GetType()} requires a value for the " +
$"{nameof(For)} parameter.");
}

// Only recalculate if the expression changed
if (For != _previousFieldAccessor)
{
var newDisplayName = ExpressionMemberAccessor.GetDisplayName(For);

_displayName = newDisplayName;
_renderHandle.Render(BuildRenderTree);

_previousFieldAccessor = For;
}

return Task.CompletedTask;
}

private void BuildRenderTree(RenderTreeBuilder builder)
{
builder.AddContent(0, _displayName);
}
}
}
87 changes: 87 additions & 0 deletions src/Components/Web/src/Forms/ExpressionMemberAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNetCore.Components.HotReload;

namespace Microsoft.AspNetCore.Components.Forms;

internal static class ExpressionMemberAccessor
{
private static readonly ConcurrentDictionary<Expression, MemberInfo> _memberInfoCache = new();

Choose a reason for hiding this comment

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

⚠️ Performance: _memberInfoCache keyed on Expression uses reference equality

_memberInfoCache is a ConcurrentDictionary<Expression, MemberInfo>. The Expression type does not override Equals/GetHashCode, so the dictionary uses default reference equality. This means:

  1. Each Blazor render cycle creates a new Expression<Func<TValue>> instance (e.g., () => model.Name), so the cache key will be a different reference each time.
  2. The cache will accumulate entries that are never hit again, effectively acting as a memory leak rather than a useful cache.
  3. The GetOrAdd factory runs on every call, performing the expression tree parsing every time.

Compare with FieldIdentifier, which caches by (Type ModelType, MemberInfo Member) — a value tuple that uses structural equality — not by the Expression object itself.

Fix: Cache by MemberInfo directly, or restructure so that after parsing the expression once (which is cheap), you cache the expensive part (display name resolution) by MemberInfo. Since _displayNameCache already uses MemberInfo as the key but is unused, the simplest fix is to remove _memberInfoCache entirely and use _displayNameCache properly. The expression-to-member parsing is not expensive enough to warrant caching.

Was this helpful? React with 👍 / 👎

  • Apply suggested fix

private static readonly ConcurrentDictionary<MemberInfo, string> _displayNameCache = new();

Choose a reason for hiding this comment

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

⚠️ Performance: _displayNameCache declared but never used — display names not cached

The _displayNameCache ConcurrentDictionary<MemberInfo, string> is declared on line 16 but never read from or written to anywhere in the class. The GetDisplayName(MemberInfo) method (lines 53-74) always performs reflection via GetCustomAttribute calls on every invocation instead of caching results.

Additionally, ClearCache() only clears _memberInfoCache and not _displayNameCache, which further confirms this was intended to be used but was left incomplete.

This means every render cycle for a DisplayName component triggers reflection to resolve display names — the caching that was supposed to be implemented (per the PR description: "same caching approach as FieldIdentifier") is not actually working for display name resolution.

Fix: Use _displayNameCache.GetOrAdd(member, ResolveDisplayName) in GetDisplayName(MemberInfo), and also clear _displayNameCache in ClearCache().

Was this helpful? React with 👍 / 👎

Suggested fix
    public static string GetDisplayName(MemberInfo member)
    {
        ArgumentNullException.ThrowIfNull(member);

        return _displayNameCache.GetOrAdd(member, static m =>
        {
            var displayAttribute = m.GetCustomAttribute<DisplayAttribute>();
            if (displayAttribute is not null)
            {
                var name = displayAttribute.GetName();
                if (name is not null)
                {
                    return name;
                }
            }

            var displayNameAttribute = m.GetCustomAttribute<DisplayNameAttribute>();
            if (displayNameAttribute?.DisplayName is not null)
            {
                return displayNameAttribute.DisplayName;
            }

            return m.Name;
        });
    }
  • Apply suggested fix


static ExpressionMemberAccessor()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

private static MemberInfo GetMemberInfo<TValue>(Expression<Func<TValue>> accessor)
{
ArgumentNullException.ThrowIfNull(accessor);

return _memberInfoCache.GetOrAdd(accessor, static expr =>
{
var lambdaExpression = (LambdaExpression)expr;
var accessorBody = lambdaExpression.Body;

if (accessorBody is UnaryExpression unaryExpression
&& unaryExpression.NodeType == ExpressionType.Convert
&& unaryExpression.Type == typeof(object))
{
accessorBody = unaryExpression.Operand;
}

if (accessorBody is not MemberExpression memberExpression)
{
throw new ArgumentException(
$"The provided expression contains a {accessorBody.GetType().Name} which is not supported. " +
$"Only simple member accessors (fields, properties) of an object are supported.");
}

return memberExpression.Member;
});
}

public static string GetDisplayName(MemberInfo member)
{
ArgumentNullException.ThrowIfNull(member);

var displayAttribute = member.GetCustomAttribute<DisplayAttribute>();

Choose a reason for hiding this comment

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

⚠️ Bug: Caching localized display names prevents language switching

The GetDisplayName(MemberInfo) method calls displayAttribute.GetName() which resolves localized strings from resource files based on the current culture. If the display name is cached (either now via _displayNameCache or once the caching bug is fixed), the cached value will be for whichever culture was active on first access. Subsequent requests in a different culture will return the stale cached value.

For example, if a user first visits in en-US and sees "Product Name", then switches to fr-FR, they'd still see "Product Name" instead of "Nom du produit" — because the cached result won't be re-evaluated.

The PR explicitly adds localization test cases (DisplayNameComponent.razor with TestResources) and the PR description calls out localization support, so this is a real scenario.

Fix: When the DisplayAttribute has a ResourceType set (indicating localization), don't cache the result — or include the current culture in the cache key. For non-localized names, caching is safe.

Was this helpful? React with 👍 / 👎

  • Apply suggested fix

if (displayAttribute is not null)
{
var name = displayAttribute.GetName();
if (name is not null)
{
return name;
}
}

var displayNameAttribute = member.GetCustomAttribute<DisplayNameAttribute>();
if (displayNameAttribute?.DisplayName is not null)
{
return displayNameAttribute.DisplayName;
}

return member.Name;
}

public static string GetDisplayName<TValue>(Expression<Func<TValue>> accessor)
{
ArgumentNullException.ThrowIfNull(accessor);
var member = GetMemberInfo(accessor);
return GetDisplayName(member);
}

private static void ClearCache()

Choose a reason for hiding this comment

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

⚠️ Bug: ClearCache doesn't clear _displayNameCache on hot reload

The ClearCache() method (line 83-86) only clears _memberInfoCache but not _displayNameCache. If display name caching is implemented (as it should be — see related finding), hot reload will not invalidate cached display names. This means developers who modify [Display(Name = "...")] attributes during development won't see updated display names until they restart the application.

This directly defeats the purpose of the hot reload integration.

Fix: Also clear _displayNameCache in ClearCache().

Was this helpful? React with 👍 / 👎

Suggested change
private static void ClearCache()
private static void ClearCache()
{
_memberInfoCache.Clear();
_displayNameCache.Clear();
}
  • Apply suggested fix

{
_memberInfoCache.Clear();
}
}
4 changes: 4 additions & 0 deletions src/Components/Web/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ Microsoft.AspNetCore.Components.Web.Media.MediaSource.MediaSource(byte[]! data,
Microsoft.AspNetCore.Components.Web.Media.MediaSource.MediaSource(System.IO.Stream! stream, string! mimeType, string! cacheKey) -> void
Microsoft.AspNetCore.Components.Web.Media.MediaSource.MimeType.get -> string!
Microsoft.AspNetCore.Components.Web.Media.MediaSource.Stream.get -> System.IO.Stream!
Microsoft.AspNetCore.Components.Forms.DisplayName<TValue>
Microsoft.AspNetCore.Components.Forms.DisplayName<TValue>.DisplayName() -> void
Microsoft.AspNetCore.Components.Forms.DisplayName<TValue>.For.get -> System.Linq.Expressions.Expression<System.Func<TValue>!>?
Microsoft.AspNetCore.Components.Forms.DisplayName<TValue>.For.set -> void
232 changes: 232 additions & 0 deletions src/Components/Web/test/Forms/DisplayNameTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Test.Helpers;

namespace Microsoft.AspNetCore.Components.Forms;

public class DisplayNameTest
{
[Fact]
public async Task ThrowsIfNoForParameterProvided()
{
// Arrange
var rootComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<string>>(0);
builder.CloseComponent();
}
};

var testRenderer = new TestRenderer();
var componentId = testRenderer.AssignRootComponentId(rootComponent);

// Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await testRenderer.RenderRootComponentAsync(componentId));
Assert.Contains("For", ex.Message);
Assert.Contains("parameter", ex.Message);
}

[Fact]
public async Task DisplaysPropertyNameWhenNoAttributePresent()
{
// Arrange
var model = new TestModel();
var rootComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<string>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PlainProperty));
builder.CloseComponent();
}
};

// Act
var output = await RenderAndGetOutput(rootComponent);

// Assert
Assert.Equal("PlainProperty", output);
}

[Fact]
public async Task DisplaysDisplayAttributeName()
{
// Arrange
var model = new TestModel();
var rootComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<string>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithDisplayAttribute));
builder.CloseComponent();
}
};

// Act
var output = await RenderAndGetOutput(rootComponent);

// Assert
Assert.Equal("Custom Display Name", output);
}

[Fact]
public async Task DisplaysDisplayNameAttributeName()
{
// Arrange
var model = new TestModel();
var rootComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<string>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithDisplayNameAttribute));
builder.CloseComponent();
}
};

// Act
var output = await RenderAndGetOutput(rootComponent);

// Assert
Assert.Equal("Custom DisplayName", output);
}

[Fact]
public async Task DisplayAttributeTakesPrecedenceOverDisplayNameAttribute()
{
// Arrange
var model = new TestModel();
var rootComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<string>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithBothAttributes));
builder.CloseComponent();
}
};

// Act
var output = await RenderAndGetOutput(rootComponent);

// Assert
// DisplayAttribute should take precedence per MVC conventions
Assert.Equal("Display Takes Precedence", output);
}

[Fact]
public async Task WorksWithDifferentPropertyTypes()
{
// Arrange
var model = new TestModel();
var intComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<int>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<int>>)(() => model.IntProperty));
builder.CloseComponent();
}
};
var dateComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<DateTime>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<DateTime>>)(() => model.DateProperty));
builder.CloseComponent();
}
};

// Act
var intOutput = await RenderAndGetOutput(intComponent);
var dateOutput = await RenderAndGetOutput(dateComponent);

// Assert
Assert.Equal("Integer Value", intOutput);
Assert.Equal("Date Value", dateOutput);
}

[Fact]
public async Task SupportsLocalizationWithResourceType()
{
var model = new TestModel();
var rootComponent = new TestHostComponent
{
InnerContent = builder =>
{
builder.OpenComponent<DisplayName<string>>(0);
builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithResourceBasedDisplay));
builder.CloseComponent();
}
};

var output = await RenderAndGetOutput(rootComponent);
Assert.Equal("Localized Display Name", output);
}

private static async Task<string> RenderAndGetOutput(TestHostComponent rootComponent)
{
var testRenderer = new TestRenderer();
var componentId = testRenderer.AssignRootComponentId(rootComponent);
await testRenderer.RenderRootComponentAsync(componentId);

var batch = testRenderer.Batches.Single();
var displayLabelComponentFrame = batch.ReferenceFrames
.First(f => f.FrameType == RenderTree.RenderTreeFrameType.Component &&
f.Component is DisplayName<string> or DisplayName<int> or DisplayName<DateTime>);

// Find the text content frame within the component
var textFrame = batch.ReferenceFrames
.First(f => f.FrameType == RenderTree.RenderTreeFrameType.Text);

return textFrame.TextContent;
}

private class TestHostComponent : ComponentBase
{
public RenderFragment InnerContent { get; set; }

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
InnerContent(builder);
}
}

private class TestModel
{
public string PlainProperty { get; set; } = string.Empty;

[Display(Name = "Custom Display Name")]
public string PropertyWithDisplayAttribute { get; set; } = string.Empty;

[DisplayName("Custom DisplayName")]
public string PropertyWithDisplayNameAttribute { get; set; } = string.Empty;

[Display(Name = "Display Takes Precedence")]
[DisplayName("This Should Not Be Used")]
public string PropertyWithBothAttributes { get; set; } = string.Empty;

[Display(Name = "Integer Value")]
public int IntProperty { get; set; }

[Display(Name = "Date Value")]
public DateTime DateProperty { get; set; }

[Display(Name = nameof(TestResources.LocalizedDisplayName), ResourceType = typeof(TestResources))]
public string PropertyWithResourceBasedDisplay { get; set; } = string.Empty;
}

public static class TestResources
{
public static string LocalizedDisplayName => "Localized Display Name";
}
}
Loading