Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary serialization tests #3878

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,29 @@ public void Serialize(ref JsonWriter writer, Exception value, IJsonFormatterReso
}

var flattenedExceptions = FlattenExceptions(value);
var formatter = formatterResolver.GetFormatter<List<Dictionary<string, object>>>();
formatter.Serialize(ref writer, flattenedExceptions, formatterResolver);
var valueFormatter = formatterResolver.GetFormatter<object>();

writer.WriteBeginArray();
for (int i = 0; i < flattenedExceptions.Count; i++)
{
if (i > 0)
writer.WriteValueSeparator();

var flattenedException = flattenedExceptions[i];
writer.WriteBeginObject();
var count = 0;
foreach (var kv in flattenedException)
{
if (count > 0)
writer.WriteValueSeparator();

writer.WritePropertyName(kv.Key);
valueFormatter.Serialize(ref writer, kv.Value, formatterResolver);
count++;
}
writer.WriteEndObject();
}
writer.WriteEndArray();
}

public Exception Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
Expand Down
57 changes: 40 additions & 17 deletions src/Elasticsearch.Net/Utf8Json/Formatters/DictionaryFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal abstract class DictionaryFormatterBase<TKey, TValue, TIntermediate, TEn
where TDictionary : class, IEnumerable<KeyValuePair<TKey, TValue>>
where TEnumerator : IEnumerator<KeyValuePair<TKey, TValue>>
{
protected bool SkipValue(TValue value) => value == null;

public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterResolver formatterResolver)
{
if (value == null)
Expand All @@ -51,14 +53,20 @@ public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterRe
var e = GetSourceEnumerator(value);
try
{
var written = 0;
if (keyFormatter != null)
{
if (e.MoveNext())
{
var item = e.Current;
keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);

if (!SkipValue(item.Value))
{
keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
written++;
}
}
else
{
Expand All @@ -67,21 +75,31 @@ public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterRe

while (e.MoveNext())
{
writer.WriteValueSeparator();

var item = e.Current;
keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
if (!SkipValue(item.Value))
{
if (written > 0)
writer.WriteValueSeparator();
keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
written++;
}
}
}
else
{
if (e.MoveNext())
{
var item = e.Current;
writer.WriteString(item.Key.ToString());
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
if (!SkipValue(item.Value))
{
written++;
writer.WriteString(item.Key.ToString());
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
}
}
else
{
Expand All @@ -90,11 +108,17 @@ public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterRe

while (e.MoveNext())
{
writer.WriteValueSeparator();
var item = e.Current;
writer.WriteString(item.Key.ToString());
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
var item = e.Current;
if (!SkipValue(item.Value))
{
if (written > 0)
writer.WriteValueSeparator();

writer.WriteString(item.Key.ToString());
writer.WriteNameSeparator();
valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
written++;
}
}
}
}
Expand Down Expand Up @@ -164,8 +188,7 @@ protected override TDictionary Complete(ref TDictionary intermediateCollection)
return intermediateCollection;
}
}



internal sealed class DictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, Dictionary<TKey, TValue>.Enumerator, Dictionary<TKey, TValue>>
{
protected override void Add(ref Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
Expand Down
21 changes: 17 additions & 4 deletions src/Nest/Aggregations/VerbatimDictionaryKeysFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,25 @@ public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterRe
seenEntries[key] = entry.Value;
}

var formatter = formatterResolver.GetFormatter<Dictionary<string, TValue>>();
formatter.Serialize(ref writer, seenEntries, formatterResolver);
writer.WriteBeginObject();
if (seenEntries.Count > 0)
{
var valueFormatter = formatterResolver.GetFormatter<TValue>();
var count = 0;
foreach (var entry in seenEntries)
{
if (count > 0)
writer.WriteValueSeparator();

writer.WritePropertyName(entry.Key);
valueFormatter.Serialize(ref writer, entry.Value, formatterResolver);
count++;
}
}
writer.WriteEndObject();
}

protected virtual bool SkipValue(KeyValuePair<TKey, TValue> entry) =>
entry.Value == null; //TODO: Check connection settings for allow nulls
protected virtual bool SkipValue(KeyValuePair<TKey, TValue> entry) => entry.Value == null;
}

internal class VerbatimInterfaceReadOnlyDictionaryKeysPreservingNullFormatter<TKey, TValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Core.Client;
using Tests.Core.Serialization;

namespace Tests.Framework.SerializationTests
{
/// <summary>
/// <see cref="IIsADictionary"/> implementations do not use
/// <see cref="VerbatimDictionaryKeysFormatter{TKey,TValue}"/>
/// so keys are camel cased on serialization, in line with NEST conventions
/// </summary>
public class IsADictionarySerializationTests
{
protected object ExpectJson => new { key1 = "value1", key2 = "value2", };

// IIsADictionary implementations do not use the VerbatimDictionaryKeysConverter
// so keys are camel cased on serialization, in line with NEST conventions
[U]
public void CanSerializeIsADictionary()
{
Expand All @@ -26,6 +32,31 @@ public void CanSerializeIsADictionary()
SerializationTestHelper.Object(isADictionary).RoundTrips(ExpectJson);
}

[U]
public void SerializesIsADictionaryNullValues()
{
var isADictionary = new MyIsADictionary(new Dictionary<object, object>
{
{ "Key1", null },
{ "Key2", "value2" },
});

var client = new ElasticClient();
client.RequestResponseSerializer.SerializeToString(isADictionary).Should().Be("{\"key1\":null,\"key2\":\"value2\"}");
}

[U]
public void SerializesIsADictionaryRespectsDefaultFieldNameInferrer()
{
var isADictionary = new MyIsADictionary(new Dictionary<object, object>
{
{ "Key1", "value1" }
});

var client = new ElasticClient(new ConnectionSettings().DefaultFieldNameInferrer(f => f.ToUpperInvariant()));
client.RequestResponseSerializer.SerializeToString(isADictionary).Should().Be("{\"KEY1\":\"value1\"}");
}

private class MyIsADictionary : IsADictionaryBase<object, object>
{
public MyIsADictionary(IDictionary<object, object> backingDictionary) : base(backingDictionary) { }
Expand Down Expand Up @@ -60,6 +91,19 @@ public void CanSerializeGenericDictionary()
SerializationTestHelper.Object(dictionary).RoundTrips(ExpectJson);
}

[U]
public void CanSerializeIgnoresDefaultFieldNameInferrer()
{
var dictionary = new Dictionary<string, string>
{
{ "Key1", "value1" },
{ "Key2", "value2" },
};

var client = new ElasticClient(new ConnectionSettings().DefaultFieldNameInferrer(f => f.ToUpperInvariant()));
client.RequestResponseSerializer.SerializeToString(dictionary).Should().Be("{\"Key1\":\"value1\",\"Key2\":\"value2\"}");
}

[U]
public void CanSerializeGenericReadOnlyDictionary()
{
Expand Down Expand Up @@ -121,6 +165,54 @@ public void CanSerializeHashTable()
SerializationTestHelper.Object(hashTable).RoundTrips(ExpectJson);
}

[U]
public void DoesNotSerializeDictionaryNullValues()
{
var dictionary = new Dictionary<string, string>
{
{ "Key1", null },
{ "Key2", "value2" },
};

SerializationTestHelper.Object(dictionary).RoundTrips(new { Key2 = "value2" });
}

[U]
public void DoesNotSerializeIDictionaryNullValues()
{
IDictionary<string, string> dictionary = new Dictionary<string, string>
{
{ "Key1", null },
{ "Key2", "value2" },
};

SerializationTestHelper.Object(dictionary).RoundTrips(new { Key2 = "value2" });
}

[U]
public void DoesNotSerializeReadOnlyDictionaryNullValues()
{
var dictionary = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{ "Key1", null },
{ "Key2", "value2" },
});

SerializationTestHelper.Object(dictionary).RoundTrips(new { Key2 = "value2" });
}

[U]
public void DoesNotSerializeIReadOnlyDictionaryNullValues()
{
IReadOnlyDictionary<string, string> dictionary = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{ "Key1", null },
{ "Key2", "value2" },
});

SerializationTestHelper.Object(dictionary).RoundTrips(new { Key2 = "value2" });
}

private class MyDictionary : IDictionary
{
private readonly IDictionary _dictionary = new Hashtable();
Expand Down