Skip to content

Commit 2789331

Browse files
committed
Fix null value handling on dictionaries
Null values within dictionaries were being ignored even with NullValueHandling set to Include on the serializer. Closes #1004
1 parent aa181f8 commit 2789331

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

Diff for: src/Nest/Resolvers/Converters/DictionaryKeysAreNotPropertyNamesJsonConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
3636

3737
foreach (DictionaryEntry entry in dictionary)
3838
{
39-
if (entry.Value == null)
39+
if (entry.Value == null && serializer.NullValueHandling == NullValueHandling.Ignore)
4040
continue;
4141
string key;
4242
var pp = entry.Key as PropertyPathMarker;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Elasticsearch.Net.Connection;
2+
using Nest.Tests.MockData.Domain;
3+
using Newtonsoft.Json;
4+
using NUnit.Framework;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Nest.Tests.Unit.Internals.Serialize
12+
{
13+
public class PartialDoc
14+
{
15+
public string Name { get; set; }
16+
public DateTime? StartedOn { get; set; }
17+
}
18+
19+
[TestFixture]
20+
public class NullValueHandlingTests : BaseJsonTests
21+
{
22+
[Test]
23+
public void IncludeNullValues_Dictionary_Test()
24+
{
25+
var client = GetTestClient(NullValueHandling.Include);
26+
27+
var doc = new Dictionary<string, object>
28+
{
29+
{ "name", "newname" },
30+
{ "startedOn", null }
31+
};
32+
33+
var expected = @"{ name: ""newname"", startedOn: null }";
34+
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));
35+
36+
Assert.True(json.JsonEquals(expected), json);
37+
}
38+
39+
[Test]
40+
public void IncludeNullValues_Object_Test()
41+
{
42+
var client = GetTestClient(NullValueHandling.Include);
43+
44+
var doc = new PartialDoc
45+
{
46+
Name = "newname",
47+
StartedOn = null
48+
};
49+
50+
var expected = @"{ name: ""newname"", startedOn: null }";
51+
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));
52+
53+
Assert.True(json.JsonEquals(expected), json);
54+
}
55+
56+
[Test]
57+
public void IgnoreNullValues_Dictionary_Test()
58+
{
59+
var client = GetTestClient(NullValueHandling.Ignore);
60+
61+
var doc = new Dictionary<string, object>
62+
{
63+
{ "name", "newname" },
64+
{ "startedOn", null }
65+
};
66+
67+
var expected = @"{ name: ""newname"" }";
68+
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));
69+
70+
Assert.True(json.JsonEquals(expected), json);
71+
}
72+
73+
[Test]
74+
public void IgnoreNullValues_Object_Test()
75+
{
76+
var client = GetTestClient(NullValueHandling.Ignore);
77+
78+
var doc = new PartialDoc
79+
{
80+
Name = "newname",
81+
StartedOn = null
82+
};
83+
84+
var expected = @"{ name: ""newname"" }";
85+
var json = Encoding.UTF8.GetString(client.Serializer.Serialize(doc));
86+
87+
Assert.True(json.JsonEquals(expected), json);
88+
}
89+
90+
private IElasticClient GetTestClient(NullValueHandling nullValueHandling)
91+
{
92+
var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
93+
.SetJsonSerializerSettingsModifier(m => m
94+
.NullValueHandling = nullValueHandling
95+
);
96+
var connection = new InMemoryConnection(settings);
97+
var client = new ElasticClient(settings, connection);
98+
99+
return client;
100+
}
101+
}
102+
}

Diff for: src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<Compile Include="Internals\Inferno\HostNameWithPathTests.cs" />
238238
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
239239
<Compile Include="Internals\Serialize\ConnectionSettingsTests.cs" />
240+
<Compile Include="Internals\Serialize\NullValueHandlingTests.cs" />
240241
<Compile Include="Internals\Serialize\OptOutTests.cs" />
241242
<Compile Include="Internals\Serialize\CustomConvertersTests.cs" />
242243
<Compile Include="ObjectInitializer\Aliases\GetAliasMoreUrlTests.cs" />

0 commit comments

Comments
 (0)