From 2a43b6a8705cc4e794da739378a7deb83249d8a5 Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Thu, 18 Apr 2024 14:00:56 +0100 Subject: [PATCH] [Gem] Fixes ES|QL Helper When passing in a parser, if the value was nil, the helper would error. Now it returns nil for nil values. --- .../lib/elasticsearch/helpers/esql_helper.rb | 2 +- .../integration/helpers/esql_helper_spec.rb | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/elasticsearch/lib/elasticsearch/helpers/esql_helper.rb b/elasticsearch/lib/elasticsearch/helpers/esql_helper.rb index 9105850967..5ba26e20b1 100644 --- a/elasticsearch/lib/elasticsearch/helpers/esql_helper.rb +++ b/elasticsearch/lib/elasticsearch/helpers/esql_helper.rb @@ -61,7 +61,7 @@ def self.query(client, query, params = {}, parser: {}) response['values'].map do |value| (value.length - 1).downto(0).map do |index| key = columns[index]['name'] - value[index] = parser[key].call value[index] if parser[key] + value[index] = parser[key].call(value[index]) if value[index] && parser[key] { key => value[index] } end.reduce({}, :merge) end diff --git a/elasticsearch/spec/integration/helpers/esql_helper_spec.rb b/elasticsearch/spec/integration/helpers/esql_helper_spec.rb index 83355aba45..12122c7157 100644 --- a/elasticsearch/spec/integration/helpers/esql_helper_spec.rb +++ b/elasticsearch/spec/integration/helpers/esql_helper_spec.rb @@ -16,6 +16,7 @@ # under the License. require_relative 'helpers_spec_helper' require 'elasticsearch/helpers/esql_helper' +require 'ipaddr' context 'Elasticsearch client helpers' do let(:index) { 'esql_helper_test' } @@ -76,8 +77,6 @@ end it 'parses iterated objects when procs are passed in' do - require 'ipaddr' - parser = { '@timestamp' => Proc.new { |t| DateTime.parse(t) }, 'client.ip' => Proc.new { |i| IPAddr.new(i) }, @@ -91,4 +90,29 @@ expect(r['event.duration']).to be_a String end end + + it 'parser does not error when value is nil, leaves nil' do + client.index( + index: index, + body: { + '@timestamp' => nil, + 'client.ip' => nil, + message: 'Connected to 10.1.0.1', + 'event.duration' => 1756465 + }, + refresh: true + ) + parser = { + '@timestamp' => Proc.new { |t| DateTime.parse(t) }, + 'client.ip' => Proc.new { |i| IPAddr.new(i) }, + 'event.duration' => Proc.new { |d| d.to_s } + } + response = esql_helper.query(client, query, parser: parser) + response.each do |r| + expect [DateTime, NilClass].include?(r['@timestamp'].class) + expect [IPAddr, NilClass].include?(r['client.ip'].class) + expect(r['message']).to be_a String + expect(r['event.duration']).to be_a String + end + end end