Skip to content

Commit

Permalink
fix GetMigrationMethods
Browse files Browse the repository at this point in the history
in case the class contains methods with return type of T[]
  • Loading branch information
FlorianAuinger committed Mar 16, 2022
1 parent 22e3baf commit 4c1d774
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}";

Expand All @@ -65,49 +71,55 @@ 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;
using System.Runtime.Serialization;
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;
}
}
}";

Expand All @@ -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;
}
}
}";

Expand Down
6 changes: 4 additions & 2 deletions Weingartner.Json.Migration.Roslyn/MigrationHashHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ public static IReadOnlyList<MigrationMethod> 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)
Expand Down

0 comments on commit 4c1d774

Please sign in to comment.