Skip to content

Commit 2529f4b

Browse files
committed
fix #1800 integration tests for PostData<>
- sending in IEnumerable<object> should always serialize with no formatting - make sure that PostData<T> can deal with boxed types.
1 parent dbc147d commit 2529f4b

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

src/Elasticsearch.Net/Transport/PostData.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,30 @@ public class PostData<T> : IPostData<T>
3535
public PostData(string item) { _literalString = item; Type = PostType.LiteralString; }
3636
public PostData(IEnumerable<string> item) { _enumurabeOfStrings = item; Type = PostType.EnumerableOfString; }
3737
public PostData(IEnumerable<object> item) { _enumerableOfObject = item; Type = PostType.EnumerableOfObject; }
38-
public PostData(T item) { _serializable = item; Type = PostType.Serializable; }
38+
public PostData(T item)
39+
{
40+
var boxedType = item.GetType();
41+
if (typeof(byte[]).IsAssignableFrom(boxedType))
42+
{
43+
WrittenBytes = item as byte[]; Type = PostType.ByteArray;
44+
}
45+
else if (typeof(string).IsAssignableFrom(boxedType))
46+
{
47+
_literalString = item as string; Type = PostType.LiteralString;
48+
}
49+
else if (typeof(IEnumerable<string>).IsAssignableFrom(boxedType))
50+
{
51+
_enumurabeOfStrings = (IEnumerable<string>)item; Type = PostType.EnumerableOfString;
52+
}
53+
else if (typeof(IEnumerable<object>).IsAssignableFrom(boxedType))
54+
{
55+
_enumerableOfObject = (IEnumerable<object>)item; Type = PostType.EnumerableOfObject;
56+
}
57+
else
58+
{
59+
_serializable = item; Type = PostType.Serializable;
60+
}
61+
}
3962

4063
public void Write(Stream writableStream, IConnectionConfigurationValues settings)
4164
{
@@ -63,7 +86,7 @@ public void Write(Stream writableStream, IConnectionConfigurationValues settings
6386
else stream = writableStream;
6487
foreach (var o in _enumerableOfObject)
6588
{
66-
settings.Serializer.Serialize(o, stream, indent);
89+
settings.Serializer.Serialize(o, stream, SerializationFormatting.None);
6790
stream.Write(new byte[] { (byte)'\n' }, 0, 1);
6891
}
6992
break;

src/Tests/ClientConcepts/LowLevel/PostData.doc.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,6 @@ private static byte[] Utf8Bytes(string s)
152152
return string.IsNullOrEmpty(s) ? null : Encoding.UTF8.GetBytes(s);
153153
}
154154
private PostData<object> ImplicitlyConvertsFrom(PostData<object> postData) => postData;
155+
155156
}
156157
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Elasticsearch.Net;
7+
using FluentAssertions;
8+
using Nest;
9+
using Newtonsoft.Json.Linq;
10+
using Tests.Framework;
11+
using Tests.Framework.Integration;
12+
using Tests.Framework.MockData;
13+
using Xunit;
14+
15+
namespace Tests.Search.MultiSearch
16+
{
17+
[Collection(IntegrationContext.ReadOnly)]
18+
public class MultiSearchLowLevelPostDataTests
19+
{
20+
private IElasticClient _client;
21+
22+
public MultiSearchLowLevelPostDataTests(ReadOnlyCluster cluster, EndpointUsage usage)
23+
{
24+
_client = cluster.Client();
25+
}
26+
27+
protected List<object> Search => new object[]
28+
{
29+
new {},
30+
new { from = 0, size = 10, query = new { match_all = new {} } },
31+
new { search_type = "count" },
32+
new {},
33+
new { index = "devs", type = "developer" },
34+
new { from = 0, size = 5, query = new { match_all = new {} } },
35+
new { index = "devs", type = "developer" },
36+
new { from = 0, size = 5, query = new { match_all = new {} } }
37+
}.ToList();
38+
39+
40+
[I] public void PostEnumerableOfObjects()
41+
{
42+
var response = this._client.Raw.Msearch<dynamic>("project", "project", this.Search);
43+
AssertResponse(response);
44+
response = this._client.Raw.Msearch<dynamic>("project", "project", (object)this.Search);
45+
AssertResponse(response);
46+
}
47+
48+
[I] public void PostEnumerableOfStrings()
49+
{
50+
var listOfStrings = Search
51+
.Select(s => this._client.Serializer.SerializeToString(s, SerializationFormatting.None))
52+
.ToList();
53+
54+
var response = this._client.Raw.Msearch<dynamic>("project", "project", listOfStrings);
55+
AssertResponse(response);
56+
response = this._client.Raw.Msearch<dynamic>("project", "project", (object)listOfStrings);
57+
AssertResponse(response);
58+
}
59+
60+
[I] public void PostString()
61+
{
62+
var str = Search
63+
.Select(s => this._client.Serializer.SerializeToString(s, SerializationFormatting.None))
64+
.ToList()
65+
.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s + "\n"), sb => sb.ToString());
66+
67+
var response = this._client.Raw.Msearch<dynamic>("project", "project", str);
68+
AssertResponse(response);
69+
response = this._client.Raw.Msearch<dynamic>("project", "project", (object)str);
70+
AssertResponse(response);
71+
}
72+
73+
[I] public void PostByteArray()
74+
{
75+
var str = Search
76+
.Select(s => this._client.Serializer.SerializeToString(s, SerializationFormatting.None))
77+
.ToList()
78+
.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s + "\n"), sb => sb.ToString());
79+
80+
var bytes = Encoding.UTF8.GetBytes(str);
81+
82+
var response = this._client.Raw.Msearch<dynamic>("project", "project", bytes);
83+
AssertResponse(response);
84+
response = this._client.Raw.Msearch<dynamic>("project", "project", (object)bytes);
85+
AssertResponse(response);
86+
}
87+
88+
public void AssertResponse(ElasticsearchResponse<dynamic> response)
89+
{
90+
response.Success.Should().BeTrue();
91+
92+
var r = response.Body;
93+
94+
JArray responses = r.responses;
95+
96+
responses.Count().Should().Be(4);
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)