Skip to content

Commit b8980ca

Browse files
authored
Fix null ref DynamicResponse when host not available (#4236)
When using the low levelclient and asking for a DyamicResponse while the server is unreachable you would get a NRE Unhandled exception. Elasticsearch.Net.UnexpectedElasticsearchClientException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Elasticsearch.Net.ResponseBuilder.SetSpecialTypes[TResponse](String mimeType, Byte[] bytes, IMemoryStreamFactory memoryStreamFactory, TResponse& cs) in /home/mpdreamz/projects/elastic/net-7/src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs:line 169 This hit the sockets exception as can be seen after applying this fix: Unsuccessful () low level call on GET: / # Audit trail of this API call: - [1] BadRequest: Node: http://localhost:9200/ Took: 00:00:00.0657238 # OriginalException: System.Net.Http.HttpRequestException: Connection refused ---> System.Net.Sockets.SocketException (111): Connection refused at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace ---
1 parent 0d2c6ed commit b8980ca

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,17 @@ private static bool SetSpecialTypes<TResponse>(string mimeType, byte[] bytes, IM
166166
else if (responseType == typeof(DynamicResponse))
167167
{
168168
//if not json store the result under "body"
169-
if (!mimeType.StartsWith(RequestData.MimeType))
169+
if (mimeType == null || !mimeType.StartsWith(RequestData.MimeType))
170170
{
171171
var dictionary = new DynamicDictionary();
172172
dictionary["body"] = new DynamicValue(bytes.Utf8String());
173173
cs = new DynamicResponse(dictionary) as TResponse;
174174
}
175175
else
176176
{
177-
using (var ms = memoryStreamFactory.Create(bytes))
178-
{
179-
var body = LowLevelRequestResponseSerializer.Instance.Deserialize<DynamicDictionary>(ms);
180-
cs = new DynamicResponse(body) as TResponse;
181-
}
177+
using var ms = memoryStreamFactory.Create(bytes);
178+
var body = LowLevelRequestResponseSerializer.Instance.Deserialize<DynamicDictionary>(ms);
179+
cs = new DynamicResponse(body) as TResponse;
182180
}
183181
}
184182
return cs != null;

0 commit comments

Comments
 (0)