From e8a4d52797f52ac8d65ff25b9d3072eee1bda686 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 10 Apr 2020 22:51:34 +0200 Subject: [PATCH] Add ValidatedNotNullAttribute (for SonarQube) (#452) * ValidatedNotNull * usings --- src/WireMock.Net/Util/BodyParser.cs | 4 +- src/WireMock.Net/Validation/Check.cs | 50 +++++++++---------- .../Validation/ValidatedNotNullAttribute.cs | 11 ++++ 3 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs diff --git a/src/WireMock.Net/Util/BodyParser.cs b/src/WireMock.Net/Util/BodyParser.cs index b890696c..826b399e 100644 --- a/src/WireMock.Net/Util/BodyParser.cs +++ b/src/WireMock.Net/Util/BodyParser.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; -using System.IO.Compression; using System.Linq; using System.Net.Http.Headers; using System.Text; diff --git a/src/WireMock.Net/Validation/Check.cs b/src/WireMock.Net/Validation/Check.cs index 90a298a1..bd42dd22 100644 --- a/src/WireMock.Net/Validation/Check.cs +++ b/src/WireMock.Net/Validation/Check.cs @@ -16,7 +16,7 @@ namespace WireMock.Validation internal static class Check { [ContractAnnotation("value:null => halt")] - public static T Condition([NoEnumeration] T value, [NotNull] Predicate condition, [InvokerParameterName] [NotNull] string parameterName) + public static T Condition([NoEnumeration] T value, [ValidatedNotNull, NotNull] Predicate condition, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) { NotNull(condition, nameof(condition)); NotNull(value, nameof(value)); @@ -32,7 +32,7 @@ namespace WireMock.Validation } [ContractAnnotation("value:null => halt")] - public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName) + public static T NotNull([NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) { if (ReferenceEquals(value, null)) { @@ -45,7 +45,7 @@ namespace WireMock.Validation } [ContractAnnotation("value:null => halt")] - public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName, [NotNull] string propertyName) + public static T NotNull([NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName, [ValidatedNotNull, NotNull] string propertyName) { if (ReferenceEquals(value, null)) { @@ -59,7 +59,7 @@ namespace WireMock.Validation } [ContractAnnotation("value:null => halt")] - public static IList NotNullOrEmpty(IList value, [InvokerParameterName] [NotNull] string parameterName) + public static IList NotNullOrEmpty(IList value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) { NotNull(value, parameterName); @@ -74,7 +74,7 @@ namespace WireMock.Validation } [ContractAnnotation("value:null => halt")] - public static string NotNullOrEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) + public static string NotNullOrEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) { Exception e = null; if (ReferenceEquals(value, null)) @@ -96,20 +96,20 @@ namespace WireMock.Validation return value; } - public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) - { - if (!ReferenceEquals(value, null) - && (value.Length == 0)) - { - NotNullOrEmpty(parameterName, nameof(parameterName)); + //public static string NullButNotEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) + //{ + // if (!ReferenceEquals(value, null) + // && (value.Length == 0)) + // { + // NotNullOrEmpty(parameterName, nameof(parameterName)); - throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName)); - } + // throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName)); + // } - return value; - } + // return value; + //} - public static IList HasNoNulls(IList value, [InvokerParameterName] [NotNull] string parameterName) + public static IList HasNoNulls(IList value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) where T : class { NotNull(value, parameterName); @@ -124,16 +124,16 @@ namespace WireMock.Validation return value; } - public static Type ValidEntityType(Type value, [InvokerParameterName] [NotNull] string parameterName) - { - if (!value.GetTypeInfo().IsClass) - { - NotNullOrEmpty(parameterName, nameof(parameterName)); + //public static Type ValidEntityType(Type value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) + //{ + // if (!value.GetTypeInfo().IsClass) + // { + // NotNullOrEmpty(parameterName, nameof(parameterName)); - throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName)); - } + // throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName)); + // } - return value; - } + // return value; + //} } } \ No newline at end of file diff --git a/src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs b/src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs new file mode 100644 index 00000000..d41d9f1c --- /dev/null +++ b/src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs @@ -0,0 +1,11 @@ +using System; + +namespace WireMock.Validation +{ + /// + /// To fix 'xxx' is null on at least one execution path. See also https://rules.sonarsource.com/csharp/RSPEC-3900. + /// + internal class ValidatedNotNullAttribute : Attribute + { + } +}