From c7dea6e0eeca7f51de6d5f1d91831c5117b55022 Mon Sep 17 00:00:00 2001 From: Jo Palac Date: Thu, 12 Oct 2023 10:37:59 +1000 Subject: [PATCH 1/3] Merge pull request #6881 from Particular/databus_systemJsonSerializer_bug Prevent Type property on DataBusProperty from being serialized --- ...us_properties_with_systemjsonserializer.cs | 85 +++++++++++++++++++ ...IApprovals.ApproveNServiceBus.approved.txt | 1 + .../DataBus/DataBusProperty.cs | 2 + 3 files changed, 88 insertions(+) create mode 100644 src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs diff --git a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs new file mode 100644 index 00000000000..a56dabcea60 --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs @@ -0,0 +1,85 @@ +namespace NServiceBus.AcceptanceTests.DataBus +{ + using System.IO; + using System.Threading.Tasks; + using AcceptanceTesting; + using AcceptanceTesting.Customization; + using EndpointTemplates; + using NUnit.Framework; + + public class When_sending_databus_properties_with_systemjsonserializer : NServiceBusAcceptanceTest + { + [Test] + public async Task Should_receive_messages_with_largepayload_correctly() + { + var payloadToSend = new byte[PayloadSize]; + + var context = await Scenario.Define() + .WithEndpoint(b => b.When(session => session.Send(new MyMessageWithLargePayload + { + Payload = new DataBusProperty(payloadToSend) + }))) + .WithEndpoint() + .Done(c => c.ReceivedPayload != null) + .Run(); + + Assert.AreEqual(payloadToSend, context.ReceivedPayload, "The large payload should be marshalled correctly using the databus"); + } + + const int PayloadSize = 500; + + public class Context : ScenarioContext + { + public byte[] ReceivedPayload { get; set; } + } + + public class Sender : EndpointConfigurationBuilder + { + public Sender() + { + EndpointSetup(builder => + { + var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); + builder.UseDataBus().BasePath(basePath); + builder.UseSerialization(); + builder.ConfigureRouting().RouteToEndpoint(typeof(MyMessageWithLargePayload), typeof(Receiver)); + }); + } + } + + public class Receiver : EndpointConfigurationBuilder + { + public Receiver() + { + EndpointSetup(builder => + { + var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); + builder.UseDataBus().BasePath(basePath); + builder.UseSerialization(); + }); + } + + public class MyMessageHandler : IHandleMessages + { + public MyMessageHandler(Context context) + { + testContext = context; + } + + public Task Handle(MyMessageWithLargePayload messageWithLargePayload, IMessageHandlerContext context) + { + testContext.ReceivedPayload = messageWithLargePayload.Payload.Value; + + return Task.CompletedTask; + } + + Context testContext; + } + } + + public class MyMessageWithLargePayload : ICommand + { + public DataBusProperty Payload { get; set; } + } + } +} \ No newline at end of file diff --git a/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt b/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt index 12bd14ee08f..1b689bec001 100644 --- a/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt +++ b/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt @@ -211,6 +211,7 @@ namespace NServiceBus protected DataBusProperty(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public bool HasValue { get; set; } public string Key { get; set; } + [System.Text.Json.Serialization.JsonIgnore] public System.Type Type { get; } public T Value { get; } public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index 9a61886fea5..6b210d79e1b 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -3,6 +3,7 @@ using System; using System.Runtime.Serialization; using System.Security; + using System.Text.Json.Serialization; /// /// Default implementation for . @@ -54,6 +55,7 @@ protected DataBusProperty(SerializationInfo info, StreamingContext context) /// /// The property . /// + [JsonIgnore] public Type Type { get; } /// From 3aade509daf6efc095bb0ad786b01afb7ddca955 Mon Sep 17 00:00:00 2001 From: Jo Palac Date: Thu, 12 Oct 2023 11:18:57 +1000 Subject: [PATCH 2/3] fix for Value beingserialized --- ...atabus_properties_with_systemjsonserializer.cs | 15 +++++++++++++++ src/NServiceBus.Core/DataBus/DataBusProperty.cs | 1 + 2 files changed, 16 insertions(+) diff --git a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs index a56dabcea60..d2b877f372e 100644 --- a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs +++ b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs @@ -1,10 +1,12 @@ namespace NServiceBus.AcceptanceTests.DataBus { + using System; using System.IO; using System.Threading.Tasks; using AcceptanceTesting; using AcceptanceTesting.Customization; using EndpointTemplates; + using NServiceBus.MessageMutator; using NUnit.Framework; public class When_sending_databus_properties_with_systemjsonserializer : NServiceBusAcceptanceTest @@ -56,6 +58,7 @@ public Receiver() var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); builder.UseDataBus().BasePath(basePath); builder.UseSerialization(); + builder.RegisterMessageMutator(new Mutator()); }); } @@ -77,6 +80,18 @@ public Task Handle(MyMessageWithLargePayload messageWithLargePayload, IMessageHa } } + public class Mutator : IMutateIncomingTransportMessages + { + public Task MutateIncoming(MutateIncomingTransportMessageContext context) + { + if (context.Body.Length > PayloadSize) + { + throw new Exception("The message body is too large, which means the DataBus was not used to transfer the payload."); + } + return Task.CompletedTask; + } + } + public class MyMessageWithLargePayload : ICommand { public DataBusProperty Payload { get; set; } diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index 6b210d79e1b..ec97b1231fa 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -50,6 +50,7 @@ protected DataBusProperty(SerializationInfo info, StreamingContext context) /// /// The value. /// + [JsonIgnore] public T Value => value; /// From c82947c4843dbf719225ebb146fd42275ea5ae7e Mon Sep 17 00:00:00 2001 From: Jo Palac Date: Thu, 12 Oct 2023 11:50:10 +1000 Subject: [PATCH 3/3] fix approval test --- .../ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt b/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt index 1b689bec001..0b0a1db2eda 100644 --- a/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt +++ b/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt @@ -213,6 +213,7 @@ namespace NServiceBus public string Key { get; set; } [System.Text.Json.Serialization.JsonIgnore] public System.Type Type { get; } + [System.Text.Json.Serialization.JsonIgnore] public T Value { get; } public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public object GetValue() { }