Skip to content

Commit 70092f7

Browse files
jhartmann123davidroth
authored andcommitted
improvement: Out of band jobs with generic message types now also output the names of the generic arguments
1 parent aae4991 commit 70092f7

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>9.5.2</Version>
3+
<Version>9.5.3</Version>
44
<TargetFramework>net8.0</TargetFramework>
55
</PropertyGroup>
66

src/Hangfire/src/MediatorHandlerContext.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public MediatorHandlerContext(object message, string handlerType)
1414
HandlerType = handlerType;
1515

1616
var type = Message.GetType();
17-
DisplayName = $"Message: {type.Name} ({type.Namespace})";
17+
DisplayName = $"Message: {GetTypeName(type)} ({type.Namespace})";
1818
}
1919

2020
public object Message { get; set; }
@@ -26,6 +26,20 @@ public MediatorHandlerContext(object message, string handlerType)
2626

2727
public override string ToString() => DisplayName;
2828

29+
private string GetTypeName(Type type)
30+
{
31+
if (!type.IsGenericType)
32+
return type.Name;
33+
34+
// Remove the `n suffix from generic type names (e.g. List`1 -> List)
35+
var index = type.Name.IndexOf('`', StringComparison.Ordinal);
36+
var typeName = index > 0 ? type.Name[..index] : type.Name;
37+
38+
var genericNames = string.Join(", ", type.GetGenericArguments().Select(GetTypeName));
39+
40+
return $"{typeName}<{genericNames}>";
41+
}
42+
2943
public class HangfireUser(List<HangfireUser.HangfireUserClaim> claims)
3044
{
3145
public List<HangfireUserClaim> Claims { get; } = claims;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Fusonic GmbH. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE file in the project root for license information.
3+
4+
using FluentAssertions;
5+
using Xunit;
6+
7+
namespace Fusonic.Extensions.Hangfire.Tests;
8+
9+
public class MediatorHandlerContextTests
10+
{
11+
[Theory]
12+
[InlineData(typeof(int), "Message: Int32 (System)")]
13+
[InlineData(typeof(List<int>), "Message: List<Int32> (System.Collections.Generic)")]
14+
[InlineData(typeof(List<List<int>>), "Message: List<List<Int32>> (System.Collections.Generic)")]
15+
[InlineData(typeof(List<List<List<int>>>), "Message: List<List<List<Int32>>> (System.Collections.Generic)")]
16+
public void DisplayName_ContainsMessageType_WithFriendlyGenericArgs(Type type, string expectedString)
17+
{
18+
var message = Activator.CreateInstance(type)!;
19+
var context = new MediatorHandlerContext(message, "HandlerType");
20+
21+
context.DisplayName.Should().Be(expectedString);
22+
}
23+
24+
[Fact]
25+
public void ToString_ReturnsDisplayName()
26+
{
27+
var context= new MediatorHandlerContext(new object(), "HandlerType");
28+
context.ToString().Should().Be("Message: Object (System)");
29+
30+
context.DisplayName = "Hallo";
31+
context.ToString().Should().Be("Hallo");
32+
}
33+
}

0 commit comments

Comments
 (0)