Skip to content

Commit

Permalink
Merge pull request #651 from Cysharp/hotfix/FixGeneratorValueTuple
Browse files Browse the repository at this point in the history
Fix invalid code generation for ValueTuple
  • Loading branch information
mayuki authored Jun 1, 2023
2 parents 2516c1d + b33b375 commit 7fa9d86
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/MagicOnion.GeneratorCore/Utils/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public static string GetFullDeclaringTypeName(this Type type)

public static string GetFullDeclaringTypeName(this ITypeSymbol typeSymbol)
{
if (typeSymbol is INamedTypeSymbol { IsGenericType: true, IsValueType: true, ContainingNamespace.Name: "System", Name: "ValueTuple" })
{
// NOTE: Roslyn 4.3.0 does not support `ExpandValueTuple` flag.
// https://github.com/dotnet/roslyn/pull/66929
return "ValueTuple";
}

return typeSymbol.ToDisplayString(
new SymbolDisplayFormat(
SymbolDisplayGlobalNamespaceStyle.OmittedAsContaining,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,37 @@ public class MyClass
typeInfo.FullName.Should().Be("global::System.Collections.Generic.List<global::System.Tuple<global::System.Int32, global::System.String>>");
}

[Fact]
public void FromSymbol_NamedValueTuple()
{
// Arrange
var (compilation, semModel) = CompilationHelper.Create(@"
namespace MyNamespace
{
using System;
using System.Collections.Generic;
public class MyClass
{
public (string Name, int Age) FieldA;
}
}
");
var symbols = compilation.GetSymbolsWithName(x => x == "FieldA", SymbolFilter.Member)
.OfType<IFieldSymbol>()
.ToArray();

// Act
var typeInfo = MagicOnionTypeInfo.CreateFromSymbol(symbols[0].Type);

// Assert
typeInfo.Namespace.Should().Be("System");
typeInfo.Name.Should().Be("ValueTuple");
typeInfo.GenericArguments.Should().HaveCount(2);
typeInfo.GenericArguments[0].Should().Be(MagicOnionTypeInfo.Create("System", "String"));
typeInfo.GenericArguments[1].Should().Be(MagicOnionTypeInfo.CreateValueType("System", "Int32"));
typeInfo.FullName.Should().Be("global::System.ValueTuple<global::System.String, global::System.Int32>");
}

[Fact]
public void ToDisplay_Short()
{
Expand Down

0 comments on commit 7fa9d86

Please sign in to comment.