Skip to content

Commit 42d9ce2

Browse files
committed
Add string extensions & combine bool check
1 parent 9395b12 commit 42d9ce2

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace HubSpot.NET.Core.Extensions
8+
{
9+
public static class StringExtensions
10+
{
11+
public static bool IsNullOrEmpty(this string value)
12+
{
13+
return string.IsNullOrEmpty(value);
14+
}
15+
}
16+
}

HubSpot.NET/Core/Requests/RequestDataConverter.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Dynamic;
44
using System.Linq;
55
using System.Reflection;
6+
using HubSpot.NET.Core.Extensions;
67
using HubSpot.NET.Core.Interfaces;
78

89
namespace HubSpot.NET.Core.Requests
@@ -219,23 +220,21 @@ internal object ConvertSingleEntity(ExpandoObject dynamicObject, object dto)
219220

220221
if (targetProp != null)
221222
{
222-
var isNullable = false;
223-
if (targetProp.PropertyType.Name.Contains("Nullable"))
224-
{
225-
isNullable = true;
226-
}
227-
Type t = Nullable.GetUnderlyingType(targetProp.PropertyType) ?? targetProp.PropertyType;
223+
var isNullable = targetProp.PropertyType.Name.Contains("Nullable");
224+
225+
var type = Nullable.GetUnderlyingType(targetProp.PropertyType) ?? targetProp.PropertyType;
228226

229-
// resolves bug where if the object property was a nullable number (int/double/etc.) and the value being processed
227+
// resolves issue where if the object property was a nullable number (int/double/etc.) and the value being processed
230228
// was null or an empty string an exception 'string was not in the correct format' occurred.
231-
if (isNullable && (dynamicValue == null || string.IsNullOrEmpty(dynamicValue.ToString())))
229+
// see https://github.com/squaredup/HubSpot.NET/pull/8
230+
if (isNullable && (dynamicValue?.ToString()).IsNullOrEmpty())
232231
{
233232
// if nullable and the value to convert is null or an empty string it should not be converted
234233
targetProp.SetValue(dto, null);
235234
}
236235
else
237236
{
238-
var value = dynamicValue.GetType() == t ? dynamicValue : Convert.ChangeType(dynamicValue, t);
237+
var value = dynamicValue.GetType() == type ? dynamicValue : Convert.ChangeType(dynamicValue, type);
239238
targetProp.SetValue(dto, value);
240239
}
241240
}

0 commit comments

Comments
 (0)