From 8c43151857108dd3cacbacf70d8a4bbf9356b303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Wed, 18 Sep 2024 16:41:55 +0200 Subject: [PATCH] Fix DotVVM02 warning on overriden ignored property We now ignore override properties if the base has JsonIgnore or Bind(None) attribute. Technically, the patch introduces a false negative if the base is ignored and the derived isn't. However, DotVVM behavior regarding this is complex (you cannot override JsonIgnore, only Bind(None)...) and given the rarity of virtual properties, I don't want to unnecesarily complicate the analyzer. fix #1784 --- .../ViewModelSerializabilityTest.cs | 30 +++++++++++++++++++ .../ViewModelSerializabilityAnalyzer.cs | 4 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Analyzers/Analyzers.Tests/Serializability/ViewModelSerializabilityTest.cs b/src/Analyzers/Analyzers.Tests/Serializability/ViewModelSerializabilityTest.cs index 5f1e6a4543..c8aeb43d26 100644 --- a/src/Analyzers/Analyzers.Tests/Serializability/ViewModelSerializabilityTest.cs +++ b/src/Analyzers/Analyzers.Tests/Serializability/ViewModelSerializabilityTest.cs @@ -547,6 +547,36 @@ public class DefaultViewModel : DotvvmViewModelBase await VerifyCS.VerifyAnalyzerAsync(text); } + [Fact] + public async Task Test_IgnoreNonSerializedMembers_BindDirectionNoneInherited_ViewModel() + { + var text = @" + using DotVVM.Framework.ViewModel; + using Newtonsoft.Json; + using System; + using System.IO; + + namespace ConsoleApplication1 + { + public class BaseVM : DotvvmViewModelBase + { + [Bind(Direction.None)] + public virtual Stream Property1 { get; } + + [JsonIgnore] + public virtual Stream Property2 { get; } + } + + public class DefaultViewModel : BaseVM + { + public override Stream Property1 { get; } + public override Stream Property2 { get; } + } + }"; + + await VerifyCS.VerifyAnalyzerAsync(text, expected: []); + } + [Fact] public async Task Test_SelfReferencingTypes_GenericArgs_ViewModel() { diff --git a/src/Analyzers/Analyzers/Serializability/ViewModelSerializabilityAnalyzer.cs b/src/Analyzers/Analyzers/Serializability/ViewModelSerializabilityAnalyzer.cs index 95328f6abc..84a55f9d75 100644 --- a/src/Analyzers/Analyzers/Serializability/ViewModelSerializabilityAnalyzer.cs +++ b/src/Analyzers/Analyzers/Serializability/ViewModelSerializabilityAnalyzer.cs @@ -317,9 +317,11 @@ private static bool IsSerializationOverriden(IPropertySymbol property, Serializa return false; } + /// Returns true if the property has a Bind(Direction.None) or JsonIgnore attribute private static bool IsSerializationIgnored(IPropertySymbol property, SerializabilityAnalysisContext context) { - return IsSerializationIgnoredUsingBindAttribute(property, context) || IsSerializationIgnoredUsingJsonIgnoreAttribute(property, context); + return IsSerializationIgnoredUsingBindAttribute(property, context) || IsSerializationIgnoredUsingJsonIgnoreAttribute(property, context) || + property.OverriddenProperty is {} overriddenProperty && IsSerializationIgnored(overriddenProperty, context); } private static bool IsSerializationIgnoredUsingBindAttribute(ISymbol propertyOrFieldSymbol, SerializabilityAnalysisContext context)