From 7dbcf34be565bed3e84eedcde611362da94e3158 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 12 Jul 2023 16:02:32 +0800 Subject: [PATCH] Enable nullable annotations for Volo.Abp.AspNetCore.Mvc.UI.Widgets --- .../Volo.Abp.AspNetCore.Mvc.UI.Widgets.csproj | 2 ++ .../Mvc/UI/Widgets/AbpViewComponentHelper.cs | 6 ++--- .../Components/WidgetResourcesViewModel.cs | 3 ++- .../Mvc/UI/Widgets/WidgetAttribute.cs | 23 +++++++------------ .../Mvc/UI/Widgets/WidgetDefinition.cs | 17 +++++++------- .../UI/Widgets/WidgetDefinitionCollection.cs | 13 ++++------- .../Mvc/UI/Widgets/WidgetManager.cs | 4 ++-- .../Mvc/UI/Widgets/WidgetResourceItem.cs | 6 ++--- 8 files changed, 32 insertions(+), 42 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo.Abp.AspNetCore.Mvc.UI.Widgets.csproj b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo.Abp.AspNetCore.Mvc.UI.Widgets.csproj index cbb3d106a1..25106fee56 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo.Abp.AspNetCore.Mvc.UI.Widgets.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo.Abp.AspNetCore.Mvc.UI.Widgets.csproj @@ -5,6 +5,8 @@ net7.0 + enable + Nullable true Volo.Abp.AspNetCore.Mvc.UI.Widgets Volo.Abp.AspNetCore.Mvc.UI.Widgets diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs index 764169fd06..c55ea9dd4c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs @@ -28,7 +28,7 @@ public class AbpViewComponentHelper : IViewComponentHelper, IViewContextAware, I Options = widgetOptions.Value; } - public virtual async Task InvokeAsync(string name, object arguments) + public virtual async Task InvokeAsync(string name, object? arguments) { var widget = Options.Widgets.Find(name); if (widget == null) @@ -39,7 +39,7 @@ public class AbpViewComponentHelper : IViewComponentHelper, IViewContextAware, I return await InvokeWidgetAsync(arguments, widget); } - public virtual async Task InvokeAsync(Type componentType, object arguments) + public virtual async Task InvokeAsync(Type componentType, object? arguments) { var widget = Options.Widgets.Find(componentType); if (widget == null) @@ -55,7 +55,7 @@ public class AbpViewComponentHelper : IViewComponentHelper, IViewContextAware, I DefaultViewComponentHelper.Contextualize(viewContext); } - protected virtual async Task InvokeWidgetAsync(object arguments, WidgetDefinition widget) + protected virtual async Task InvokeWidgetAsync(object? arguments, WidgetDefinition widget) { PageWidgetManager.TryAdd(widget); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/Components/WidgetResourcesViewModel.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/Components/WidgetResourcesViewModel.cs index 8430ba6b91..11dff8def7 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/Components/WidgetResourcesViewModel.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/Components/WidgetResourcesViewModel.cs @@ -4,5 +4,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets.Components; public class WidgetResourcesViewModel { - public IReadOnlyList Widgets { get; set; } + public IReadOnlyList Widgets { get; set; } = default!; + } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetAttribute.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetAttribute.cs index 2cc1b9232d..31fc18a8a0 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetAttribute.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetAttribute.cs @@ -8,30 +8,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets; [AttributeUsage(AttributeTargets.Class)] public class WidgetAttribute : Attribute { - [CanBeNull] - public string[] StyleFiles { get; set; } + public string[]? StyleFiles { get; set; } - [CanBeNull] - public Type[] StyleTypes { get; set; } + public Type[]? StyleTypes { get; set; } - [CanBeNull] - public string[] ScriptFiles { get; set; } + public string[]? ScriptFiles { get; set; } - [CanBeNull] - public Type[] ScriptTypes { get; set; } + public Type[]? ScriptTypes { get; set; } - [CanBeNull] - public string DisplayName { get; set; } + public string? DisplayName { get; set; } - [CanBeNull] - public Type DisplayNameResource { get; set; } + public Type? DisplayNameResource { get; set; } - [CanBeNull] - public string[] RequiredPolicies { get; set; } + public string[]? RequiredPolicies { get; set; } public bool RequiresAuthentication { get; set; } - public string RefreshUrl { get; set; } + public string? RefreshUrl { get; set; } public bool AutoInitialize { get; set; } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs index 2a9b213494..0e5d8b8edf 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs @@ -26,7 +26,7 @@ public class WidgetDefinition get => _displayName; set => _displayName = Check.NotNull(value, nameof(value)); } - private ILocalizableString _displayName; + private ILocalizableString _displayName = default!; [NotNull] public Type ViewComponentType { get; } @@ -46,14 +46,13 @@ public class WidgetDefinition [NotNull] public List Scripts { get; } - [CanBeNull] - public string RefreshUrl { get; set; } + public string? RefreshUrl { get; set; } public bool AutoInitialize { get; set; } public WidgetDefinition( [NotNull] Type viewComponentType, - [CanBeNull] ILocalizableString displayName = null) + ILocalizableString? displayName = null) { ViewComponentType = Check.NotNull(viewComponentType, nameof(viewComponentType)); @@ -74,12 +73,12 @@ public class WidgetDefinition if (!widgetAttribute.StyleTypes.IsNullOrEmpty()) { - styles.AddRange(widgetAttribute.StyleTypes.Select(type => new WidgetResourceItem(type))); + styles.AddRange(widgetAttribute.StyleTypes!.Select(type => new WidgetResourceItem(type))); } if (!widgetAttribute.StyleFiles.IsNullOrEmpty()) { - styles.AddRange(widgetAttribute.StyleFiles.Select(src => new WidgetResourceItem(src))); + styles.AddRange(widgetAttribute.StyleFiles!.Select(src => new WidgetResourceItem(src))); } return styles; @@ -91,12 +90,12 @@ public class WidgetDefinition if (!widgetAttribute.ScriptTypes.IsNullOrEmpty()) { - scripts.AddRange(widgetAttribute.ScriptTypes.Select(type => new WidgetResourceItem(type))); + scripts.AddRange(widgetAttribute.ScriptTypes!.Select(type => new WidgetResourceItem(type))); } if (!widgetAttribute.ScriptFiles.IsNullOrEmpty()) { - scripts.AddRange(widgetAttribute.ScriptFiles.Select(src => new WidgetResourceItem(src))); + scripts.AddRange(widgetAttribute.ScriptFiles!.Select(src => new WidgetResourceItem(src))); } return scripts; @@ -108,7 +107,7 @@ public class WidgetDefinition if (!widgetAttribute.RequiredPolicies.IsNullOrEmpty()) { - policies.AddRange(widgetAttribute.RequiredPolicies); + policies.AddRange(widgetAttribute.RequiredPolicies!); } return policies; diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinitionCollection.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinitionCollection.cs index db4266b872..b0e56f3826 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinitionCollection.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinitionCollection.cs @@ -30,34 +30,31 @@ public class WidgetDefinitionCollection } public WidgetDefinition Add( - [CanBeNull] ILocalizableString displayName = null) + ILocalizableString? displayName = null) { return Add(typeof(TViewComponent), displayName); } public WidgetDefinition Add( [NotNull] Type viewComponentType, - [CanBeNull] ILocalizableString displayName = null) + ILocalizableString? displayName = null) { var widget = new WidgetDefinition(viewComponentType, displayName); Add(widget); return widget; } - [CanBeNull] - public WidgetDefinition Find(string name) + public WidgetDefinition? Find(string name) { return _widgetsByName.GetOrDefault(name); } - [CanBeNull] - public WidgetDefinition Find() + public WidgetDefinition? Find() { return Find(typeof(TViewComponent)); } - [CanBeNull] - public WidgetDefinition Find(Type viewComponentType) + public WidgetDefinition? Find(Type viewComponentType) { return _widgetsByType.GetOrDefault(viewComponentType); } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetManager.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetManager.cs index f86a457058..422f628a9e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetManager.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetManager.cs @@ -27,7 +27,7 @@ public class WidgetManager : IWidgetManager { var widget = Options.Widgets.Find(widgetComponentType); - return await IsGrantedAsyncInternal(widget, widgetComponentType.FullName); + return await IsGrantedAsyncInternal(widget, widgetComponentType.FullName!); } public async Task IsGrantedAsync(string name) @@ -37,7 +37,7 @@ public class WidgetManager : IWidgetManager return await IsGrantedAsyncInternal(widget, name); } - private async Task IsGrantedAsyncInternal(WidgetDefinition widget, string wantedWidgetName) + private async Task IsGrantedAsyncInternal(WidgetDefinition? widget, string wantedWidgetName) { if (widget == null) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetResourceItem.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetResourceItem.cs index 29cff2faef..1b2f800649 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetResourceItem.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetResourceItem.cs @@ -5,11 +5,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets; public class WidgetResourceItem { - [CanBeNull] - public string Src { get; } + public string? Src { get; } - [CanBeNull] - public Type Type { get; } + public Type? Type { get; } public WidgetResourceItem([NotNull] string src) {