Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to disable KeyValuePairSerializer #774

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/protobuf-net.Test/Serializers/ImplicitTupleOptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using ProtoBuf.Meta;
using Xunit;

namespace ProtoBuf.unittest.Serializers
{
public sealed class ImplicitTupleOptionTests
{
public class ImplicitTupleType
{
public int Value { get; }

public ImplicitTupleType(int value)
{
Value = value;
}
}

[Fact]
public void ImplicitTupleTypeFailsWhenOptionDisabled()
{
var model = RuntimeTypeModel.Create();
model.AllowImplicitTuples = false;
var kv = new KeyValuePair<int, string>(0xdead, "hello");
Assert.Throws<InvalidOperationException>(() => model.DeepClone(kv));
}

[Fact]
public void DictionaryWorksWithoutImplicitTuples()
{
var value = new Dictionary<int, string>()
{
[123] = "abc",
[456] = "def"
};
var model = RuntimeTypeModel.Create();
model.AllowImplicitTuples = false;
var clone = model.DeepClone(value);
Assert.Equal(value.Count, clone.Count); //, "Runtime");
Assert.Equal("abc", clone[123]); //, "Runtime");
Assert.Equal("def", clone[456]); //, "Runtime");

model.CompileInPlace();
clone = model.DeepClone(value);
Assert.Equal(value.Count, clone.Count); //, "CompileInPlace");
Assert.Equal("abc", clone[123]); //, "CompileInPlace");
Assert.Equal("def", clone[456]); //, "CompileInPlace");

clone = model.Compile().DeepClone(value);
Assert.Equal(value.Count, clone.Count); //, "Compile");
Assert.Equal("abc", clone[123]); //, "Compile");
Assert.Equal("def", clone[456]); //, "Compile");
}
}
}
1 change: 0 additions & 1 deletion src/protobuf-net.Test/Serializers/KeyValuePairTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ProtoBuf.unittest.Serializers
{

public class KeyValuePairTests
{
[Fact]
Expand Down
5 changes: 3 additions & 2 deletions src/protobuf-net/Meta/MetaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,9 @@ internal static AttributeFamily GetContractFamily(RuntimeTypeModel model, Type t
break;
}
}
if (family == AttributeFamily.None)
{ // check for obvious tuples
if (family == AttributeFamily.None && model.AllowImplicitTuples)
{
// check for obvious tuples
if (ResolveTupleConstructor(type, out _) is object)
{
family |= AttributeFamily.AutoTuple;
Expand Down
15 changes: 15 additions & 0 deletions src/protobuf-net/Meta/RuntimeTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private enum RuntimeTypeModelOptions
UseImplicitZeroDefaults = 1 << 15,
AllowParseableTypes = 1 << 16,
AutoAddProtoContractTypesOnly = 1 << 17,
AllowImplicitTuples = 1 << 18,
}

/// <summary>
Expand Down Expand Up @@ -163,6 +164,19 @@ public bool InternStrings
set { SetOption(RuntimeTypeModelOptions.InternStrings, value); }
}

/// <summary>
/// Global switch that enables or disables the implicit handling of tuple-like types.
/// With this enabled, types that
/// - have a constructor with parameters that are equivalent to all its public members
/// - has only read-only properties, or whose name includes Tuple
/// are serialized even if they are not attributed.
/// </summary>
public bool AllowImplicitTuples
{
get { return GetOption(RuntimeTypeModelOptions.AllowImplicitTuples); }
set { SetOption(RuntimeTypeModelOptions.AllowImplicitTuples, value); }
}

/// <summary>
/// The default model, used to support ProtoBuf.Serializer
/// </summary>
Expand Down Expand Up @@ -556,6 +570,7 @@ internal RuntimeTypeModel(bool isDefault, string name)
{
AutoAddMissingTypes = true;
UseImplicitZeroDefaults = true;
AllowImplicitTuples = true;
SetOption(RuntimeTypeModelOptions.IsDefaultModel, isDefault);
#if !DEBUG
try
Expand Down