From 995490ae721940d763b28694f9d1e44aecb95ade Mon Sep 17 00:00:00 2001 From: Stefan Ossendorf Date: Wed, 23 Oct 2024 21:55:46 +0200 Subject: [PATCH] Nullable errors fixed in already migrated projects Fixes #1233 --- .../ApplicationContextManagerInMemory.cs | 10 ++- Source/Csla.Channels.Grpc/GrpcPortal.cs | 9 +-- .../Csla.Channels.RabbitMq/RabbitMqPortal.cs | 11 ++- .../Csla.Channels.RabbitMq/RabbitMqProxy.cs | 68 ++++++++---------- .../ApplicationContextManager.cs | 2 +- .../Server/Hosts/HttpPortal.cs | 35 ++++++---- .../Csla/DataPortalClient/DataPortalProxy.cs | 70 +++++++++---------- .../DataPortalClient/PrimitiveCriteria.cs | 9 ++- .../DataPortalChannel/CriteriaRequest.cs | 12 ++-- .../Hosts/DataPortalChannel/UpdateRequest.cs | 13 ++-- 10 files changed, 118 insertions(+), 121 deletions(-) diff --git a/Source/Csla.AspNetCore/Blazor/ApplicationContextManagerInMemory.cs b/Source/Csla.AspNetCore/Blazor/ApplicationContextManagerInMemory.cs index 52b4dd352e..14a264d432 100644 --- a/Source/Csla.AspNetCore/Blazor/ApplicationContextManagerInMemory.cs +++ b/Source/Csla.AspNetCore/Blazor/ApplicationContextManagerInMemory.cs @@ -134,7 +134,7 @@ public IPrincipal GetUser() /// IHostEnvironmentAuthenticationStateProvider service. /// /// Principal object. - public virtual void SetUser(IPrincipal principal) + public virtual void SetUser(IPrincipal? principal) { if (!ReferenceEquals(CurrentPrincipal, principal)) { @@ -151,13 +151,17 @@ public virtual void SetUser(IPrincipal principal) } else if (HttpContext is not null) { - HttpContext.User = (ClaimsPrincipal)principal; + if (principal is not ClaimsPrincipal claimsPrincipal) + { + throw new ArgumentException("typeof(principal) != ClaimsPrincipal"); + } + HttpContext.User = claimsPrincipal; } else { throw new InvalidOperationException("HttpContext==null, !CircuitExists"); } - CurrentPrincipal = principal; + CurrentPrincipal = principal ?? UnauthenticatedPrincipal; } } diff --git a/Source/Csla.Channels.Grpc/GrpcPortal.cs b/Source/Csla.Channels.Grpc/GrpcPortal.cs index 62a2b9e79a..c9191b489d 100644 --- a/Source/Csla.Channels.Grpc/GrpcPortal.cs +++ b/Source/Csla.Channels.Grpc/GrpcPortal.cs @@ -311,7 +311,7 @@ public async Task Delete(CriteriaRequest request) request = ConvertRequest(request); // unpack criteria data into object - object? criteria = GetCriteria(_applicationContext, request.CriteriaData); + object criteria = GetCriteria(_applicationContext, request.CriteriaData); if (criteria is DataPortalClient.PrimitiveCriteria primitiveCriteria) { criteria = primitiveCriteria.Value; @@ -345,12 +345,9 @@ public async Task Delete(CriteriaRequest request) #region Criteria - private static object? GetCriteria(ApplicationContext applicationContext, byte[]? criteriaData) + private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData) { - object? criteria = null; - if (criteriaData != null) - criteria = applicationContext.GetRequiredService().Deserialize(criteriaData); - return criteria; + return applicationContext.GetRequiredService().Deserialize(criteriaData) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed); } #endregion Criteria diff --git a/Source/Csla.Channels.RabbitMq/RabbitMqPortal.cs b/Source/Csla.Channels.RabbitMq/RabbitMqPortal.cs index 1e266edc9a..c07946d6c6 100644 --- a/Source/Csla.Channels.RabbitMq/RabbitMqPortal.cs +++ b/Source/Csla.Channels.RabbitMq/RabbitMqPortal.cs @@ -238,7 +238,7 @@ private async Task Update(UpdateRequest request) { request = ConvertRequest(request); // unpack object - object obj = GetCriteria(_applicationContext, request.ObjectData) ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized); + object obj = GetCriteria(_applicationContext, request.ObjectData ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized)); var context = new DataPortalContext( _applicationContext, Deserialize(request.Principal), @@ -278,7 +278,7 @@ private async Task Delete(CriteriaRequest request) request = ConvertRequest(request); // unpack criteria data into object - object? criteria = GetCriteria(_applicationContext, request.CriteriaData); + object criteria = GetCriteria(_applicationContext, request.CriteriaData); if (criteria is DataPortalClient.PrimitiveCriteria primitiveCriteria) { criteria = primitiveCriteria.Value; @@ -312,12 +312,9 @@ private async Task Delete(CriteriaRequest request) #region Criteria - private static object? GetCriteria(ApplicationContext applicationContext, byte[]? criteriaData) + private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData) { - object? criteria = null; - if (criteriaData != null) - criteria = applicationContext.GetRequiredService().Deserialize(criteriaData); - return criteria; + return applicationContext.GetRequiredService().Deserialize(criteriaData) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed); } #endregion Criteria diff --git a/Source/Csla.Channels.RabbitMq/RabbitMqProxy.cs b/Source/Csla.Channels.RabbitMq/RabbitMqProxy.cs index 9cb23f2a96..a1bcd96669 100644 --- a/Source/Csla.Channels.RabbitMq/RabbitMqProxy.cs +++ b/Source/Csla.Channels.RabbitMq/RabbitMqProxy.cs @@ -113,16 +113,15 @@ private void DisposeRabbitMq() Connection = null; } - /// - /// Called by to create a - /// new business object. - /// - /// Type of business object to create. - /// Criteria object describing business object. - /// DataPortalContext object passed to the server. - /// True if the client-side proxy should synchronously invoke the server. - public override async Task Create(Type objectType, object? criteria, DataPortalContext context, bool isSync) + /// + public override async Task Create(Type objectType, object criteria, DataPortalContext context, bool isSync) { + if (objectType is null) + throw new ArgumentNullException(nameof(objectType)); + if (criteria is null) + throw new ArgumentNullException(nameof(criteria)); + if (context is null) + throw new ArgumentNullException(nameof(context)); if (isSync) throw new NotSupportedException("isSync == true"); @@ -137,18 +136,15 @@ public override async Task Create(Type objectType, object? cri } } - /// - /// Called by to load an - /// existing business object. - /// - /// Type of business object to create. - /// Criteria object describing business object. - /// - /// object passed to the server. - /// - /// True if the client-side proxy should synchronously invoke the server. - public override async Task Fetch(Type objectType, object? criteria, DataPortalContext context, bool isSync) + /// + public override async Task Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { + if (objectType is null) + throw new ArgumentNullException(nameof(objectType)); + if (criteria is null) + throw new ArgumentNullException(nameof(criteria)); + if (context is null) + throw new ArgumentNullException(nameof(context)); if (isSync) throw new NotSupportedException("isSync == true"); @@ -163,17 +159,12 @@ public override async Task Fetch(Type objectType, object? crit } } - /// - /// Called by to update a - /// business object. - /// - /// The business object to update. - /// - /// object passed to the server. - /// - /// True if the client-side proxy should synchronously invoke the server. public override async Task Update(object obj, DataPortalContext context, bool isSync) { + if (obj is null) + throw new ArgumentNullException(nameof(obj)); + if (context is null) + throw new ArgumentNullException(nameof(context)); if (isSync) throw new NotSupportedException("isSync == true"); @@ -188,18 +179,15 @@ public override async Task Update(object obj, DataPortalContex } } - /// - /// Called by to delete a - /// business object. - /// - /// Type of business object to create. - /// Criteria object describing business object. - /// - /// object passed to the server. - /// - /// True if the client-side proxy should synchronously invoke the server. - public override async Task Delete(Type objectType, object? criteria, DataPortalContext context, bool isSync) + /// + public override async Task Delete(Type objectType, object criteria, DataPortalContext context, bool isSync) { + if (objectType is null) + throw new ArgumentNullException(nameof(objectType)); + if (criteria is null) + throw new ArgumentNullException(nameof(criteria)); + if (context is null) + throw new ArgumentNullException(nameof(context)); if (isSync) throw new NotSupportedException("isSync == true"); diff --git a/Source/Csla.Web.Mvc.Shared/ApplicationContextManager.cs b/Source/Csla.Web.Mvc.Shared/ApplicationContextManager.cs index 254676817f..201badc509 100644 --- a/Source/Csla.Web.Mvc.Shared/ApplicationContextManager.cs +++ b/Source/Csla.Web.Mvc.Shared/ApplicationContextManager.cs @@ -54,7 +54,7 @@ public System.Security.Principal.IPrincipal GetUser() /// Sets the current principal. /// /// Principal object. - public void SetUser(System.Security.Principal.IPrincipal principal) + public void SetUser(System.Security.Principal.IPrincipal? principal) { HttpContext.Current.User = principal; } diff --git a/Source/Csla.Web.Mvc.Shared/Server/Hosts/HttpPortal.cs b/Source/Csla.Web.Mvc.Shared/Server/Hosts/HttpPortal.cs index e31d0e8768..3333959763 100644 --- a/Source/Csla.Web.Mvc.Shared/Server/Hosts/HttpPortal.cs +++ b/Source/Csla.Web.Mvc.Shared/Server/Hosts/HttpPortal.cs @@ -21,8 +21,8 @@ namespace Csla.Server.Hosts /// public class HttpPortal { - private IDataPortalServer dataPortalServer; - private ApplicationContext _applicationContext; + private readonly IDataPortalServer dataPortalServer; + private readonly ApplicationContext _applicationContext; /// /// Creates an instance of the type @@ -31,16 +31,20 @@ public class HttpPortal /// Data portal server service public HttpPortal(ApplicationContext applicationContext, IDataPortalServer dataPortal) { - dataPortalServer = dataPortal; - _applicationContext = applicationContext; + dataPortalServer = dataPortal ?? throw new ArgumentNullException(nameof(dataPortal)); + _applicationContext = applicationContext ?? throw new ArgumentNullException(nameof(applicationContext)); } /// /// Create and initialize an existing business object. /// /// The request parameter object. + /// is . public async Task Create(CriteriaRequest request) { + if (request is null) + throw new ArgumentNullException(nameof(request)); + var result = _applicationContext.CreateInstanceDI(); try { @@ -83,8 +87,12 @@ public async Task Create(CriteriaRequest request) /// Get an existing business object. /// /// The request parameter object. + /// is . public async Task Fetch(CriteriaRequest request) { + if (request is null) + throw new ArgumentNullException(nameof(request)); + var result = _applicationContext.CreateInstanceDI(); try { @@ -127,14 +135,18 @@ public async Task Fetch(CriteriaRequest request) /// Update a business object. /// /// The request parameter object. + /// is . public async Task Update(UpdateRequest request) { + if (request is null) + throw new ArgumentNullException(nameof(request)); + var result = _applicationContext.CreateInstanceDI(); try { request = ConvertRequest(request); // unpack object - object obj = GetCriteria(_applicationContext, request.ObjectData) ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized); + object obj = GetCriteria(_applicationContext, request.ObjectData ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized)); var context = new DataPortalContext( _applicationContext, Deserialize(request.Principal), @@ -166,8 +178,12 @@ public async Task Update(UpdateRequest request) /// Delete a business object. /// /// The request parameter object. + /// is . public async Task Delete(CriteriaRequest request) { + if (request is null) + throw new ArgumentNullException(nameof(request)); + var result = _applicationContext.CreateInstanceDI(); try { @@ -208,12 +224,9 @@ public async Task Delete(CriteriaRequest request) #region Criteria - private static object? GetCriteria(ApplicationContext applicationContext, byte[]? criteriaData) + private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData) { - object? criteria = null; - if (criteriaData != null) - criteria = applicationContext.GetRequiredService().Deserialize(criteriaData); - return criteria; + return applicationContext.GetRequiredService().Deserialize(criteriaData) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed); } #endregion Criteria @@ -256,9 +269,7 @@ private T Deserialize(byte[] data) { var deserializedData = _applicationContext.GetRequiredService().Deserialize(data) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed); if (deserializedData is not T castedData) - { throw new SerializationException(string.Format(Resources.DeserializationFailedDueToWrongData, typeof(T).FullName)); - } return castedData; } diff --git a/Source/Csla/DataPortalClient/DataPortalProxy.cs b/Source/Csla/DataPortalClient/DataPortalProxy.cs index 75739d0607..ebc0f913cd 100644 --- a/Source/Csla/DataPortalClient/DataPortalProxy.cs +++ b/Source/Csla/DataPortalClient/DataPortalProxy.cs @@ -49,23 +49,21 @@ protected DataPortalProxy(ApplicationContext applicationContext) public abstract string DataPortalUrl { get; } /// - public async virtual Task Create(Type objectType, object? criteria, DataPortalContext context, bool isSync) + public async virtual Task Create(Type objectType, object criteria, DataPortalContext context, bool isSync) { if (objectType is null) throw new ArgumentNullException(nameof(objectType)); + if (criteria is null) + throw new ArgumentNullException(nameof(criteria)); if (context is null) throw new ArgumentNullException(nameof(context)); DataPortalResult result; try { - var request = GetBaseCriteriaRequest(); + var request = GetBaseCriteriaRequest(criteria); request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName!); - if (criteria is not IMobileObject) - { - criteria = new PrimitiveCriteria(criteria); - } - request.CriteriaData = ApplicationContext.GetRequiredService().Serialize(criteria); + request = ConvertRequest(request); var serialized = ApplicationContext.GetRequiredService().Serialize(request); serialized = await CallDataPortalServer(serialized, "create", GetRoutingToken(objectType), isSync).ConfigureAwait(false); @@ -100,23 +98,20 @@ public async virtual Task Create(Type objectType, object? crit } /// - public async virtual Task Fetch(Type objectType, object? criteria, DataPortalContext context, bool isSync) + public async virtual Task Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { if (objectType is null) throw new ArgumentNullException(nameof(objectType)); + if (criteria is null) + throw new ArgumentNullException(nameof(criteria)); if (context is null) throw new ArgumentNullException(nameof(context)); DataPortalResult result; try { - var request = GetBaseCriteriaRequest(); + var request = GetBaseCriteriaRequest(criteria); request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName!); - if (criteria is not IMobileObject) - { - criteria = new PrimitiveCriteria(criteria); - } - request.CriteriaData = ApplicationContext.GetRequiredService().Serialize(criteria); request = ConvertRequest(request); var serialized = ApplicationContext.GetRequiredService().Serialize(request); @@ -163,8 +158,8 @@ public async virtual Task Update(object obj, DataPortalContext DataPortalResult result; try { - var request = GetBaseUpdateCriteriaRequest(); - request.ObjectData = ApplicationContext.GetRequiredService().Serialize(obj); + var request = GetBaseUpdateCriteriaRequest(obj); + request = ConvertRequest(request); var serialized = ApplicationContext.GetRequiredService().Serialize(request); @@ -201,23 +196,20 @@ public async virtual Task Update(object obj, DataPortalContext } /// - public async virtual Task Delete(Type objectType, object? criteria, DataPortalContext context, bool isSync) + public async virtual Task Delete(Type objectType, object criteria, DataPortalContext context, bool isSync) { if (objectType is null) throw new ArgumentNullException(nameof(objectType)); + if (criteria is null) + throw new ArgumentNullException(nameof(criteria)); if (context is null) throw new ArgumentNullException(nameof(context)); DataPortalResult result; try { - var request = GetBaseCriteriaRequest(); + var request = GetBaseCriteriaRequest(criteria); request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName!); - if (criteria is not IMobileObject) - { - criteria = new PrimitiveCriteria(criteria); - } - request.CriteriaData = ApplicationContext.GetRequiredService().Serialize(criteria); request = ConvertRequest(request); var serialized = ApplicationContext.GetRequiredService().Serialize(request); @@ -336,7 +328,23 @@ internal bool ExecutionIsNotOnLogicalOrPhysicalServer #region Criteria - private CriteriaRequest GetBaseCriteriaRequest() + private CriteriaRequest GetBaseCriteriaRequest(object criteria) + { + if (criteria is not IMobileObject) + criteria = new PrimitiveCriteria(criteria); + + var criteriaData = ApplicationContext.GetRequiredService().Serialize(criteria); + + return CreateRequest(criteriaData); + + } + + private UpdateRequest GetBaseUpdateCriteriaRequest(object obj) + { + return CreateRequest(ApplicationContext.GetRequiredService().Serialize(obj)); + } + + private T CreateRequest(object payload) { var securityOptions = ApplicationContext.GetRequiredService(); @@ -346,19 +354,7 @@ private CriteriaRequest GetBaseCriteriaRequest() var clientCulture = System.Globalization.CultureInfo.CurrentCulture.Name; var clientUICulture = System.Globalization.CultureInfo.CurrentUICulture.Name; - return ApplicationContext.CreateInstanceDI(principal, clientContext, clientCulture, clientUICulture); - } - - private UpdateRequest GetBaseUpdateCriteriaRequest() - { - var result = ApplicationContext.CreateInstanceDI(); - var securityOptions = ApplicationContext.GetRequiredService(); - result.ObjectData = null; - result.ClientContext = ApplicationContext.GetRequiredService().Serialize(ApplicationContext.ClientContext); - result.Principal = ApplicationContext.GetRequiredService().Serialize(securityOptions.FlowSecurityPrincipalFromClient ? ApplicationContext.User : null); - result.ClientCulture = Thread.CurrentThread.CurrentCulture.Name; - result.ClientUICulture = Thread.CurrentThread.CurrentUICulture.Name; - return result; + return ApplicationContext.CreateInstanceDI(principal, clientContext, clientCulture, clientUICulture, payload); } #endregion Criteria diff --git a/Source/Csla/DataPortalClient/PrimitiveCriteria.cs b/Source/Csla/DataPortalClient/PrimitiveCriteria.cs index 011b3d7449..ebfb477cc2 100644 --- a/Source/Csla/DataPortalClient/PrimitiveCriteria.cs +++ b/Source/Csla/DataPortalClient/PrimitiveCriteria.cs @@ -19,22 +19,25 @@ public class PrimitiveCriteria : IMobileObject /// /// Creates an instance of the type. /// + [Obsolete(MobileFormatter.DefaultCtorObsoleteMessage, error: true)] +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. It's okay to suppress because it can't be used by user code public PrimitiveCriteria() { } +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. /// /// New instance of the criteria /// /// Criteria value - public PrimitiveCriteria(object? value) + public PrimitiveCriteria(object value) { _value = value; } - private object? _value; + private object _value; /// /// Criteria value /// - public object? Value + public object Value { get { return _value; } private set { _value = value; } diff --git a/Source/Csla/Server/Hosts/DataPortalChannel/CriteriaRequest.cs b/Source/Csla/Server/Hosts/DataPortalChannel/CriteriaRequest.cs index 7ffd8a4971..2f1be77a7e 100644 --- a/Source/Csla/Server/Hosts/DataPortalChannel/CriteriaRequest.cs +++ b/Source/Csla/Server/Hosts/DataPortalChannel/CriteriaRequest.cs @@ -36,15 +36,15 @@ public string TypeName /// /// Serialized data for the criteria object. /// - public static readonly PropertyInfo CriteriaDataProperty = RegisterProperty(nameof(CriteriaData)); + public static readonly PropertyInfo CriteriaDataProperty = RegisterProperty(nameof(CriteriaData)); /// /// Serialized data for the criteria object. /// - public byte[]? CriteriaData + public byte[] CriteriaData { get { return GetProperty(CriteriaDataProperty); } - set { LoadProperty(CriteriaDataProperty, value); } + set { LoadProperty(CriteriaDataProperty, value ?? throw new ArgumentNullException(nameof(CriteriaData))); } } /// @@ -118,15 +118,15 @@ public string ClientUICulture /// /// /// - /// , , or is . - public CriteriaRequest(byte[] principal, byte[] clientContext, string clientCulture, string clientUICulture) + /// , , , or is . + public CriteriaRequest(byte[] principal, byte[] clientContext, string clientCulture, string clientUICulture, byte[] criteriaData) { Principal = principal ?? throw new ArgumentNullException(nameof(principal)); ClientContext = clientContext ?? throw new ArgumentNullException(nameof(clientContext)); ClientCulture = clientCulture ?? throw new ArgumentNullException(nameof(clientCulture)); ClientUICulture = clientUICulture ?? throw new ArgumentNullException(nameof(clientUICulture)); TypeName = string.Empty; - CriteriaData = null; + CriteriaData = criteriaData ?? throw new ArgumentNullException(nameof(criteriaData)); } /// diff --git a/Source/Csla/Server/Hosts/DataPortalChannel/UpdateRequest.cs b/Source/Csla/Server/Hosts/DataPortalChannel/UpdateRequest.cs index 8e76a57006..9a42a41699 100644 --- a/Source/Csla/Server/Hosts/DataPortalChannel/UpdateRequest.cs +++ b/Source/Csla/Server/Hosts/DataPortalChannel/UpdateRequest.cs @@ -20,15 +20,15 @@ public class UpdateRequest : ReadOnlyBase /// /// Serialized object data. /// - public static readonly PropertyInfo ObjectDataProperty = RegisterProperty(nameof(ObjectData)); + public static readonly PropertyInfo ObjectDataProperty = RegisterProperty(nameof(ObjectData)); /// /// Serialized object data. /// - public byte[]? ObjectData + public byte[] ObjectData { get { return GetProperty(ObjectDataProperty); } - set { LoadProperty(ObjectDataProperty, value); } + set { LoadProperty(ObjectDataProperty, value ?? throw new ArgumentNullException(nameof(ObjectData))); } } /// @@ -102,14 +102,15 @@ public string ClientUICulture /// /// /// - /// , , or is . - public UpdateRequest(byte[] principal, byte[] clientContext, string clientCulture, string clientUICulture) + /// + /// , , , or is . + public UpdateRequest(byte[] principal, byte[] clientContext, string clientCulture, string clientUICulture, byte[] objectData) { Principal = principal ?? throw new ArgumentNullException(nameof(principal)); ClientContext = clientContext ?? throw new ArgumentNullException(nameof(clientContext)); ClientCulture = clientCulture ?? throw new ArgumentNullException(nameof(clientCulture)); ClientUICulture = clientUICulture ?? throw new ArgumentNullException(nameof(clientUICulture)); - ObjectData = null; + ObjectData = objectData ?? throw new ArgumentNullException(nameof(objectData)); } ///