Skip to content

Commit

Permalink
Merge pull request #8 from bfutrell70/master
Browse files Browse the repository at this point in the history
Fixed issue converting an empty object to a nullable numeric property
  • Loading branch information
clarkd authored May 1, 2018
2 parents f4ca32e + 42d9ce2 commit c240c8c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
15 changes: 15 additions & 0 deletions HubSpot.NET.Examples/Deals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public static void Example()
*/
var deals = api.Deal.List<DealHubSpotModel>(false,
new ListRequestOptions { PropertiesToInclude = new List<string> { "dealname", "amount" } });

/**
* Get all deals
*/
//var moreResults = true;
//long offset = 0;
//while (moreResults)
//{
// var allDeals = api.Deal.List<DealHubSpotModel>(false,
// new ListRequestOptions { PropertiesToInclude = new List<string> { "dealname", "amount", "hubspot_owner_id", "closedate" }, Limit = 100, Offset = offset });

// moreResults = allDeals.MoreResultsAvailable;
// if (moreResults) offset = allDeals.ContinuationOffset;

//}
}
}
}
2 changes: 1 addition & 1 deletion HubSpot.NET/Api/Deal/Dto/DealHubSpotModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public DealHubSpotModel()
public string CloseDate { get; set; }

[DataMember(Name = "amount")]
public double Amount { get; set; }
public double? Amount { get; set; }

[DataMember(Name = "dealtype")]
public string DealType { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions HubSpot.NET/Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HubSpot.NET.Core.Extensions
{
public static class StringExtensions
{
public static bool IsNullOrEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
}
}
24 changes: 18 additions & 6 deletions HubSpot.NET/Core/Requests/RequestDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Dynamic;
using System.Linq;
using System.Reflection;
using HubSpot.NET.Core.Extensions;
using HubSpot.NET.Core.Interfaces;

namespace HubSpot.NET.Core.Requests
Expand Down Expand Up @@ -219,12 +220,23 @@ internal object ConvertSingleEntity(ExpandoObject dynamicObject, object dto)

if (targetProp != null)
{

Type t = Nullable.GetUnderlyingType(targetProp.PropertyType) ?? targetProp.PropertyType;

var value = dynamicValue.GetType() == t ? dynamicValue : Convert.ChangeType(dynamicValue, t);

targetProp.SetValue(dto, value);
var isNullable = targetProp.PropertyType.Name.Contains("Nullable");

var type = Nullable.GetUnderlyingType(targetProp.PropertyType) ?? targetProp.PropertyType;

// resolves issue where if the object property was a nullable number (int/double/etc.) and the value being processed
// was null or an empty string an exception 'string was not in the correct format' occurred.
// see https://github.com/squaredup/HubSpot.NET/pull/8
if (isNullable && (dynamicValue?.ToString()).IsNullOrEmpty())
{
// if nullable and the value to convert is null or an empty string it should not be converted
targetProp.SetValue(dto, null);
}
else
{
var value = dynamicValue.GetType() == type ? dynamicValue : Convert.ChangeType(dynamicValue, type);
targetProp.SetValue(dto, value);
}
}
}
return dto;
Expand Down

0 comments on commit c240c8c

Please sign in to comment.