Skip to content

Commit e14a8ff

Browse files
Copilotjaviercn
andauthored
[Blazor] Fix GenerateMarkerKey to handle string keys properly (#62814)
* Fix GenerateMarkerKey to handle string keys and add comprehensive tests --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: javiercn <[email protected]>
1 parent 0791d1c commit e14a8ff

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Components/Endpoints/src/Rendering/SSRRenderModeBoundary.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ private ComponentMarkerKey GenerateMarkerKey(int sequence, object? componentKey)
212212
var sequenceString = sequence.ToString(CultureInfo.InvariantCulture);
213213

214214
var locationHash = $"{componentTypeNameHash}:{sequenceString}";
215-
var formattedComponentKey = (componentKey as IFormattable)?.ToString(null, CultureInfo.InvariantCulture) ?? string.Empty;
215+
var formattedComponentKey = componentKey switch
216+
{
217+
string str => str,
218+
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
219+
_ => string.Empty
220+
};
216221

217222
return new()
218223
{

src/Components/Endpoints/test/SSRRenderModeBoundaryTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,33 @@ public Task SetParametersAsync(ParameterView parameters)
9696
=> throw new NotImplementedException();
9797
}
9898

99+
public static IEnumerable<object[]> ComponentKeyTestData()
100+
{
101+
yield return new object[] { "test-string-key", "test-string-key" };
102+
yield return new object[] { 42, "42" };
103+
yield return new object[] { Guid.Parse("12345678-1234-1234-1234-123456789012"), "12345678-1234-1234-1234-123456789012" };
104+
yield return new object[] { 123.45, "123.45" };
105+
yield return new object[] { new DateTime(2023, 12, 25, 10, 30, 0, DateTimeKind.Utc), "12/25/2023 10:30:00" };
106+
yield return new object[] { null, string.Empty };
107+
yield return new object[] { new object(), string.Empty };
108+
}
109+
110+
[Theory]
111+
[MemberData(nameof(ComponentKeyTestData))]
112+
public void GetComponentMarkerKey_WorksWithVariousKeyTypes(object componentKey, string expectedFormattedKey)
113+
{
114+
// Arrange
115+
var httpContext = new DefaultHttpContext();
116+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
117+
118+
// Act
119+
var markerKey = boundary.GetComponentMarkerKey(1, componentKey);
120+
121+
// Assert
122+
Assert.Equal(expectedFormattedKey, markerKey.FormattedComponentKey);
123+
Assert.NotEmpty(markerKey.LocationHash);
124+
}
125+
99126
class ServerRenderModeSubclass : InteractiveServerRenderMode { }
100127
class WebAssemblyRenderModeSubclass : InteractiveWebAssemblyRenderMode { }
101128
class AutoRenderModeSubclass : InteractiveAutoRenderMode { }

0 commit comments

Comments
 (0)