Skip to content

Commit ed272e0

Browse files
committed
Fix #2054: Throw an exception if null is passed to Bulk() and fix NRE in DebugInformation
1 parent 458e9d1 commit ed272e0

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/Nest/CommonAbstractions/Response/ResponseBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ public string DebugInformation
4343
get
4444
{
4545
var sb = new StringBuilder();
46-
sb.Append($"{(!IsValid ? "Inv" : "V")}alid NEST response built from a ");
47-
sb.AppendLine(ApiCall?.ToString().ToCamelCase() ?? "null ApiCall which is highly exceptional, please open a bug if you see this");
46+
sb.Append($"{(!this.IsValid ? "Inv" : "V")}alid NEST response built from a ");
47+
sb.AppendLine(this.ApiCall?.ToString().ToCamelCase() ?? "null ApiCall which is highly exceptional, please open a bug if you see this");
4848
if (!this.IsValid) this.DebugIsValid(sb);
49-
ResponseStatics.DebugInformationBuilder(ApiCall, sb);
49+
if (this.ApiCall != null) ResponseStatics.DebugInformationBuilder(ApiCall, sb);
5050
return sb.ToString();
5151
}
5252
}
5353
protected virtual void DebugIsValid(StringBuilder sb) { }
5454

55-
public override string ToString() => $"{(!IsValid ? "Inv" : "V")}alid NEST response built from a {this.ApiCall?.ToString().ToCamelCase()}";
55+
public override string ToString() => $"{(!this.IsValid ? "Inv" : "V")}alid NEST response built from a {this.ApiCall?.ToString().ToCamelCase()}";
5656

5757
}
5858
}

src/Nest/Document/Multiple/Bulk/BulkResponse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class BulkResponse : ResponseBase, IBulkResponse
1919
public override bool IsValid => base.IsValid && !this.Errors && !this.ItemsWithErrors.HasAny();
2020
protected override void DebugIsValid(StringBuilder sb)
2121
{
22+
if (this.Items == null) return;
2223
sb.AppendLine($"# Invalid Bulk items:");
2324
foreach(var i in Items.Select((item, i) => new { item, i}).Where(i=>!i.item.IsValid))
2425
sb.AppendLine($" operation[{i.i}]: {i.item}");

src/Nest/Document/Multiple/Bulk/ElasticClient-Bulk.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Nest
77
public partial interface IElasticClient
88
{
99
/// <summary>
10-
/// The bulk API makes it possible to perform many index/delete operations in a single API call.
10+
/// The bulk API makes it possible to perform many index/delete operations in a single API call.
1111
/// This can greatly increase the indexing speed.
1212
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
1313
/// </summary>
@@ -28,23 +28,33 @@ public partial interface IElasticClient
2828
public partial class ElasticClient
2929
{
3030
/// <inheritdoc/>
31-
public IBulkResponse Bulk(IBulkRequest request) =>
31+
public IBulkResponse Bulk(IBulkRequest request) =>
3232
this.Dispatcher.Dispatch<IBulkRequest, BulkRequestParameters, BulkResponse>(
3333
request, this.LowLevelDispatch.BulkDispatch<BulkResponse>
3434
);
3535

3636
/// <inheritdoc/>
37-
public IBulkResponse Bulk(Func<BulkDescriptor, IBulkRequest> selector = null) =>
38-
this.Bulk(selector.InvokeOrDefault(new BulkDescriptor()));
37+
public IBulkResponse Bulk(Func<BulkDescriptor, IBulkRequest> selector = null)
38+
{
39+
// selector should not be nullable, but we can't change it for backwards compatibility reasons
40+
if (selector == null)
41+
throw new ArgumentNullException(nameof(selector));
42+
return this.Bulk(selector.InvokeOrDefault(new BulkDescriptor()));
43+
}
3944

4045
/// <inheritdoc/>
41-
public Task<IBulkResponse> BulkAsync(IBulkRequest request) =>
46+
public Task<IBulkResponse> BulkAsync(IBulkRequest request) =>
4247
this.Dispatcher.DispatchAsync<IBulkRequest, BulkRequestParameters, BulkResponse, IBulkResponse>(
4348
request, this.LowLevelDispatch.BulkDispatchAsync<BulkResponse>
4449
);
4550

4651
/// <inheritdoc/>
47-
public Task<IBulkResponse> BulkAsync(Func<BulkDescriptor, IBulkRequest> selector = null) =>
48-
this.BulkAsync(selector.InvokeOrDefault(new BulkDescriptor()));
52+
public Task<IBulkResponse> BulkAsync(Func<BulkDescriptor, IBulkRequest> selector = null)
53+
{
54+
// selector should not be nullable, but we can't change it for backwards compatibility reasons
55+
if (selector == null)
56+
throw new ArgumentNullException(nameof(selector));
57+
return this.BulkAsync(selector.InvokeOrDefault(new BulkDescriptor()));
58+
}
4959
}
50-
}
60+
}

0 commit comments

Comments
 (0)