forked from qodo-benchmark/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Blazor supports DisplayName for models
#29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomerqodo
wants to merge
17
commits into
coderabbit_full_base_blazor_supports_displayname_for_models_pr4
Choose a base branch
from
coderabbit_full_head_blazor_supports_displayname_for_models_pr4
base: coderabbit_full_base_blazor_supports_displayname_for_models_pr4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d9e6138
API proposal.
ilonatommy 98348b1
Unit tests.
ilonatommy 79aa210
Add E2E tests.
ilonatommy f2a48e9
Alphabetically.
ilonatommy 9c21366
Update templates to use the new feature.
ilonatommy 1403178
Cleanup.
ilonatommy d93ac50
Update src/Components/Web/src/Forms/DisplayNameLabel.cs
ilonatommy c284b24
Allign null operations.
ilonatommy ba6aa50
Feedback: remove `Label` from the name to avoid misunderstandings.
ilonatommy b031f3b
Fix merge error
ilonatommy ffa638d
Feedback: use same caching approach as `FieldIdentifier` uses.
ilonatommy 047e7b6
Fix build.
ilonatommy e026ea0
Feedback: use the infrastructure from `ValidationMessage`.
ilonatommy cfea382
Feedback: Make sure localization is supported.
ilonatommy 62ac049
Feedback: helper clas can handle cache.
ilonatommy ca4d4dd
Update src/Components/Web/src/Forms/DisplayName.cs
ilonatommy 6d720c5
update pr
tomerqodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| private static readonly ConcurrentDictionary<MemberInfo, string> _displayNameCache = new(); | ||
|
|
||
| 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>(); | ||
| 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() | ||
| { | ||
| _memberInfoCache.Clear(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_displayNameCacheis declared but never used.The
_displayNameCachedictionary is declared butGetDisplayName(MemberInfo)doesn't use it - it recalculates the display name on every call. Either remove the unused cache or implement caching inGetDisplayName.🔧 Option 1: Remove unused cache
private static readonly ConcurrentDictionary<Expression, MemberInfo> _memberInfoCache = new(); - private static readonly ConcurrentDictionary<MemberInfo, string> _displayNameCache = new();🔧 Option 2: Implement caching in GetDisplayName
public static string GetDisplayName(MemberInfo member) { ArgumentNullException.ThrowIfNull(member); + return _displayNameCache.GetOrAdd(member, static m => + { - var displayAttribute = member.GetCustomAttribute<DisplayAttribute>(); + var displayAttribute = m.GetCustomAttribute<DisplayAttribute>(); - if (displayAttribute is not null) + if (displayAttribute is not null) - { + { - var name = displayAttribute.GetName(); + var name = displayAttribute.GetName(); - if (name is not null) + if (name is not null) - { + { - return name; + return name; - } + } - } + } - var displayNameAttribute = member.GetCustomAttribute<DisplayNameAttribute>(); + var displayNameAttribute = m.GetCustomAttribute<DisplayNameAttribute>(); - if (displayNameAttribute?.DisplayName is not null) + if (displayNameAttribute?.DisplayName is not null) - { + { - return displayNameAttribute.DisplayName; + return displayNameAttribute.DisplayName; - } + } - return member.Name; + return m.Name; + }); }Note: If implementing Option 2, consider whether
DisplayAttributewithResourceTypeshould be cached, as localized values may change based on culture. The current uncached approach is safer for localization scenarios.📝 Committable suggestion
🤖 Prompt for AI Agents