-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
KenticoCloud.ContentManagement.Tests/Modules/ActionInvoker/ActionInvokerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using KenticoCloud.ContentManagement.Models.Items; | ||
using KenticoCloud.ContentManagement.Modules.ActionInvoker; | ||
using KenticoCloud.ContentManagement.Modules.HttpClient; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace KenticoCloud.ContentManagement.Tests | ||
{ | ||
internal class FakeContentManagementHttpClient : IContentManagementHttpClient | ||
{ | ||
internal string requestBody; | ||
|
||
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage message) | ||
{ | ||
requestBody = await message.Content.ReadAsStringAsync(); | ||
return new HttpResponseMessage(HttpStatusCode.Accepted); | ||
} | ||
} | ||
|
||
public class ActionInvokerTests | ||
{ | ||
[Theory] | ||
[Trait("Issue", "29")] | ||
[InlineData(0.0, "0")] | ||
[InlineData(29.0, "29.0")] | ||
public void ActionInvokerSerializeElementContainingZero_SerializedJsonContainsZero(decimal d, string s) | ||
{ | ||
var httpClient = new FakeContentManagementHttpClient(); | ||
var actionInvoker = new ActionInvoker(httpClient, new MessageCreator("{api_key}")); | ||
|
||
var contentItemVariantUpsertModel = new ContentItemVariantUpsertModel() | ||
{ | ||
Elements = new | ||
{ | ||
zero = d, | ||
optZero = new decimal?(d), | ||
}, | ||
}; | ||
|
||
var result = actionInvoker.InvokeMethodAsync<ContentItemVariantUpsertModel, dynamic>("{endpoint_url}", HttpMethod.Get, contentItemVariantUpsertModel); | ||
Assert.Equal($"{{\"elements\":{{\"zero\":{s},\"optZero\":{s}}}}}", httpClient.requestBody); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
KenticoCloud.ContentManagement/Modules/ActionInvoker/DecimalObjectConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
|
||
namespace KenticoCloud.ContentManagement.Modules.ActionInvoker | ||
{ | ||
internal class DecimalObjectConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
=> objectType == typeof(decimal) || objectType == typeof(decimal?); | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
=> throw new NotImplementedException($"{nameof(DecimalObjectConverter)} class should be only used for serialization."); | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
switch (value) | ||
{ | ||
case decimal dec when dec == 0: | ||
// Serialize decimal(0) to 0 instead of 0.0 | ||
// NOTE: See issue #29 | ||
JToken.FromObject(0).WriteTo(writer); | ||
break; | ||
|
||
default: | ||
JToken.FromObject(value).WriteTo(writer); | ||
break; | ||
} | ||
} | ||
} | ||
} |