Skip to content

Commit c240c8c

Browse files
authored
Merge pull request #8 from bfutrell70/master
Fixed issue converting an empty object to a nullable numeric property
2 parents f4ca32e + 42d9ce2 commit c240c8c

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

HubSpot.NET.Examples/Deals.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ public static void Example()
3333
*/
3434
var deals = api.Deal.List<DealHubSpotModel>(false,
3535
new ListRequestOptions { PropertiesToInclude = new List<string> { "dealname", "amount" } });
36+
37+
/**
38+
* Get all deals
39+
*/
40+
//var moreResults = true;
41+
//long offset = 0;
42+
//while (moreResults)
43+
//{
44+
// var allDeals = api.Deal.List<DealHubSpotModel>(false,
45+
// new ListRequestOptions { PropertiesToInclude = new List<string> { "dealname", "amount", "hubspot_owner_id", "closedate" }, Limit = 100, Offset = offset });
46+
47+
// moreResults = allDeals.MoreResultsAvailable;
48+
// if (moreResults) offset = allDeals.ContinuationOffset;
49+
50+
//}
3651
}
3752
}
3853
}

HubSpot.NET/Api/Deal/Dto/DealHubSpotModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public DealHubSpotModel()
5353
public string CloseDate { get; set; }
5454

5555
[DataMember(Name = "amount")]
56-
public double Amount { get; set; }
56+
public double? Amount { get; set; }
5757

5858
[DataMember(Name = "dealtype")]
5959
public string DealType { get; set; }
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: 18 additions & 6 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,12 +220,23 @@ internal object ConvertSingleEntity(ExpandoObject dynamicObject, object dto)
219220

220221
if (targetProp != null)
221222
{
222-
223-
Type t = Nullable.GetUnderlyingType(targetProp.PropertyType) ?? targetProp.PropertyType;
224-
225-
var value = dynamicValue.GetType() == t ? dynamicValue : Convert.ChangeType(dynamicValue, t);
226-
227-
targetProp.SetValue(dto, value);
223+
var isNullable = targetProp.PropertyType.Name.Contains("Nullable");
224+
225+
var type = Nullable.GetUnderlyingType(targetProp.PropertyType) ?? targetProp.PropertyType;
226+
227+
// resolves issue where if the object property was a nullable number (int/double/etc.) and the value being processed
228+
// was null or an empty string an exception 'string was not in the correct format' occurred.
229+
// see https://github.com/squaredup/HubSpot.NET/pull/8
230+
if (isNullable && (dynamicValue?.ToString()).IsNullOrEmpty())
231+
{
232+
// if nullable and the value to convert is null or an empty string it should not be converted
233+
targetProp.SetValue(dto, null);
234+
}
235+
else
236+
{
237+
var value = dynamicValue.GetType() == type ? dynamicValue : Convert.ChangeType(dynamicValue, type);
238+
targetProp.SetValue(dto, value);
239+
}
228240
}
229241
}
230242
return dto;

0 commit comments

Comments
 (0)