-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix GenerateMarkerKey to handle string keys properly in Blazor SSR #62814
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
Conversation
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
[Fact] | ||
public void GetComponentMarkerKey_WorksWithStringKey() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); | ||
var stringKey = "test-string-key"; | ||
|
||
// Act | ||
var markerKey = boundary.GetComponentMarkerKey(1, stringKey); | ||
|
||
// Assert | ||
Assert.Equal(stringKey, markerKey.FormattedComponentKey); | ||
Assert.NotEmpty(markerKey.LocationHash); | ||
} | ||
|
||
[Fact] | ||
public void GetComponentMarkerKey_WorksWithIntKey() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); | ||
var intKey = 42; | ||
|
||
// Act | ||
var markerKey = boundary.GetComponentMarkerKey(1, intKey); | ||
|
||
// Assert | ||
Assert.Equal("42", markerKey.FormattedComponentKey); | ||
Assert.NotEmpty(markerKey.LocationHash); | ||
} | ||
|
||
[Fact] | ||
public void GetComponentMarkerKey_WorksWithGuidKey() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); | ||
var guidKey = Guid.Parse("12345678-1234-1234-1234-123456789012"); | ||
|
||
// Act | ||
var markerKey = boundary.GetComponentMarkerKey(1, guidKey); | ||
|
||
// Assert | ||
Assert.Equal("12345678-1234-1234-1234-123456789012", markerKey.FormattedComponentKey); | ||
Assert.NotEmpty(markerKey.LocationHash); | ||
} | ||
|
||
[Fact] | ||
public void GetComponentMarkerKey_WorksWithDoubleKey() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); | ||
var doubleKey = 123.45; | ||
|
||
// Act | ||
var markerKey = boundary.GetComponentMarkerKey(1, doubleKey); | ||
|
||
// Assert | ||
Assert.Equal("123.45", markerKey.FormattedComponentKey); | ||
Assert.NotEmpty(markerKey.LocationHash); | ||
} | ||
|
||
[Fact] | ||
public void GetComponentMarkerKey_WorksWithDateTimeKey() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode()); | ||
var dateTimeKey = new DateTime(2023, 12, 25, 10, 30, 0, DateTimeKind.Utc); | ||
|
||
// Act | ||
var markerKey = boundary.GetComponentMarkerKey(1, dateTimeKey); | ||
|
||
// Assert | ||
Assert.Equal("12/25/2023 10:30:00", markerKey.FormattedComponentKey); | ||
Assert.NotEmpty(markerKey.LocationHash); | ||
} |
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.
Can you make all these tests into an xUnit theory instead?
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.
Done! Converted all the individual test methods to a single xUnit Theory with MemberData. This reduces the test code from 111 lines to 25 lines while maintaining the same test coverage. Commit be35c27
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
The
GenerateMarkerKey
method inSSRRenderModeBoundary
was failing when string values were passed as component keys because strings don't implementIFormattable
. This caused string keys to be converted to empty strings instead of preserving their actual values.Problem
The original code attempted to cast all component keys to
IFormattable
:When a string was passed as the key, the cast to
IFormattable
would fail (sincestring
doesn't implementIFormattable
), resulting in an empty string instead of the actual string value.Solution
Updated the method to use pattern matching with explicit handling for strings:
This ensures that:
IFormattable
types (int, Guid, DateTime, etc.) are properly formatted usingInvariantCulture
Tests Added
Added comprehensive tests for all common primitive types:
All tests verify that the keys are properly formatted and preserved in the
ComponentMarkerKey.FormattedComponentKey
property.Fixes #62813.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.