Skip to content

Commit

Permalink
Error when using JsonMessageSerializer with default settings #252
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
  • Loading branch information
zarusz committed Apr 24, 2024
1 parent 5faa08a commit 1b74fea
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Host.Plugin.Properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Import Project="Common.NuGet.Properties.xml" />

<PropertyGroup>
<Version>2.3.2</Version>
<Version>2.3.3</Version>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ public class JsonMessageSerializer : IMessageSerializer

public JsonMessageSerializer(JsonSerializerOptions options = null)
{
Options = options ?? new(JsonSerializerDefaults.Web)
Options = options ?? CreateDefaultOptions();
}

public virtual JsonSerializerOptions CreateDefaultOptions()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
AllowTrailingCommas = true
};
Options.Converters.Add(new ObjectToInferredTypesConverter());
options.Converters.Add(new ObjectToInferredTypesConverter());
return options;
}

public byte[] Serialize(Type t, object message) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static class SerializationBuilderExtensions
public static TBuilder AddJsonSerializer<TBuilder>(this TBuilder builder, JsonSerializerOptions options = null)
where TBuilder : ISerializationBuilder
{
builder.RegisterSerializer<JsonMessageSerializer>(services => {
builder.RegisterSerializer<JsonMessageSerializer>(services =>
{
services.TryAddSingleton(svp => new JsonMessageSerializer(options ?? svp.GetService<JsonSerializerOptions>()));
});
return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace SlimMessageBus.Host.Serialization.SystemTextJson.Test;

using FluentAssertions;

public class JsonMessageSerializerTests
{
Expand Down Expand Up @@ -29,4 +28,49 @@ public void When_SerializeAndDeserialize_Given_TypeObject_Then_TriesToInferPrimi
// assert
deserializedValue.Should().Be(expectedValue);
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_RegisterSerializer_Then_UsesOptionsFromContainerIfAvailable(bool jsonOptionComeFromContainer)
{
// arrange
var mbb = MessageBusBuilder.Create();
mbb.AddJsonSerializer();

var services = new ServiceCollection();

var jsonOptions = new JsonSerializerOptions();

// Simulate the options have been used in an serializer already (see https://github.com/zarusz/SlimMessageBus/issues/252)
// Modifying options (adding converters) when the options are already in use by a serializer will throw an exception: System.InvalidOperationException : This JsonSerializerOptions instance is read-only or has already been used in serialization or deserialization.
JsonSerializer.SerializeToUtf8Bytes(new Dictionary<string, object>(), typeof(Dictionary<string, object>), jsonOptions);

if (jsonOptionComeFromContainer)
{
services.AddSingleton(jsonOptions);
}

foreach (var action in mbb.PostConfigurationActions)
{
action(services);
}

var serviceProvider = services.BuildServiceProvider();

var subject = serviceProvider.GetRequiredService<JsonMessageSerializer>();

// act
if (jsonOptionComeFromContainer)
{
subject.Options.Should().BeSameAs(jsonOptions);
}
else
{
subject.Options.Should().NotBeSameAs(jsonOptions);
}

// assert
serviceProvider.GetService<IMessageSerializer>().Should().BeSameAs(subject);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
global using System.Text.Json;

global using FluentAssertions;

global using Microsoft.Extensions.DependencyInjection;
Expand Down

0 comments on commit 1b74fea

Please sign in to comment.