Skip to content

Commit

Permalink
Fix null value handling on dictionaries
Browse files Browse the repository at this point in the history
Null values within dictionaries were being ignored even with
NullValueHandling set to Include on the serializer.

Closes #1004
  • Loading branch information
gmarz committed Oct 20, 2014
1 parent aa181f8 commit 2789331
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s

foreach (DictionaryEntry entry in dictionary)
{
if (entry.Value == null)
if (entry.Value == null && serializer.NullValueHandling == NullValueHandling.Ignore)
continue;
string key;
var pp = entry.Key as PropertyPathMarker;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Elasticsearch.Net.Connection;
using Nest.Tests.MockData.Domain;
using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nest.Tests.Unit.Internals.Serialize
{
public class PartialDoc
{
public string Name { get; set; }
public DateTime? StartedOn { get; set; }
}

[TestFixture]
public class NullValueHandlingTests : BaseJsonTests
{
[Test]
public void IncludeNullValues_Dictionary_Test()
{
var client = GetTestClient(NullValueHandling.Include);

var doc = new Dictionary<string, object>
{
{ "name", "newname" },
{ "startedOn", null }
};

var expected = @"{ name: ""newname"", startedOn: null }";
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));

Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void IncludeNullValues_Object_Test()
{
var client = GetTestClient(NullValueHandling.Include);

var doc = new PartialDoc
{
Name = "newname",
StartedOn = null
};

var expected = @"{ name: ""newname"", startedOn: null }";
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));

Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void IgnoreNullValues_Dictionary_Test()
{
var client = GetTestClient(NullValueHandling.Ignore);

var doc = new Dictionary<string, object>
{
{ "name", "newname" },
{ "startedOn", null }
};

var expected = @"{ name: ""newname"" }";
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));

Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void IgnoreNullValues_Object_Test()
{
var client = GetTestClient(NullValueHandling.Ignore);

var doc = new PartialDoc
{
Name = "newname",
StartedOn = null
};

var expected = @"{ name: ""newname"" }";
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));

Assert.True(json.JsonEquals(expected), json);
}

private IElasticClient GetTestClient(NullValueHandling nullValueHandling)
{
var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
.SetJsonSerializerSettingsModifier(m => m
.NullValueHandling = nullValueHandling
);
var connection = new InMemoryConnection(settings);
var client = new ElasticClient(settings, connection);

return client;
}
}
}
1 change: 1 addition & 0 deletions src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
<Compile Include="Internals\Inferno\HostNameWithPathTests.cs" />
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
<Compile Include="Internals\Serialize\ConnectionSettingsTests.cs" />
<Compile Include="Internals\Serialize\NullValueHandlingTests.cs" />
<Compile Include="Internals\Serialize\OptOutTests.cs" />
<Compile Include="Internals\Serialize\CustomConvertersTests.cs" />
<Compile Include="ObjectInitializer\Aliases\GetAliasMoreUrlTests.cs" />
Expand Down

0 comments on commit 2789331

Please sign in to comment.