Skip to content

[Blazor] Generate marker key doesn't work for strings. #62813

@javiercn

Description

@javiercn

private ComponentMarkerKey GenerateMarkerKey(int sequence, object? componentKey)
{
var componentTypeNameHash = _componentTypeNameHashCache.GetOrAdd(_componentType, TypeNameHash.Compute);
var sequenceString = sequence.ToString(CultureInfo.InvariantCulture);
var locationHash = $"{componentTypeNameHash}:{sequenceString}";
var formattedComponentKey = (componentKey as IFormattable)?.ToString(null, CultureInfo.InvariantCulture) ?? string.Empty;
return new()
{
LocationHash = locationHash,
FormattedComponentKey = formattedComponentKey,
};
}

The attached code fails when you provide a string as a key, since strings are not IFormattable. We should explicitly check if the key is already a string and use it directly.

We should also have a reasonable set of tests that validate that all common primitive types that we expect to work (ints, guids, and so on) successfully generate a key string.

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions