diff --git a/src/MaksIT.Core/Abstractions/Webapi/PatchRequestModelBase.cs b/src/MaksIT.Core/Abstractions/Webapi/PatchRequestModelBase.cs index 3813e04..35be9b2 100644 --- a/src/MaksIT.Core/Abstractions/Webapi/PatchRequestModelBase.cs +++ b/src/MaksIT.Core/Abstractions/Webapi/PatchRequestModelBase.cs @@ -2,21 +2,34 @@ using System.ComponentModel.DataAnnotations; using MaksIT.Core.Webapi.Models; +using System.Diagnostics.CodeAnalysis; namespace MaksIT.Core.Abstractions.Webapi; public abstract class PatchRequestModelBase : RequestModelBase { - public Dictionary Operations = new Dictionary(); + public Dictionary? Operations { get; set; } private bool HasNonNullPatchField => GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(prop => prop.Name != nameof(Operations)) .Any(prop => prop.GetValue(this) != null); - public bool TryGetOperation(string propertyName, out PatchOperation operation) { - return Operations.TryGetValue(propertyName, out operation); + public bool TryGetOperation(string propertyName, [NotNullWhen(true)] out PatchOperation? operation) { + if (Operations == null) { + operation = null; + return false; + } + + if (Operations.TryGetValue(propertyName, out var tempOperation)) { + operation = tempOperation; + return true; + } + else { + operation = null; + return false; + } } public override IEnumerable Validate(ValidationContext validationContext) { @@ -24,9 +37,11 @@ public abstract class PatchRequestModelBase : RequestModelBase { yield return new ValidationResult("At least one patch field must be provided", new string[] { "PatchField" }); } - foreach (var operation in Operations) { - if (!Enum.IsDefined(typeof(PatchOperation), operation.Value)) { - yield return new ValidationResult($"Invalid patch operation '{operation.Value}' for property '{operation.Key}'", new string[] { operation.Key }); + if (Operations != null) { + foreach (var operation in Operations) { + if (!Enum.IsDefined(typeof(PatchOperation), operation.Value)) { + yield return new ValidationResult($"Invalid patch operation '{operation.Value}' for property '{operation.Key}'", new string[] { operation.Key }); + } } } } diff --git a/src/MaksIT.Core/MaksIT.Core.csproj b/src/MaksIT.Core/MaksIT.Core.csproj index 4607f63..79504d9 100644 --- a/src/MaksIT.Core/MaksIT.Core.csproj +++ b/src/MaksIT.Core/MaksIT.Core.csproj @@ -8,7 +8,7 @@ MaksIT.Core - 1.2.5 + 1.2.6 Maksym Sadovnychyy MAKS-IT MaksIT.Core