Skip to content

Commit

Permalink
Add backward compatibility to PrimitiveSerializers (#5280)
Browse files Browse the repository at this point in the history
* Add backward compatibility to PrimitiveSerializers

* Update API Approver list
  • Loading branch information
Arkatufus authored Sep 16, 2021
1 parent 708b539 commit 6101fea
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ namespace Akka.Remote.Serialization
}
public sealed class PrimitiveSerializers : Akka.Serialization.SerializerWithStringManifest
{
public PrimitiveSerializers(Akka.Actor.ExtendedActorSystem system) { }
public PrimitiveSerializers(Akka.Actor.ExtendedActorSystem system, Akka.Configuration.Config config) { }
public override object FromBinary(byte[] bytes, string manifest) { }
public override string Manifest(object obj) { }
public override byte[] ToBinary(object obj) { }
Expand Down
8 changes: 8 additions & 0 deletions src/core/Akka.Remote.Tests/RemoteConfigSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public void Remoting_should_contain_correct_BatchWriter_settings_in_ReferenceCon
s.BatchWriterSettings.EnableBatching.Should().BeTrue();
s.BatchWriterSettings.MaxExplicitFlushes.Should().Be(BatchWriterSettings.DefaultMaxPendingWrites);
}

[Fact]
public void Remoting_should_contain_correct_PrimitiveSerializer_settings_in_ReferenceConf()
{
var c = RARP.For(Sys).Provider.RemoteSettings.Config.GetConfig("akka.actor.serialization-settings.primitive");
c.Should().NotBeNull();
c.GetBoolean("use-neutral-primitives").Should().BeFalse();
}
}
}

43 changes: 43 additions & 0 deletions src/core/Akka.Remote.Tests/Serialization/BugFix5279Spec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// //-----------------------------------------------------------------------
// // <copyright file="BugFix5279Spec.cs" company="Akka.NET Project">
// // Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// // Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// // </copyright>
// //-----------------------------------------------------------------------

using Akka.Actor;
using Akka.Configuration;
using Akka.Remote.Serialization;
using Akka.TestKit;
using Akka.Util;
using Xunit;
using FluentAssertions;

namespace Akka.Remote.Tests.Serialization
{
public class BugFix5279Spec: AkkaSpec
{
[Theory]
[InlineData(1, "I")]
[InlineData(1L, "L")]
[InlineData("1", "S")]
public void PrimitiveSerializer_without_useNeutralPrimitives_should_return_custom_manifest(object data, string manifest)
{
var config = ConfigurationFactory.ParseString("use-neutral-primitives = off");
var serializer = new PrimitiveSerializers((ExtendedActorSystem)Sys, config);
serializer.Manifest(data).Should().Be(manifest);
}

[Theory]
[InlineData(1)]
[InlineData(1L)]
[InlineData("1")]
public void PrimitiveSerializer_without_useNeutralPrimitives_should_return_type_manifest(object data)
{
var config = ConfigurationFactory.ParseString("use-neutral-primitives = on");
var serializer = new PrimitiveSerializers((ExtendedActorSystem)Sys, config);
serializer.Manifest(data).Should().Be(data.GetType().TypeQualifiedName());
}

}
}
6 changes: 5 additions & 1 deletion src/core/Akka.Remote/Configuration/Remote.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
# actor.jar, because otherwise they would be repeated in config rendering.

akka {

actor {
serialization-settings {
primitive {
use-neutral-primitives = off
}
}

serializers {
akka-containers = "Akka.Remote.Serialization.MessageContainerSerializer, Akka.Remote"
Expand Down
12 changes: 11 additions & 1 deletion src/core/Akka.Remote/Serialization/PrimitiveSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Text;
using Akka.Actor;
using Akka.Configuration;
using Akka.Serialization;
using Akka.Util;

Expand All @@ -29,12 +30,18 @@ public sealed class PrimitiveSerializers : SerializerWithStringManifest
internal const string Int32ManifestNetFx = "System.Int32, mscorlib";
internal const string Int64ManifestNetFx = "System.Int64, mscorlib";

private readonly bool _useNeutralPrimitives;

/// <summary>
/// Initializes a new instance of the <see cref="PrimitiveSerializers" /> class.
/// </summary>
/// <param name="system">The actor system to associate with this serializer. </param>
public PrimitiveSerializers(ExtendedActorSystem system) : base(system)
public PrimitiveSerializers(ExtendedActorSystem system, Config config) : base(system)
{
if (config == null)
throw new ConfigurationException("configuration is null");

_useNeutralPrimitives = config.GetBoolean("use-neutral-primitives");
}

/// <inheritdoc />
Expand Down Expand Up @@ -78,6 +85,9 @@ public override object FromBinary(byte[] bytes, string manifest)
/// <inheritdoc />
public override string Manifest(object obj)
{
if (_useNeutralPrimitives)
return obj.GetType().TypeQualifiedName();

switch (obj)
{
case string _:
Expand Down

0 comments on commit 6101fea

Please sign in to comment.