From 23451f2c8539a8e9c21672f799240f6d5bb4efab Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 10 Apr 2020 20:37:56 +0200 Subject: [PATCH 1/2] ValidatedNotNull --- src/WireMock.Net/Validation/Check.cs | 50 +++++++++---------- .../Validation/ValidatedNotNullAttribute.cs | 11 ++++ 2 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs diff --git a/src/WireMock.Net/Validation/Check.cs b/src/WireMock.Net/Validation/Check.cs index 90a298a15..bd42dd228 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 @@ public static T Condition([NoEnumeration] T value, [NotNull] Predicate con } [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 @@ public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotN } [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 @@ public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotN } [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 @@ public static IList NotNullOrEmpty(IList value, [InvokerParameterName] } [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 @@ public static string NotNullOrEmpty(string value, [InvokerParameterName] [NotNul 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 @@ public static IList HasNoNulls(IList value, [InvokerParameterName] [Not 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 000000000..d41d9f1c1 --- /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 + { + } +} From 372ba87531c9bf7490cf0063c2e0b69cb9db4860 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 10 Apr 2020 20:42:33 +0200 Subject: [PATCH 2/2] usings --- src/WireMock.Net/Util/BodyParser.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/WireMock.Net/Util/BodyParser.cs b/src/WireMock.Net/Util/BodyParser.cs index b890696c7..826b399ef 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;