-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix null value handling on dictionaries
Null values within dictionaries were being ignored even with NullValueHandling set to Include on the serializer. Closes #1004
- Loading branch information
Showing
3 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
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
102 changes: 102 additions & 0 deletions
102
src/Tests/Nest.Tests.Unit/Internals/Serialize/NullValueHandlingTests.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,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; | ||
} | ||
} | ||
} |
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