Skip to content

Commit

Permalink
Merge branch 'Haaxor1689-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
petrsvihlik committed Oct 19, 2018
2 parents ded1797 + 5a4e90d commit 8c77b9e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using KenticoCloud.ContentManagement.Modules.HttpClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http.Headers;
using System.Collections.Generic;

using KenticoCloud.ContentManagement.Modules.HttpClient;

using Newtonsoft.Json;

namespace KenticoCloud.ContentManagement.Modules.ActionInvoker
{
Expand All @@ -17,8 +15,10 @@ internal class ActionInvoker : IActionInvoker
private IContentManagementHttpClient _cmHttpClient;
private MessageCreator _messageCreator;

private JsonSerializerSettings _serializeSettings = new JsonSerializerSettings {
private JsonSerializerSettings _serializeSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter> { new DecimalObjectConverter() }
};

private JsonSerializerSettings _deserializeSettings = new JsonSerializerSettings
Expand All @@ -32,7 +32,7 @@ public ActionInvoker(IContentManagementHttpClient cmHttpClient, MessageCreator m
_cmHttpClient = cmHttpClient;
_messageCreator = messageCreator;
}


private async Task<T> ReadResultAsync<T>(HttpResponseMessage response)
{
Expand Down
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;
}
}
}
}

0 comments on commit 8c77b9e

Please sign in to comment.