Skip to content

Commit

Permalink
Merge pull request #1556 from Scormave/fixNullableKnownTypes
Browse files Browse the repository at this point in the history
Fixed nullable embedded types are being added to generated resolver
  • Loading branch information
AArnott committed Mar 11, 2023
2 parents a59c65e + 99ae5e1 commit 81199b1
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 31 deletions.
128 changes: 98 additions & 30 deletions sandbox/TestData2/Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,24 @@ internal static class GeneratedResolverGetFormatterHelper

static GeneratedResolverGetFormatterHelper()
{
lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, int>(14)
{
{ typeof(global::System.Collections.Generic.List<global::TestData2.A>), 0 },
{ typeof(global::System.Collections.Generic.List<global::TestData2.B>), 1 },
{ typeof(global::TestData2.Nest1.Id), 2 },
{ typeof(global::TestData2.Nest2.Id), 3 },
{ typeof(global::TestData2.A), 4 },
{ typeof(global::TestData2.B), 5 },
{ typeof(global::TestData2.C), 6 },
{ typeof(global::TestData2.Nest1), 7 },
{ typeof(global::TestData2.Nest1.IdType), 8 },
{ typeof(global::TestData2.Nest2), 9 },
{ typeof(global::TestData2.Nest2.IdType), 10 },
{ typeof(global::TestData2.PropNameCheck1), 11 },
{ typeof(global::TestData2.PropNameCheck2), 12 },
{ typeof(global::TestData2.Record), 13 },
lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, int>(16)
{
{ typeof(global::System.Collections.Generic.List<byte[]>), 0 },
{ typeof(global::System.Collections.Generic.List<global::TestData2.A>), 1 },
{ typeof(global::System.Collections.Generic.List<global::TestData2.B>), 2 },
{ typeof(global::TestData2.Nest1.Id), 3 },
{ typeof(global::TestData2.Nest2.Id), 4 },
{ typeof(global::TestData2.A), 5 },
{ typeof(global::TestData2.B), 6 },
{ typeof(global::TestData2.C), 7 },
{ typeof(global::TestData2.Nest1), 8 },
{ typeof(global::TestData2.Nest1.IdType), 9 },
{ typeof(global::TestData2.Nest2), 10 },
{ typeof(global::TestData2.Nest2.IdType), 11 },
{ typeof(global::TestData2.NullableTest), 12 },
{ typeof(global::TestData2.PropNameCheck1), 13 },
{ typeof(global::TestData2.PropNameCheck2), 14 },
{ typeof(global::TestData2.Record), 15 },
};
}

Expand All @@ -76,20 +78,22 @@ internal static object GetFormatter(global::System.Type t)

switch (key)
{
case 0: return new global::MessagePack.Formatters.ListFormatter<global::TestData2.A>();
case 1: return new global::MessagePack.Formatters.ListFormatter<global::TestData2.B>();
case 2: return new MessagePack.Formatters.TestData2.Nest1_IdFormatter();
case 3: return new MessagePack.Formatters.TestData2.Nest2_IdFormatter();
case 4: return new MessagePack.Formatters.TestData2.AFormatter();
case 5: return new MessagePack.Formatters.TestData2.BFormatter();
case 6: return new MessagePack.Formatters.TestData2.CFormatter();
case 7: return new MessagePack.Formatters.TestData2.Nest1Formatter();
case 8: return new MessagePack.Formatters.TestData2.Nest1_IdTypeFormatter();
case 9: return new MessagePack.Formatters.TestData2.Nest2Formatter();
case 10: return new MessagePack.Formatters.TestData2.Nest2_IdTypeFormatter();
case 11: return new MessagePack.Formatters.TestData2.PropNameCheck1Formatter();
case 12: return new MessagePack.Formatters.TestData2.PropNameCheck2Formatter();
case 13: return new MessagePack.Formatters.TestData2.RecordFormatter();
case 0: return new global::MessagePack.Formatters.ListFormatter<byte[]>();
case 1: return new global::MessagePack.Formatters.ListFormatter<global::TestData2.A>();
case 2: return new global::MessagePack.Formatters.ListFormatter<global::TestData2.B>();
case 3: return new MessagePack.Formatters.TestData2.Nest1_IdFormatter();
case 4: return new MessagePack.Formatters.TestData2.Nest2_IdFormatter();
case 5: return new MessagePack.Formatters.TestData2.AFormatter();
case 6: return new MessagePack.Formatters.TestData2.BFormatter();
case 7: return new MessagePack.Formatters.TestData2.CFormatter();
case 8: return new MessagePack.Formatters.TestData2.Nest1Formatter();
case 9: return new MessagePack.Formatters.TestData2.Nest1_IdTypeFormatter();
case 10: return new MessagePack.Formatters.TestData2.Nest2Formatter();
case 11: return new MessagePack.Formatters.TestData2.Nest2_IdTypeFormatter();
case 12: return new MessagePack.Formatters.TestData2.NullableTestFormatter();
case 13: return new MessagePack.Formatters.TestData2.PropNameCheck1Formatter();
case 14: return new MessagePack.Formatters.TestData2.PropNameCheck2Formatter();
case 15: return new MessagePack.Formatters.TestData2.RecordFormatter();
default: return null;
}
}
Expand Down Expand Up @@ -564,6 +568,70 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
}
}

public sealed class NullableTestFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::TestData2.NullableTest>
{
// a
private static global::System.ReadOnlySpan<byte> GetSpan_a() => new byte[1 + 1] { 161, 97 };
// b
private static global::System.ReadOnlySpan<byte> GetSpan_b() => new byte[1 + 1] { 161, 98 };

public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::TestData2.NullableTest value, global::MessagePack.MessagePackSerializerOptions options)
{
if (value is null)
{
writer.WriteNil();
return;
}

var formatterResolver = options.Resolver;
writer.WriteMapHeader(2);
writer.WriteRaw(GetSpan_a());
global::MessagePack.FormatterResolverExtensions.GetFormatterWithVerify<int[]>(formatterResolver).Serialize(ref writer, value.a, options);
writer.WriteRaw(GetSpan_b());
global::MessagePack.FormatterResolverExtensions.GetFormatterWithVerify<global::System.Collections.Generic.List<byte[]>>(formatterResolver).Serialize(ref writer, value.b, options);
}

public global::TestData2.NullableTest Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
{
return null;
}

options.Security.DepthStep(ref reader);
var formatterResolver = options.Resolver;
var length = reader.ReadMapHeader();
var ____result = new global::TestData2.NullableTest();

for (int i = 0; i < length; i++)
{
var stringKey = global::MessagePack.Internal.CodeGenHelpers.ReadStringSpan(ref reader);
switch (stringKey.Length)
{
default:
FAIL:
reader.Skip();
continue;
case 1:
switch (global::MessagePack.Internal.AutomataKeyGen.GetKey(ref stringKey))
{
default: goto FAIL;
case 97UL:
____result.a = global::MessagePack.FormatterResolverExtensions.GetFormatterWithVerify<int[]>(formatterResolver).Deserialize(ref reader, options);
continue;
case 98UL:
____result.b = global::MessagePack.FormatterResolverExtensions.GetFormatterWithVerify<global::System.Collections.Generic.List<byte[]>>(formatterResolver).Deserialize(ref reader, options);
continue;
}

}
}

reader.Depth--;
return ____result;
}
}

public sealed class PropNameCheck1Formatter : global::MessagePack.Formatters.IMessagePackFormatter<global::TestData2.PropNameCheck1>
{
// MyProperty1
Expand Down
17 changes: 17 additions & 0 deletions sandbox/TestData2/NullableTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma warning disable SA1307 // Accessible fields should begin with upper-case letter
#pragma warning disable SA1401 // Fields should be private
#pragma warning disable SA1649 // File name should match first type name

#nullable enable

namespace TestData2;

[MessagePackObject(true)]
public class NullableTest
{
public int[]? a;
public List<byte[]?> b = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void CollectCore(ITypeSymbol typeSymbol)
return;
}

var typeSymbolString = typeSymbol.ToString() ?? throw new InvalidOperationException();
var typeSymbolString = typeSymbol.WithNullableAnnotation(NullableAnnotation.NotAnnotated).ToString() ?? throw new InvalidOperationException();
if (this.embeddedTypes.Contains(typeSymbolString))
{
return;
Expand Down

0 comments on commit 81199b1

Please sign in to comment.