Skip to content

Commit

Permalink
Merge pull request #1244 from riganti/fix/serializability-analyzers
Browse files Browse the repository at this point in the history
Fixed and improved serializability analyzers
  • Loading branch information
acizmarik authored Dec 22, 2021
2 parents 858f52d + 0d17844 commit 7876247
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class DefaultViewModel : DotvvmViewModelBase
}",

VerifyCS.Diagnostic(ViewModelSerializabilityAnalyzer.UseSerializablePropertiesRule)
.WithLocation(0).WithArguments("System.IO.FileInfo"));
.WithLocation(0).WithArguments("this.NonSerializableProperty"));
}

[Fact]
Expand All @@ -72,7 +72,7 @@ public class DefaultViewModel : DotvvmViewModelBase
}",

VerifyCS.Diagnostic(ViewModelSerializabilityAnalyzer.UseSerializablePropertiesRule)
.WithLocation(0).WithArguments("System.Collections.Generic.List<System.IO.Stream>"));
.WithLocation(0).WithArguments("this.NonSerializableList"));
}

[Fact]
Expand All @@ -88,13 +88,12 @@ namespace ConsoleApplication1
{
public class DefaultViewModel : DotvvmViewModelBase
{
public int SerializableProperty { get; set; }
{|#0:public IList<Stream> NonSerializableList { get; set; }|}
{|#0:public IList<IDisposable> PotentiallyNonSerializableList { get; set; }|}
}
}",

VerifyCS.Diagnostic(ViewModelSerializabilityAnalyzer.DoNotUseUninstantiablePropertiesRule)
.WithLocation(0).WithArguments("System.Collections.Generic.IList<System.IO.Stream>"));
VerifyCS.Diagnostic(ViewModelSerializabilityAnalyzer.UseSerializablePropertiesRule)
.WithLocation(0).WithArguments("this.PotentiallyNonSerializableList"));
}

[Fact]
Expand Down Expand Up @@ -220,6 +219,7 @@ namespace ConsoleApplication1
public class DefaultViewModel : DotvvmViewModelBase
{
public DateTime? DateTime { get; set; }
public DateTimeOffset? DateTimeOffset { get; set; }
public TimeSpan? TimeSpan { get; set; }
public Guid? Guid { get; set; }
}
Expand Down Expand Up @@ -264,6 +264,7 @@ public class DefaultViewModel : DotvvmViewModelBase
public object Object { get; set; }
public string String { get; set; }
public DateTime DateTime { get; set; }
public DateTimeOffset DateTimeOffset { get; set; }
public TimeSpan TimeSpan { get; set; }
public Guid Guid { get; set; }
}
Expand Down Expand Up @@ -293,6 +294,28 @@ public class DefaultViewModel : DotvvmViewModelBase
await VerifyCS.VerifyAnalyzerAsync(test);
}

[Fact]
public async void Test_NoWarningsForEnumerables_ViewModel()
{
var test = @"
using DotVVM.Framework.ViewModel;
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class DefaultViewModel : DotvvmViewModelBase
{
public IEnumerable<int> Enumerable { get; set; }
public IList<int> List { get; set; }
public ICollection<int> Collection { get; set; }
public ICollection<IList<IEnumerable<int>>> WowCollection { get; set; }
}
}";

await VerifyCS.VerifyAnalyzerAsync(test);
}

[Fact]
public async void Test_UserTypesAreSerializableAndSupported_ViewModel()
{
Expand All @@ -311,6 +334,11 @@ public class DefaultViewModel : DotvvmViewModelBase
public class UserType
{
public string Property { get; set; }
public int? NullableProp { get; set; }
public DateTime? NullableDateTime { get; set; }
public DateTimeOffset? NullableDateTimeOffset { get; set; }
public bool FlagProp { get; set; }
public IList<int> List { get; set; }
}
}";

Expand All @@ -330,12 +358,12 @@ namespace ConsoleApplication1
public class DefaultViewModel : DotvvmViewModelBase
{
public int SerializableProperty { get; set; }
{|#0:public LinkedList<object> LinkedList { get; set; }|}
{|#0:public Action Action { get; set; }|}
}
}",

VerifyCS.Diagnostic(ViewModelSerializabilityAnalyzer.UseSerializablePropertiesRule)
.WithLocation(0).WithArguments("System.Collections.Generic.LinkedList<object>"));
.WithLocation(0).WithArguments("this.Action"));
}

[Fact]
Expand Down Expand Up @@ -455,5 +483,56 @@ public class DefaultViewModel : DotvvmViewModelBase

await VerifyCS.VerifyAnalyzerAsync(text);
}


[Fact]
public async void Test_WhiteListedDotvvmTypes_Properties_ViewModel()
{
var text = @"
using DotVVM.Framework.Controls;
using DotVVM.Framework.ViewModel;
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class DefaultViewModel : DotvvmViewModelBase
{
public GridViewDataSet<int> DataSet { get; set; }
public UploadedFilesCollection UploadedFiles { get; set; }
}
}";

await VerifyCS.VerifyAnalyzerAsync(text);
}

[Fact]
public async void Test_GenericReferenceType_Properties_ViewModel()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using DotVVM.Framework.Controls;
using DotVVM.Framework.ViewModel;
using System;
using System.IO;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class DefaultViewModel : DotvvmViewModelBase
{
public WrappedValue<int> Value { get; set; }
{|#0:public WrappedValue<Stream> NonSerializable { get; set; }|}
}
public class WrappedValue<T>
{
public string Name { get; set; }
public T Value { get; set; }
}
}",

VerifyCS.Diagnostic(ViewModelSerializabilityAnalyzer.UseSerializablePropertiesRule).WithLocation(0)
.WithArguments("this.NonSerializable.Value"));
}
}
}
1 change: 0 additions & 1 deletion src/Analyzers/Analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ Rule ID | Category | Severity | Notes
--------|----------|----------|-------
DotVVM02 | Serializability | Warning | ViewModelSerializabilityAnalyzer
DotVVM03 | Serializability | Warning | ViewModelSerializabilityAnalyzer
DotVVM04 | Serializability | Warning | ViewModelSerializabilityAnalyzer
1 change: 0 additions & 1 deletion src/Analyzers/Analyzers/DotvvmDiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ public static class DotvvmDiagnosticIds

public const string UseSerializablePropertiesInViewModelRuleId = "DotVVM02";
public const string DoNotUseFieldsInViewModelRuleId = "DotVVM03";
public const string DoNotUseUninstantiablePropertiesInViewModelRuleId = "DotVVM04";
}
}
31 changes: 2 additions & 29 deletions src/Analyzers/Analyzers/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions src/Analyzers/Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,9 @@
<value>Unsupported property type in viewmodel.</value>
</data>
<data name="Serializability_NonSerializableType_Message" xml:space="preserve">
<value>Property of type '{0}' is not serializable</value>
<value>Property on path '{0}' is not serializable</value>
</data>
<data name="Serializability_NonSerializableType_Title" xml:space="preserve">
<value>Properties must be serializable</value>
</data>
<data name="Serializability_UninstantiableType_Description" xml:space="preserve">
<value>Use concrete implementation in viewmodels to ensure that all properties are correctly serialized.</value>
</data>
<data name="Serializability_UninstantiableType_Message" xml:space="preserve">
<value>Property of abstract type '{0}' might not be correctly serialized</value>
</data>
<data name="Serializability_UninstantiableType_Title" xml:space="preserve">
<value>Interfaces and abstract classes might not be correctly serialized</value>
</data>
</root>
Loading

0 comments on commit 7876247

Please sign in to comment.