Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to force array #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/logstash/filters/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion logstash-filter-xml.gemspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
34 changes: 34 additions & 0 deletions spec/filters/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => '<foo><bar>Content</bar></foo>') 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" => '<foo><bar>Content</bar></foo>') do
insist { subject["parseddata"] } == { "bar" => "Content" }
end
end

end