Skip to content

[Backport 7.7] Add special serialization handling for nullable ValueTuples #4715

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

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Elasticsearch.Net/Utf8Json/Resolvers/DynamicGenericResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,45 @@ internal static object GetFormatter(Type t)

return CreateInstance(tupleFormatterType, ti.GenericTypeArguments);
}

// Nullable ValueTuple
else if (isNullable && nullableElementType.GetTypeInfo().IsConstructedGenericType() &&
nullableElementType.GetTypeInfo().FullName.StartsWith("System.ValueTuple"))
{
Type tupleFormatterType = null;
switch (nullableElementType.GenericTypeArguments.Length)
{
case 1:
tupleFormatterType = typeof(ValueTupleFormatter<>);
break;
case 2:
tupleFormatterType = typeof(ValueTupleFormatter<,>);
break;
case 3:
tupleFormatterType = typeof(ValueTupleFormatter<,,>);
break;
case 4:
tupleFormatterType = typeof(ValueTupleFormatter<,,,>);
break;
case 5:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,>);
break;
case 6:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,>);
break;
case 7:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,,>);
break;
case 8:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,,,>);
break;
default:
break;
}

var tupleFormatter = CreateInstance(tupleFormatterType, nullableElementType.GenericTypeArguments);
return CreateInstance(typeof(StaticNullableFormatter<>), new [] { nullableElementType }, tupleFormatter);
}
#endif

// ArraySegement
Expand Down
38 changes: 38 additions & 0 deletions tests/Tests.Reproduce/GithubIssue4703.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Text;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using FluentAssertions;
using Nest;
using Tests.Core.Client;

namespace Tests.Reproduce
{
public class GithubIssue4703
{
[U]
public void NullableValueTupleDoesNotThrow()
{
Func<IndexResponse> action = () =>
TestClient.DefaultInMemoryClient.Index(
new ExampleDoc
{
tupleNullable = ("somestring", 42),
}, i => i.Index("index"));

var a = action.Should().NotThrow();
var response = a.Subject;

var json = Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
json.Should().Be(@"{""tupleNullable"":{""Item1"":""somestring"",""Item2"":42}}");
}
}

public class ExampleDoc
{
public (string info, int number)? tupleNullable { get; set; }
}
}