From a348177126afd1a557f2758cfa9eec9e74e63429 Mon Sep 17 00:00:00 2001 From: Shannon Date: Sat, 2 Nov 2013 15:18:08 +1100 Subject: [PATCH 1/3] Fixes object extensions and tests --- src/Umbraco.Core/ObjectExtensions.cs | 131 +++++++++++---------- src/Umbraco.Tests/ObjectExtensionsTests.cs | 17 +++ 2 files changed, 87 insertions(+), 61 deletions(-) diff --git a/src/Umbraco.Core/ObjectExtensions.cs b/src/Umbraco.Core/ObjectExtensions.cs index 2b55a2276d..603c508e0f 100644 --- a/src/Umbraco.Core/ObjectExtensions.cs +++ b/src/Umbraco.Core/ObjectExtensions.cs @@ -97,8 +97,17 @@ namespace Umbraco.Core // if we've got a nullable of something, we try to convert directly to that thing. if (destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>)) { + var underlyingType = Nullable.GetUnderlyingType(destinationType); + + //special case for empty strings for bools/dates which should return null if an empty string + var asString = input as string; + if (asString != null && string.IsNullOrEmpty(asString) && (underlyingType == typeof(DateTime) || underlyingType == typeof(bool))) + { + return Attempt.Succeed(null); + } + // recursively call into myself with the inner (not-nullable) type and handle the outcome - var nonNullable = input.TryConvertTo(Nullable.GetUnderlyingType(destinationType)); + var nonNullable = input.TryConvertTo(underlyingType); // and if sucessful, fall on through to rewrap in a nullable; if failed, pass on the exception if (nonNullable.Success) @@ -202,78 +211,78 @@ namespace Umbraco.Core if (destinationType == typeof(string)) return Attempt.Succeed(input); - if (input == null || input.Length == 0) + if (string.IsNullOrEmpty(input)) { if (destinationType == typeof(Boolean)) return Attempt.Succeed(false); // special case for booleans, null/empty == false - else if (destinationType == typeof(DateTime)) - return Attempt.Succeed(false); + if (destinationType == typeof(DateTime)) + return Attempt.Succeed(DateTime.MinValue); } // we have a non-empty string, look for type conversions in the expected order of frequency of use... if (destinationType.IsPrimitive) { - if (destinationType == typeof(Int32)) + if (destinationType == typeof(Int32)) { Int32 value; return Int32.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); } - else if (destinationType == typeof(Int64)) - { - Int64 value; - return Int64.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(Boolean)) - { - Boolean value; - if (Boolean.TryParse(input, out value)) - return Attempt.Succeed(value); // don't declare failure so the CustomBooleanTypeConverter can try - } - else if (destinationType == typeof(Int16)) - { - Int16 value; - return Int16.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(Double)) - { - Double value; - return Double.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(Single)) - { - Single value; - return Single.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(Char)) - { - Char value; - return Char.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(Byte)) - { - Byte value; - return Byte.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(SByte)) - { - SByte value; - return SByte.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(UInt32)) - { - UInt32 value; - return UInt32.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(UInt16)) - { - UInt16 value; - return UInt16.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } - else if (destinationType == typeof(UInt64)) - { - UInt64 value; - return UInt64.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); - } + if (destinationType == typeof(Int64)) + { + Int64 value; + return Int64.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + if (destinationType == typeof(Boolean)) + { + Boolean value; + if (Boolean.TryParse(input, out value)) + return Attempt.Succeed(value); // don't declare failure so the CustomBooleanTypeConverter can try + } + else if (destinationType == typeof(Int16)) + { + Int16 value; + return Int16.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(Double)) + { + Double value; + return Double.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(Single)) + { + Single value; + return Single.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(Char)) + { + Char value; + return Char.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(Byte)) + { + Byte value; + return Byte.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(SByte)) + { + SByte value; + return SByte.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(UInt32)) + { + UInt32 value; + return UInt32.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(UInt16)) + { + UInt16 value; + return UInt16.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } + else if (destinationType == typeof(UInt64)) + { + UInt64 value; + return UInt64.TryParse(input, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + } } else if (destinationType == typeof(Guid)) { diff --git a/src/Umbraco.Tests/ObjectExtensionsTests.cs b/src/Umbraco.Tests/ObjectExtensionsTests.cs index f42f2445c2..b98e7607bb 100644 --- a/src/Umbraco.Tests/ObjectExtensionsTests.cs +++ b/src/Umbraco.Tests/ObjectExtensionsTests.cs @@ -114,6 +114,23 @@ namespace Umbraco.Tests Assert.AreEqual(DateTime.Equals(dateTime.Date, result.Result.Date), testCase.Value, testCase.Key); } } + + [Test] + public virtual void CanConvertBlankStringToNullDateTime() + { + var result = "".TryConvertTo(); + Assert.IsTrue(result.Success); + Assert.IsNull(result.Result); + } + + [Test] + public virtual void CanConvertBlankStringToNullBool() + { + var result = "".TryConvertTo(); + Assert.IsTrue(result.Success); + Assert.IsNull(result.Result); + } + [Test] public virtual void CanConvertBlankStringToDateTime() { From 37967973a0a0043db6beb7b3d9e387cf9fdf323a Mon Sep 17 00:00:00 2001 From: Shannon Date: Sat, 2 Nov 2013 15:18:29 +1100 Subject: [PATCH 2/3] Fixes config tests since we've removed this section --- .../UmbracoSettings/umbracoSettings.config | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/Umbraco.Tests/Configurations/UmbracoSettings/umbracoSettings.config b/src/Umbraco.Tests/Configurations/UmbracoSettings/umbracoSettings.config index c245b29154..363d0dd50a 100644 --- a/src/Umbraco.Tests/Configurations/UmbracoSettings/umbracoSettings.config +++ b/src/Umbraco.Tests/Configurations/UmbracoSettings/umbracoSettings.config @@ -96,20 +96,6 @@ error handler is defined then you'll see the Yellow Screen Of Death (YSOD) error page. Note the error can also be handled by the umbraco.macro.Error event, where you can log/alarm with your own code and change the behaviour per event. --> inline - - - HideFileDuplicates ashx,aspx,ascx,config,cshtml,vbhtml,asmx,air,axd From 45d339928ab5979b91e23785cc8161b2ab1dd8cc Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Sun, 3 Nov 2013 10:57:42 +0100 Subject: [PATCH 3/3] Include missing System.Net.Http/System.Net.Http.WebRequest --- src/Umbraco.Web.UI/Umbraco.Web.UI.csproj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index 5291f937a8..09ba05f489 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -183,7 +183,9 @@ - + + True + ..\packages\Microsoft.Net.Http.2.2.15\lib\net45\System.Net.Http.Extensions.dll @@ -193,7 +195,9 @@ ..\packages\Microsoft.Net.Http.2.2.15\lib\net45\System.Net.Http.Primitives.dll - + + True +