Skip to content

Commit

Permalink
Allow using nullable complex types (and arrays) in the message types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega authored Mar 30, 2021
1 parent a8215bf commit c12d359
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
20 changes: 17 additions & 3 deletions src/NServiceBus.Core.Tests/MessageMapper/MessageMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,29 @@ public void CreateInstance_WhenMessageInitialized_ShouldBeThreadsafe()
});
}

#if NETCOREAPP
#nullable enable
[Test]
public void Should_handle_messages_with_nullable_reference_types()
{
var mapper = new MessageMapper();

// Type defined in separate assembly as a workaround
// because we can't use nullable refeference types yet
mapper.CreateInstance<WithDodgyNullable.IMyMessage>();
mapper.CreateInstance<IMessageWithNullableProperties>();
}

public interface IMessageWithNullableProperties : NServiceBus.ICommand, NServiceBus.IMessage
{
string? NullableString { get; set; }
object[]? NullableArray { get; set; }
List<NullableComplexTypeItem>? NullableList { get; set; }
}

public class NullableComplexTypeItem
{
}
#nullable disable
#endif

[Test]
public void CreateInstance_WhenMessageNotInitialized_ShouldBeThreadsafe()
{
Expand Down Expand Up @@ -253,5 +266,6 @@ public interface IMessageInterfaceWithNullablePropertyAttribute
object Value { get; set; }
}


}
}
6 changes: 0 additions & 6 deletions src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
<Reference Include="System.Transactions" />
</ItemGroup>

<!-- Workaround reference because we can't use nullable reference types yet -->
<ItemGroup>
<Reference Include="WithDodgyNullable" HintPath="TestDlls\WithDodgyNullable.dll" />
</ItemGroup>
<!-- End workaround -->

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Mono.Cecil" Version="0.10.4" />
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace NServiceBus
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand Down Expand Up @@ -99,26 +100,39 @@ static void AddCustomAttributeToProperty(CustomAttributeData attributeData, Prop
{
var namedArguments = attributeData.NamedArguments;

object[] constructorArgs = attributeData.ConstructorArguments.Select(ExtractValue).ToArray();
if (namedArguments == null)
{
var attributeBuilder = new CustomAttributeBuilder(
attributeData.Constructor,
attributeData.ConstructorArguments.Select(x => x.Value).ToArray());
constructorArgs);

propBuilder.SetCustomAttribute(attributeBuilder);
}
else
{
PropertyInfo[] namedProperties = namedArguments.Select(x => (PropertyInfo)x.MemberInfo).ToArray();
object[] propertyValues = namedArguments.Select(x => x.TypedValue.Value).ToArray();

var attributeBuilder = new CustomAttributeBuilder(
attributeData.Constructor,
attributeData.ConstructorArguments.Select(x => x.Value).ToArray(),
namedArguments.Select(x => (PropertyInfo)x.MemberInfo).ToArray(),
namedArguments.Select(x => x.TypedValue.Value).ToArray());
constructorArgs,
namedProperties,
propertyValues);

propBuilder.SetCustomAttribute(attributeBuilder);
}
}

static object ExtractValue(CustomAttributeTypedArgument arg)
{
if (arg.Value is ReadOnlyCollection<CustomAttributeTypedArgument> nestedValue)
{
return nestedValue.Select(x => x.Value).ToArray();
}
return arg.Value;
}

/// <summary>
/// Returns all properties on the given type, going up the inheritance hierarchy.
/// </summary>
Expand Down

0 comments on commit c12d359

Please sign in to comment.