Skip to content

Commit 7725b03

Browse files
authored
Merge pull request #450 from commercetools/fix-asdecimal-deserializer
Fix NPE for AsDecimal extension methods
2 parents cd9dba9 + 9f08332 commit 7725b03

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

commercetools.Sdk/Tests/commercetools.Sdk.Api.Tests/CustomFieldsTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
4+
using System.Text.Json;
45
using commercetools.Sdk.Api.Models.Common;
56
using commercetools.Sdk.Api.Models.Products;
67
using commercetools.Sdk.Api.Models.Types;
@@ -12,6 +13,24 @@ namespace commercetools.Sdk.Api.Tests;
1213

1314
public class CustomFieldsTest
1415
{
16+
17+
[Fact]
18+
public void AsDecimalOrLong()
19+
{
20+
var fieldsStr = "{ \"number\": 91.62, \"numbers\": [91.62, 91.26, 91] }";
21+
IFieldContainer fields = JsonSerializer.Deserialize<FieldContainer>(fieldsStr);
22+
23+
Assert.Equal(91.62m, fields.AsDecimal("number").Value);
24+
Assert.Equal(92, fields.AsLong("number").Value);
25+
Assert.Equal(91.62m, fields.AsSetDecimal("numbers")[0]);
26+
Assert.Equal(92, fields.AsSetLong("numbers")[0]);
27+
Assert.Equal(91.26m, fields.AsSetDecimal("numbers")[1]);
28+
Assert.Equal(91, fields.AsSetLong("numbers")[1]);
29+
Assert.Equal(91.0m, fields.AsSetDecimal("numbers")[2]);
30+
Assert.Equal(91, fields.AsSetLong("numbers")[2]);
31+
32+
}
33+
1534
[Fact]
1635
public void fields()
1736
{

commercetools.Sdk/commercetools.Sdk.Api/Models/Types/IFieldContainer.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.Json;
45
using commercetools.Sdk.Api.Models.Common;
6+
using commercetools.Sdk.Api.Serialization;
57

68
namespace commercetools.Sdk.Api.Models.Types;
79

@@ -59,9 +61,13 @@ public object GetValue(string fieldName)
5961
public decimal? AsDecimal(string fieldName)
6062
{
6163
var value = GetValue(fieldName);
62-
if (GetValue(fieldName) is decimal decimalValue) { return decimalValue; }
63-
if (GetValue(fieldName) is long longValue) { return Convert.ToDecimal(longValue); }
64-
64+
if (value is decimal decimalValue) { return decimalValue; }
65+
if (value is long longValue) { return Convert.ToDecimal(longValue); }
66+
if (value is JsonElement { ValueKind: JsonValueKind.Number } element)
67+
{
68+
return element.GetDecimal();
69+
}
70+
6571
return null;
6672
}
6773

@@ -70,6 +76,10 @@ public object GetValue(string fieldName)
7076
var value = GetValue(fieldName);
7177
if (value is decimal decimalValue) { return Convert.ToInt64(decimalValue); }
7278
if (value is long longValue) { return longValue; }
79+
if (value is JsonElement { ValueKind: JsonValueKind.Number } element)
80+
{
81+
return Convert.ToInt64(element.GetDecimal());
82+
}
7383

7484
return null;
7585
}
@@ -132,6 +142,10 @@ public object GetValue(string fieldName)
132142
{
133143
return longs.Select(Convert.ToDecimal).ToList();
134144
}
145+
if (value is JsonElement { ValueKind: JsonValueKind.Array } elements && elements.GetFirstArrayElementValueKind() == JsonValueKind.Number)
146+
{
147+
return elements.EnumerateArray().Select(element => element.GetDecimal()).ToList();
148+
}
135149

136150
return null;
137151
}
@@ -144,6 +158,10 @@ public object GetValue(string fieldName)
144158
{
145159
return decimals.Select(Convert.ToInt64).ToList();
146160
}
161+
if (value is JsonElement { ValueKind: JsonValueKind.Array } elements && elements.GetFirstArrayElementValueKind() == JsonValueKind.Number)
162+
{
163+
return elements.EnumerateArray().Select(element => Convert.ToInt64(element.GetDecimal())).ToList();
164+
}
147165

148166
return null;
149167
}

0 commit comments

Comments
 (0)