From 4c1d774338c6cd86aa7f9ca09cd1946fdb16642c Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 16 Mar 2022 10:47:02 +0100 Subject: [PATCH] fix GetMigrationMethods in case the class contains methods with return type of T[] --- .../AddMigrationMethodCodeFixProviderSpec.cs | 203 ++++++++++++------ .../MigrationHashHelper.cs | 6 +- 2 files changed, 141 insertions(+), 68 deletions(-) diff --git a/Weingartner.Json.Migration.Roslyn.Spec/AddMigrationMethodCodeFixProviderSpec.cs b/Weingartner.Json.Migration.Roslyn.Spec/AddMigrationMethodCodeFixProviderSpec.cs index b670c8f..7ea46c5 100644 --- a/Weingartner.Json.Migration.Roslyn.Spec/AddMigrationMethodCodeFixProviderSpec.cs +++ b/Weingartner.Json.Migration.Roslyn.Spec/AddMigrationMethodCodeFixProviderSpec.cs @@ -26,31 +26,37 @@ public void ShouldAddMigrationMethod() var smallFailingDoc = @"using Weingartner.Json.Migration; using System.Runtime.Serialization; -[Migratable(""1234"")] -[DataContract] -class TypeName +namespace NameSpaceName { - [DataMember] - public int A { get; set; } - [DataMember] - public int B { get; set; } + [Migratable(""1234"")] + [DataContract] + class TypeName + { + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + } }"; var expected = @"using Weingartner.Json.Migration; using System.Runtime.Serialization; -[Migratable(""327430167"")] -[DataContract] -class TypeName +namespace NameSpaceName { - [DataMember] - public int A { get; set; } - [DataMember] - public int B { get; set; } - - private static JToken Migrate_1(JToken data, JsonSerializer serializer) + [Migratable(""327430167"")] + [DataContract] + class TypeName { - throw new System.NotImplementedException(); - return data; + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + + private static JToken Migrate_1(JToken data, JsonSerializer serializer) + { + throw new System.NotImplementedException(); + return data; + } } }"; @@ -65,21 +71,24 @@ public void ShouldAddSecondMigrationMethod() using Newtonsoft.Json; using Newtonsoft.Json.Linq; -[Migratable(""327430167"")] -[DataContract] -class TypeName +namespace NameSpaceName { - [DataMember] - public int A { get; set; } - [DataMember] - public int B { get; set; } - [DataMember] - public int C { get; set; } - - private static JToken Migrate_1(JToken data, JsonSerializer serializer) + [Migratable(""327430167"")] + [DataContract] + class TypeName { - data[""B""] = 0; - return data; + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + [DataMember] + public int C { get; set; } + + private static JToken Migrate_1(JToken data, JsonSerializer serializer) + { + data[""B""] = 0; + return data; + } } }"; var expected = @"using Weingartner.Json.Migration; @@ -87,27 +96,30 @@ private static JToken Migrate_1(JToken data, JsonSerializer serializer) using Newtonsoft.Json; using Newtonsoft.Json.Linq; -[Migratable(""-1225206030"")] -[DataContract] -class TypeName +namespace NameSpaceName { - [DataMember] - public int A { get; set; } - [DataMember] - public int B { get; set; } - [DataMember] - public int C { get; set; } - - private static JToken Migrate_1(JToken data, JsonSerializer serializer) + [Migratable(""-1225206030"")] + [DataContract] + class TypeName { - data[""B""] = 0; - return data; - } + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + [DataMember] + public int C { get; set; } + + private static JToken Migrate_1(JToken data, JsonSerializer serializer) + { + data[""B""] = 0; + return data; + } - private static JToken Migrate_2(JToken data, JsonSerializer serializer) - { - throw new System.NotImplementedException(); - return data; + private static JToken Migrate_2(JToken data, JsonSerializer serializer) + { + throw new System.NotImplementedException(); + return data; + } } }"; @@ -120,31 +132,90 @@ public void ShouldAddMigrationMethodToStruct() var smallFailingDoc = @"using Weingartner.Json.Migration; using System.Runtime.Serialization; -[Migratable(""1234"")] -[DataContract] -struct TypeName +namespace NameSpaceName { - [DataMember] - public int A { get; set; } - [DataMember] - public int B { get; set; } + [Migratable(""1234"")] + [DataContract] + struct TypeName + { + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + } }"; var expected = @"using Weingartner.Json.Migration; using System.Runtime.Serialization; -[Migratable(""327430167"")] -[DataContract] -struct TypeName +namespace NameSpaceName { - [DataMember] - public int A { get; set; } - [DataMember] - public int B { get; set; } + [Migratable(""327430167"")] + [DataContract] + struct TypeName + { + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + + private static JToken Migrate_1(JToken data, JsonSerializer serializer) + { + throw new System.NotImplementedException(); + return data; + } + } +}"; - private static JToken Migrate_1(JToken data, JsonSerializer serializer) + VerifyCSharpFix(smallFailingDoc, expected, null, true); + } + + [Fact] + public void ShouldNotFailIfAMethodHasArrayReturnType() + { + var smallFailingDoc = @"using Weingartner.Json.Migration; +using System.Runtime.Serialization; + +namespace NameSpaceName +{ + [Migratable(""1234"")] + [DataContract] + class TypeName { - throw new System.NotImplementedException(); - return data; + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + + public int[] SomeMethod() + { + return new[] { 1, 2, 3 }; + } + } +}"; + var expected = @"using Weingartner.Json.Migration; +using System.Runtime.Serialization; + +namespace NameSpaceName +{ + [Migratable(""327430167"")] + [DataContract] + class TypeName + { + [DataMember] + public int A { get; set; } + [DataMember] + public int B { get; set; } + + public int[] SomeMethod() + { + return new[] { 1, 2, 3 }; + } + + private static JToken Migrate_1(JToken data, JsonSerializer serializer) + { + throw new System.NotImplementedException(); + return data; + } } }"; diff --git a/Weingartner.Json.Migration.Roslyn/MigrationHashHelper.cs b/Weingartner.Json.Migration.Roslyn/MigrationHashHelper.cs index 6d731ec..1923d7d 100644 --- a/Weingartner.Json.Migration.Roslyn/MigrationHashHelper.cs +++ b/Weingartner.Json.Migration.Roslyn/MigrationHashHelper.cs @@ -112,8 +112,10 @@ public static IReadOnlyList GetMigrationMethods(ITypeSymbol typ return new MethodParameter(parameterType); }) .ToList(); - var returnType = new SimpleType(m.ReturnType.ToString(), - new AssemblyName(m.ReturnType.ContainingAssembly.ToString())); + var returnTypeAssemblyName = m.ReturnType.Kind == SymbolKind.ArrayType + ? ((IArrayTypeSymbol)m.ReturnType).ElementType.ContainingAssembly.ToString() + : m.ReturnType.ContainingAssembly.ToString(); + var returnType = new SimpleType(m.ReturnType.ToString(), new AssemblyName(returnTypeAssemblyName)); return MigrationMethod.TryParse(declaringType, parameters, returnType, m.Name); }) .Where(m => m != null)