Skip to content

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

Merged
merged 3 commits into from
Jul 18, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 18, 2025

The GenerateMarkerKey method in SSRRenderModeBoundary was failing when string values were passed as component keys because strings don't implement IFormattable. 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:

var formattedComponentKey = (componentKey as IFormattable)?.ToString(null, CultureInfo.InvariantCulture) ?? string.Empty;

When a string was passed as the key, the cast to IFormattable would fail (since string doesn't implement IFormattable), resulting in an empty string instead of the actual string value.

Solution

Updated the method to use pattern matching with explicit handling for strings:

var formattedComponentKey = componentKey switch
{
    string str => str,
    IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
    _ => string.Empty
};

This ensures that:

  • String keys are used directly without formatting
  • IFormattable types (int, Guid, DateTime, etc.) are properly formatted using InvariantCulture
  • Non-formattable objects default to empty string

Tests Added

Added comprehensive tests for all common primitive types:

  • String keys (the main fix)
  • Integer keys
  • Guid keys
  • Double keys
  • DateTime keys
  • Null keys
  • Non-formattable object keys

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.

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Blazor] Generate marker key doesn't work for strings. Fix GenerateMarkerKey to handle string keys properly in Blazor SSR Jul 18, 2025
@Copilot Copilot AI requested a review from javiercn July 18, 2025 18:48
Copilot finished work on behalf of javiercn July 18, 2025 18:48
Comment on lines 99 to 177
[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);
}
Copy link
Member

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?

Copy link
Contributor Author

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>
Copilot finished work on behalf of javiercn July 18, 2025 18:57
@Copilot Copilot AI requested a review from javiercn July 18, 2025 18:57
@javiercn javiercn marked this pull request as ready for review July 18, 2025 19:02
@javiercn javiercn requested a review from a team as a code owner July 18, 2025 19:02
@javiercn javiercn enabled auto-merge (squash) July 18, 2025 19:03
@javiercn javiercn merged commit e14a8ff into main Jul 18, 2025
30 checks passed
@javiercn javiercn deleted the copilot/fix-62813 branch July 18, 2025 20:35
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jul 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Blazor] Generate marker key doesn't work for strings.
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy