From ed272e05bc5b526e5aadb7ded4f31e665a37fe61 Mon Sep 17 00:00:00 2001 From: gmarz Date: Thu, 21 Apr 2016 10:18:43 -0400 Subject: [PATCH] Fix #2054: Throw an exception if null is passed to Bulk() and fix NRE in DebugInformation --- .../Response/ResponseBase.cs | 8 +++--- .../Document/Multiple/Bulk/BulkResponse.cs | 1 + .../Multiple/Bulk/ElasticClient-Bulk.cs | 26 +++++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Nest/CommonAbstractions/Response/ResponseBase.cs b/src/Nest/CommonAbstractions/Response/ResponseBase.cs index 5bbb755c7fe..af309a13e07 100644 --- a/src/Nest/CommonAbstractions/Response/ResponseBase.cs +++ b/src/Nest/CommonAbstractions/Response/ResponseBase.cs @@ -43,16 +43,16 @@ public string DebugInformation get { var sb = new StringBuilder(); - sb.Append($"{(!IsValid ? "Inv" : "V")}alid NEST response built from a "); - sb.AppendLine(ApiCall?.ToString().ToCamelCase() ?? "null ApiCall which is highly exceptional, please open a bug if you see this"); + sb.Append($"{(!this.IsValid ? "Inv" : "V")}alid NEST response built from a "); + sb.AppendLine(this.ApiCall?.ToString().ToCamelCase() ?? "null ApiCall which is highly exceptional, please open a bug if you see this"); if (!this.IsValid) this.DebugIsValid(sb); - ResponseStatics.DebugInformationBuilder(ApiCall, sb); + if (this.ApiCall != null) ResponseStatics.DebugInformationBuilder(ApiCall, sb); return sb.ToString(); } } protected virtual void DebugIsValid(StringBuilder sb) { } - public override string ToString() => $"{(!IsValid ? "Inv" : "V")}alid NEST response built from a {this.ApiCall?.ToString().ToCamelCase()}"; + public override string ToString() => $"{(!this.IsValid ? "Inv" : "V")}alid NEST response built from a {this.ApiCall?.ToString().ToCamelCase()}"; } } diff --git a/src/Nest/Document/Multiple/Bulk/BulkResponse.cs b/src/Nest/Document/Multiple/Bulk/BulkResponse.cs index 773ecde2352..4b11d87a1b7 100644 --- a/src/Nest/Document/Multiple/Bulk/BulkResponse.cs +++ b/src/Nest/Document/Multiple/Bulk/BulkResponse.cs @@ -19,6 +19,7 @@ public class BulkResponse : ResponseBase, IBulkResponse public override bool IsValid => base.IsValid && !this.Errors && !this.ItemsWithErrors.HasAny(); protected override void DebugIsValid(StringBuilder sb) { + if (this.Items == null) return; sb.AppendLine($"# Invalid Bulk items:"); foreach(var i in Items.Select((item, i) => new { item, i}).Where(i=>!i.item.IsValid)) sb.AppendLine($" operation[{i.i}]: {i.item}"); diff --git a/src/Nest/Document/Multiple/Bulk/ElasticClient-Bulk.cs b/src/Nest/Document/Multiple/Bulk/ElasticClient-Bulk.cs index 39b1eade027..dda825ccba1 100644 --- a/src/Nest/Document/Multiple/Bulk/ElasticClient-Bulk.cs +++ b/src/Nest/Document/Multiple/Bulk/ElasticClient-Bulk.cs @@ -7,7 +7,7 @@ namespace Nest public partial interface IElasticClient { /// - /// The bulk API makes it possible to perform many index/delete operations in a single API call. + /// The bulk API makes it possible to perform many index/delete operations in a single API call. /// This can greatly increase the indexing speed. /// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html /// @@ -28,23 +28,33 @@ public partial interface IElasticClient public partial class ElasticClient { /// - public IBulkResponse Bulk(IBulkRequest request) => + public IBulkResponse Bulk(IBulkRequest request) => this.Dispatcher.Dispatch( request, this.LowLevelDispatch.BulkDispatch ); /// - public IBulkResponse Bulk(Func selector = null) => - this.Bulk(selector.InvokeOrDefault(new BulkDescriptor())); + public IBulkResponse Bulk(Func selector = null) + { + // selector should not be nullable, but we can't change it for backwards compatibility reasons + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + return this.Bulk(selector.InvokeOrDefault(new BulkDescriptor())); + } /// - public Task BulkAsync(IBulkRequest request) => + public Task BulkAsync(IBulkRequest request) => this.Dispatcher.DispatchAsync( request, this.LowLevelDispatch.BulkDispatchAsync ); /// - public Task BulkAsync(Func selector = null) => - this.BulkAsync(selector.InvokeOrDefault(new BulkDescriptor())); + public Task BulkAsync(Func selector = null) + { + // selector should not be nullable, but we can't change it for backwards compatibility reasons + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + return this.BulkAsync(selector.InvokeOrDefault(new BulkDescriptor())); + } } -} \ No newline at end of file +}