diff --git a/src/Nest/Resolvers/Converters/DictionaryKeysAreNotPropertyNamesJsonConverter.cs b/src/Nest/Resolvers/Converters/DictionaryKeysAreNotPropertyNamesJsonConverter.cs index 12a27fa2c56..8455c4ab785 100644 --- a/src/Nest/Resolvers/Converters/DictionaryKeysAreNotPropertyNamesJsonConverter.cs +++ b/src/Nest/Resolvers/Converters/DictionaryKeysAreNotPropertyNamesJsonConverter.cs @@ -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; diff --git a/src/Tests/Nest.Tests.Unit/Internals/Serialize/NullValueHandlingTests.cs b/src/Tests/Nest.Tests.Unit/Internals/Serialize/NullValueHandlingTests.cs new file mode 100644 index 00000000000..a7481e7c20e --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Internals/Serialize/NullValueHandlingTests.cs @@ -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 + { + { "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 + { + { "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; + } + } +} diff --git a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj index 59fe476d94f..79e53e1c734 100644 --- a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj +++ b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj @@ -237,6 +237,7 @@ +