Skip to content

Commit e205bb9

Browse files
author
Daniel Low
committed
Correctly handle empty response bodies.
The thrift representation of an empty array is the same as null, so when the body is empty (such as with a HeadSync), the current implementation of Execute will throw an exception because MemoryStream cannot handle nulls. Instead use an empty byte array. Also add integration test for this case.
1 parent 9486a09 commit e205bb9

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Connections/Elasticsearch.Net.Connection.Thrift/ThriftConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,13 @@ private ElasticsearchResponse<Stream> Execute(RestRequest restRequest, object de
274274
if (result.Status == Status.OK || result.Status == Status.CREATED || result.Status == Status.ACCEPTED)
275275
{
276276
var response = ElasticsearchResponse<Stream>.Create(
277-
this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body));
277+
this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body ?? new byte[0]));
278278
return response;
279279
}
280280
else
281281
{
282282
var response = ElasticsearchResponse<Stream>.Create(
283-
this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body));
283+
this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body ?? new byte[0]));
284284
return response;
285285
}
286286
}

src/Tests/Nest.Tests.Integration/Connection/Thrift/ThiftBugReportTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,16 @@ public void IndexExistShouldNotThrowOn404()
2626
unknownIndexResult.ConnectionStatus.HttpStatusCode.Should().Be(404);
2727

2828
}
29+
30+
[Test]
31+
public void EmptyResponseShouldNotThrowError()
32+
{
33+
var isValidThriftConnection = this._thriftClient.RootNodeInfo().IsValid;
34+
isValidThriftConnection.Should().BeTrue();
35+
36+
var result = this._thriftClient.Connection.HeadSync(ElasticsearchConfiguration.CreateBaseUri(9500));
37+
result.Success.Should().BeTrue();
38+
result.OriginalException.Should().BeNull();
39+
}
2940
}
3041
}

0 commit comments

Comments
 (0)