From e420fce3a618739e1427d33c0cb91454fe1988ea Mon Sep 17 00:00:00 2001 From: "D. Ror" Date: Wed, 28 Jun 2023 16:44:08 -0400 Subject: [PATCH] [Backend] Resolve CA1711, CA1725, CA2215, CA2229 (#2328) --- .editorconfig | 2 -- Backend.Tests/Backend.Tests.csproj | 2 +- Backend/Helper/FileOperations.cs | 4 ++++ Backend/Helper/FileStorage.cs | 17 +++++++++++++++-- Backend/Helper/LiftHelper.cs | 4 ++++ Backend/Interfaces/ILiftService.cs | 20 ++++++++++---------- Backend/Models/UserRole.cs | 3 +++ Backend/Services/LiftService.cs | 11 ++++++++--- Backend/Services/MergeService.cs | 4 ++++ Backend/Services/PermissionService.cs | 4 ++++ Backend/Services/UserEditService.cs | 8 ++++---- 11 files changed, 57 insertions(+), 22 deletions(-) diff --git a/.editorconfig b/.editorconfig index 34e8772e70..458384a0d1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,8 +10,6 @@ indent_size = 4 # CA1305 requires using a FormatProvider with int.Parse and string.Format. dotnet_diagnostic.CA1305.severity = none dotnet_diagnostic.CA1710.severity = warning -# CA1816 is our only exception to Minimum. -dotnet_diagnostic.CA1816.severity = none # CS1591 is our only exception to EnforceCodeStyleInBuild+GenerateDocumentationFile. dotnet_diagnostic.CS1591.severity = none # IDE0005 requires both EnforceCodeStyleInBuild and GenerateDocumentationFile set to true. diff --git a/Backend.Tests/Backend.Tests.csproj b/Backend.Tests/Backend.Tests.csproj index a4ebcd60e9..eb93543609 100644 --- a/Backend.Tests/Backend.Tests.csproj +++ b/Backend.Tests/Backend.Tests.csproj @@ -9,7 +9,7 @@ Recommended true true - $(NoWarn);CA1305;CA1816;CS1591 + $(NoWarn);CA1305;CS1591 diff --git a/Backend/Helper/FileOperations.cs b/Backend/Helper/FileOperations.cs index bac89967ca..4af9c2eb86 100644 --- a/Backend/Helper/FileOperations.cs +++ b/Backend/Helper/FileOperations.cs @@ -3,6 +3,7 @@ using System.IO; using System.IO.Compression; using System.Linq; +using System.Runtime.Serialization; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -13,6 +14,9 @@ public class InvalidFileException : Exception { public InvalidFileException(string message) : base(message) { } + protected InvalidFileException(SerializationInfo info, StreamingContext context) + : base(info, context) { } + } /// diff --git a/Backend/Helper/FileStorage.cs b/Backend/Helper/FileStorage.cs index e258b62f95..5eae788d3b 100644 --- a/Backend/Helper/FileStorage.cs +++ b/Backend/Helper/FileStorage.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Runtime.Serialization; namespace BackendFramework.Helper { @@ -24,11 +25,23 @@ public enum FileType /// Indicates that an error occurred locating the current user's home directory. [Serializable] - public class HomeFolderNotFoundException : Exception { } + public class HomeFolderNotFoundException : Exception + { + public HomeFolderNotFoundException() { } + + protected HomeFolderNotFoundException(SerializationInfo info, StreamingContext context) + : base(info, context) { } + } /// Indicates an invalid input id. [Serializable] - public class InvalidIdException : Exception { } + public class InvalidIdException : Exception + { + public InvalidIdException() { } + + protected InvalidIdException(SerializationInfo info, StreamingContext context) + : base(info, context) { } + } /// /// Generate a path to the file name of an audio file for the Project based on the Word ID. diff --git a/Backend/Helper/LiftHelper.cs b/Backend/Helper/LiftHelper.cs index bd4485d0aa..78cd6bb3b1 100644 --- a/Backend/Helper/LiftHelper.cs +++ b/Backend/Helper/LiftHelper.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Runtime.Serialization; using SIL.Lift.Parsing; namespace BackendFramework.Helper @@ -10,6 +11,9 @@ public class InvalidLiftFileException : InvalidFileException { public InvalidLiftFileException(string message) : base("Malformed LIFT file: " + message) { } + protected InvalidLiftFileException(SerializationInfo info, StreamingContext context) + : base(info, context) { } + } public static class LiftHelper diff --git a/Backend/Interfaces/ILiftService.cs b/Backend/Interfaces/ILiftService.cs index de80c9e40c..434b3e87ea 100644 --- a/Backend/Interfaces/ILiftService.cs +++ b/Backend/Interfaces/ILiftService.cs @@ -8,18 +8,18 @@ namespace BackendFramework.Interfaces public interface ILiftService { ILiftMerger GetLiftImporterExporter(string projectId, IWordRepository wordRepo); - Task LdmlImport(string filePath, IProjectRepository projRepo, Project project); + Task LdmlImport(string dirPath, IProjectRepository projRepo, Project project); Task LiftExport(string projectId, IWordRepository wordRepo, IProjectRepository projRepo); - // Methods to store, retrieve, and delete an export/import string in a common dictionary. - void StoreExport(string key, string filePath); - string? RetrieveExport(string key); - bool DeleteExport(string key); - void SetExportInProgress(string key, bool isInProgress); - bool IsExportInProgress(string key); - void StoreImport(string key, string filePath); - string? RetrieveImport(string key); - bool DeleteImport(string key); + // Methods to store, retrieve, and delete an export string in a common dictionary. + void StoreExport(string userId, string filePath); + string? RetrieveExport(string userId); + bool DeleteExport(string userId); + void SetExportInProgress(string userId, bool isInProgress); + bool IsExportInProgress(string userId); + void StoreImport(string userId, string filePath); + string? RetrieveImport(string userId); + bool DeleteImport(string userId); } public interface ILiftMerger : ILexiconMerger diff --git a/Backend/Models/UserRole.cs b/Backend/Models/UserRole.cs index e763311c00..05fe28259f 100644 --- a/Backend/Models/UserRole.cs +++ b/Backend/Models/UserRole.cs @@ -72,7 +72,10 @@ public override int GetHashCode() } } +#pragma warning disable CA1711 + // Ignoring CA1711, which requires identifiers ending in Permission to implement System.Security.IPermission. public enum Permission +#pragma warning restore CA1711 { /// Project Owner by default should be given to the user who created the project Owner = 6, diff --git a/Backend/Services/LiftService.cs b/Backend/Services/LiftService.cs index dfb41a7f93..9f87801770 100644 --- a/Backend/Services/LiftService.cs +++ b/Backend/Services/LiftService.cs @@ -4,6 +4,7 @@ using System.IO.Compression; using System.Linq; using System.Reflection; +using System.Runtime.Serialization; using System.Security; using System.Text; using System.Threading.Tasks; @@ -49,12 +50,13 @@ protected override void InsertPronunciationIfNeeded( } } - // This raises error CA1816, which is currently suppressed in .editorconfig and with . +#pragma warning disable CA1816, CA2215 public override void Dispose() { // TODO: When updating the LiftWriter dependency, check to see if its Dispose() implementation // has been fixed properly to avoid needing to override its Dispose method. // https://github.com/sillsdev/libpalaso/blob/master/SIL.DictionaryServices/Lift/LiftWriter.cs + // Also, re-evaluate our CA1816 violation. Dispose(true); } @@ -73,9 +75,10 @@ protected override void Dispose(bool disposing) Disposed = true; - // Generally, the base class Dispose method would be called here, but it accesses _writer, + // Generally, the base class Dispose method would be called here (CA2215), but it accesses _writer, // and we are disposing of that ourselves in the child class to fix a memory leak. } +#pragma warning restore CA1816, CA2215 } [Serializable] @@ -83,6 +86,8 @@ public class MissingProjectException : Exception { public MissingProjectException(string message) : base(message) { } + protected MissingProjectException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } public class LiftService : ILiftService @@ -105,7 +110,7 @@ public LiftService() } /// Store status that a user's export is in-progress. - public void SetExportInProgress(string userId, bool isInProgress = true) + public void SetExportInProgress(string userId, bool isInProgress) { _liftExports.Remove(userId); if (isInProgress) diff --git a/Backend/Services/MergeService.cs b/Backend/Services/MergeService.cs index 782d4c0334..0dbf20b6a7 100644 --- a/Backend/Services/MergeService.cs +++ b/Backend/Services/MergeService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using System.Runtime.Serialization; using BackendFramework.Helper; using BackendFramework.Interfaces; using BackendFramework.Models; @@ -223,6 +224,9 @@ public class InvalidBlacklistEntryException : Exception public InvalidBlacklistEntryException() { } public InvalidBlacklistEntryException(string message) : base(message) { } + + protected InvalidBlacklistEntryException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } } } diff --git a/Backend/Services/PermissionService.cs b/Backend/Services/PermissionService.cs index 33407c9d55..0917cf554c 100644 --- a/Backend/Services/PermissionService.cs +++ b/Backend/Services/PermissionService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; +using System.Runtime.Serialization; using System.Security.Claims; using System.Text; using System.Text.Json; @@ -244,6 +245,9 @@ public InvalidJwtTokenException() { } public InvalidJwtTokenException(string msg) : base(msg) { } public InvalidJwtTokenException(string msg, Exception innerException) : base(msg, innerException) { } + + protected InvalidJwtTokenException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } } diff --git a/Backend/Services/UserEditService.cs b/Backend/Services/UserEditService.cs index f209c969ed..565e5b3e34 100644 --- a/Backend/Services/UserEditService.cs +++ b/Backend/Services/UserEditService.cs @@ -61,7 +61,7 @@ public async Task> AddGoalToUserEdit(string projectId, string u /// Adds a string representation of a step to a specified /// A bool: success of operation - public async Task AddStepToGoal(string projectId, string userEditId, int goalIndex, string newStep) + public async Task AddStepToGoal(string projectId, string userEditId, int goalIndex, string stepString) { var oldUserEdit = await _userEditRepo.GetUserEdit(projectId, userEditId); if (oldUserEdit is null || goalIndex >= oldUserEdit.Edits.Count) @@ -70,7 +70,7 @@ public async Task AddStepToGoal(string projectId, string userEditId, int g } var newUserEdit = oldUserEdit.Clone(); - newUserEdit.Edits[goalIndex].StepData.Add(newStep); + newUserEdit.Edits[goalIndex].StepData.Add(stepString); var updateResult = await _userEditRepo.Replace(projectId, userEditId, newUserEdit); return updateResult; } @@ -78,7 +78,7 @@ public async Task AddStepToGoal(string projectId, string userEditId, int g /// Updates a specified step to in a specified /// A bool: success of operation public async Task UpdateStepInGoal( - string projectId, string userEditId, int goalIndex, string updatedStep, int stepIndex) + string projectId, string userEditId, int goalIndex, string stepString, int stepIndex) { var oldUserEdit = await _userEditRepo.GetUserEdit(projectId, userEditId); if (oldUserEdit is null || goalIndex >= oldUserEdit.Edits.Count @@ -88,7 +88,7 @@ public async Task UpdateStepInGoal( } var newUserEdit = oldUserEdit.Clone(); - newUserEdit.Edits[goalIndex].StepData[stepIndex] = updatedStep; + newUserEdit.Edits[goalIndex].StepData[stepIndex] = stepString; var updateResult = await _userEditRepo.Replace(projectId, userEditId, newUserEdit); return updateResult; }