From 75c78cf569e2ffd7a7867936c29fefac23392cf6 Mon Sep 17 00:00:00 2001 From: Gabriel Moskovicz Date: Mon, 25 Apr 2016 11:23:56 -0300 Subject: [PATCH 1/2] added option to force array --- lib/logstash/filters/xml.rb | 6 +++++- spec/filters/xml_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/logstash/filters/xml.rb b/lib/logstash/filters/xml.rb index 2ba58f8..e960bc9 100644 --- a/lib/logstash/filters/xml.rb +++ b/lib/logstash/filters/xml.rb @@ -62,6 +62,10 @@ class LogStash::Filters::Xml < LogStash::Filters::Base # field as described above. Setting this to false will prevent that. config :store_xml, :validate => :boolean, :default => true + # By default the filter will force single elements to be arrays. Setting this to + # false will prevent storing single elements in arrays. + config :force_array, :validate => :boolean, :default => true + # By default only namespaces declarations on the root element are considered. # This allows to configure all namespace declarations to parse the XML document. # @@ -159,7 +163,7 @@ def filter(event) if @store_xml begin - event[@target] = XmlSimple.xml_in(value) + event[@target] = XmlSimple.xml_in(value, "ForceArray" => @force_array) matched = true rescue => e event.tag(XMLPARSEFAILURE_TAG) diff --git a/spec/filters/xml_spec.rb b/spec/filters/xml_spec.rb index c4b5b09..b1dfcc1 100644 --- a/spec/filters/xml_spec.rb +++ b/spec/filters/xml_spec.rb @@ -272,4 +272,38 @@ end end + + describe "parse with forcing array (Default)" do + config <<-CONFIG + filter { + xml { + source => "xmldata" + target => "parseddata" + } + } + CONFIG + + # Single value + sample("xmldata" => 'Content') do + insist { subject["parseddata"] } == { "bar" => ["Content"] } + end + end + + describe "parse disabling forcing array" do + config <<-CONFIG + filter { + xml { + source => "xmldata" + target => "parseddata" + force_array => false + } + } + CONFIG + + # Single value + sample("xmldata" => 'Content') do + insist { subject["parseddata"] } == { "bar" => "Content" } + end + end + end From c1aea51ce60085be2e169340283e657157fd452c Mon Sep 17 00:00:00 2001 From: Gabriel Moskovicz Date: Mon, 25 Apr 2016 11:33:54 -0300 Subject: [PATCH 2/2] modified changelog and bumped version --- CHANGELOG.md | 2 ++ CONTRIBUTORS | 1 + logstash-filter-xml.gemspec | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa9004b..2291b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +# 2.1.4 + - Added setting to disable forcing single values to be added in arrays. Ref: https://github.com/logstash-plugins/logstash-filter-xml/pull/28. # 2.1.3 - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash # 2.1.2 diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7741e3d..c081aee 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -10,6 +10,7 @@ Contributors: * Pier-Hugues Pellerin (ph) * Richard Pijnenburg (electrical) * Suyog Rao (suyograo) +* Gabriel Moskovicz (gmoskovicz) Note: If you've sent us patches, bug reports, or otherwise contributed to Logstash, and you aren't on the list above and want to be, please let us know diff --git a/logstash-filter-xml.gemspec b/logstash-filter-xml.gemspec index 5b9fb64..17ed62a 100644 --- a/logstash-filter-xml.gemspec +++ b/logstash-filter-xml.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'logstash-filter-xml' - s.version = '2.1.3' + s.version = '2.1.4' s.licenses = ['Apache License (2.0)'] s.summary = "Takes a field that contains XML and expands it into an actual datastructure." s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"