Skip to content

Commit cf1f330

Browse files
committed
Merge branch 'fix/1.x-jobject' into 1.x
2 parents aef45fc + f5cb12c commit cf1f330

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandlerBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ protected byte[] PostData(object data)
6464
return (string.Join("\n", ss) + "\n").Utf8Bytes();
6565
}
6666

67-
if (typeof(IEnumerable<object>).IsAssignableFrom(dataType))
67+
// JObject should not be treated as an IEnumerable<object> as doing so
68+
// results in malformed json in the request. Allow it to be handled
69+
// by the serializer similar to any other type.
70+
if (typeof(IEnumerable<object>).IsAssignableFrom(dataType) &&
71+
dataType.FullName != "Newtonsoft.Json.Linq.JObject")
6872
{
6973
var so = (IEnumerable<object>)data;
7074
var joined = string.Join("\n", so
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using Newtonsoft.Json.Linq;
6+
using NUnit.Framework;
7+
8+
namespace Nest.Tests.Integration.Index
9+
{
10+
[TestFixture]
11+
public class IndexJObjectTests : IntegrationTests
12+
{
13+
private IEnumerable<JObject> _jobjects;
14+
15+
public IEnumerable<JObject> JObjects
16+
{
17+
get
18+
{
19+
if (_jobjects == null)
20+
{
21+
_jobjects = Enumerable.Range(1, 1000)
22+
.Select(i =>
23+
new JObject
24+
{
25+
{ "id", i },
26+
{ "name", $"name {i}" },
27+
{ "value", Math.Pow(i, 2) },
28+
{ "date", new DateTime(2016, 1, 1) },
29+
{
30+
"child", new JObject
31+
{
32+
{ "child_name", $"child_name {i}{i}" },
33+
{ "child_value", 3 }
34+
}
35+
}
36+
});
37+
}
38+
39+
return _jobjects;
40+
}
41+
}
42+
43+
[Test]
44+
public void Index()
45+
{
46+
var jObject = JObjects.First();
47+
48+
var indexResult = this.Client.Index(jObject, f => f
49+
.Id(jObject["id"].Value<int>())
50+
);
51+
52+
indexResult.IsValid.Should().BeTrue();
53+
indexResult.RequestInformation.HttpStatusCode.Should().Be(201);
54+
indexResult.Created.Should().BeTrue();
55+
indexResult.Type.Should().Be("jobject");
56+
}
57+
58+
[Test]
59+
public void Bulk()
60+
{
61+
var bulkResponse = this.Client.Bulk(b => b
62+
.IndexMany(JObjects.Skip(1), (bi, d) => bi
63+
.Document(d)
64+
.Id(d["id"].Value<int>())
65+
)
66+
);
67+
68+
bulkResponse.IsValid.Should().BeTrue();
69+
bulkResponse.RequestInformation.HttpStatusCode.Should().Be(200);
70+
71+
foreach (var response in bulkResponse.Items)
72+
{
73+
response.IsValid.Should().BeTrue();
74+
response.Status.Should().Be(201);
75+
}
76+
}
77+
}
78+
}

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<Compile Include="Failover\FailoverSniffOnStartupTests.cs" />
125125
<Compile Include="Failover\FailoverPingTests.cs" />
126126
<Compile Include="Help\AliasTests.cs" />
127+
<Compile Include="Index\IndexJObjectTests.cs" />
127128
<Compile Include="Index\ReindexTests.cs" />
128129
<Compile Include="Index\IndexUsingUrlIdTests.cs" />
129130
<Compile Include="Indices\Analysis\Analyzers\AnalyzerTest.cs" />

0 commit comments

Comments
 (0)