Skip to content

Commit a39ef5a

Browse files
committed
try to reproduce #3538 to no avail
1 parent 510865f commit a39ef5a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using System.Threading;
5+
using Elastic.Xunit.XunitPlumbing;
6+
using FluentAssertions;
7+
using Nest;
8+
using Nest.JsonNetSerializer;
9+
using Newtonsoft.Json;
10+
using Tests.Core.Client.Settings;
11+
12+
namespace Tests.Reproduce
13+
{
14+
public class GithubIssue3538
15+
{
16+
[U] public void EmptyPolicyCausesNullReferenceException()
17+
{
18+
var converter = new MyPocoJsonConverter();
19+
var connectionSettings = new AlwaysInMemoryConnectionSettings(
20+
sourceSerializerFactory: (builtin, settings) => new JsonNetSerializer(builtin, settings,
21+
() =>
22+
{
23+
var jSettings = new JsonSerializerSettings();
24+
jSettings.Converters.Add(converter);
25+
return jSettings;
26+
}))
27+
.DefaultIndex("blah");
28+
29+
var client = new ElasticClient(connectionSettings);
30+
31+
var result = client.IndexDocument(new MyPoco() { Id = 1, Name = "x" });
32+
converter.CanConvertCallCount.Should().Be(1);
33+
34+
client.Bulk(b => b.Index<MyPoco>(i=>i.Document(new MyPoco { Id = 2, Name = "y" })));
35+
converter.CanConvertCallCount.Should().Be(2);
36+
37+
client.Bulk(b => b.Update<MyPoco>(i=>i.Doc(new MyPoco { Id = 3, Name = "z" })));
38+
converter.CanConvertCallCount.Should().Be(3);
39+
40+
client.Bulk(b => b.Update<MyPoco>(i=>i.Upsert(new MyPoco { Id = 4, Name = "a" })));
41+
converter.CanConvertCallCount.Should().Be(4);
42+
43+
converter.WriteCount.Should().Be(4);
44+
converter.SeenIds.Should().BeEquivalentTo(1, 2, 3, 4);
45+
46+
}
47+
48+
public class MyPoco
49+
{
50+
public int Id { get; set; }
51+
public string Name { get; set; }
52+
}
53+
54+
public class MyPocoJsonConverter : JsonConverter
55+
{
56+
private int _canConvertCallCount;
57+
public int CanConvertCallCount => _canConvertCallCount;
58+
59+
private int _writeCount;
60+
public int WriteCount => _writeCount;
61+
62+
public List<int> SeenIds { get; } = new List<int>();
63+
64+
65+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
66+
{
67+
Interlocked.Increment(ref _writeCount);
68+
69+
if (!(value is MyPoco poco))
70+
{
71+
writer.WriteNull();
72+
return;
73+
}
74+
75+
writer.WriteStartObject();
76+
writer.WritePropertyName("id");
77+
writer.WriteValue(poco.Id);
78+
SeenIds.Add(poco.Id);
79+
writer.WritePropertyName("name");
80+
writer.WriteValue(poco.Name);
81+
writer.WriteEndObject();
82+
}
83+
84+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
85+
new MyPoco { };
86+
87+
public override bool CanConvert(Type objectType)
88+
{
89+
Interlocked.Increment(ref _canConvertCallCount);
90+
91+
return objectType == typeof(MyPoco);
92+
}
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)