logstash-plugins/logstash-filter-xml

Empty events set for failed xpath

inammathe opened this issue · 1 comments

This bug was introduced in #57
The issue it creates is for failed xpath queries e.g.

xml {
    source => "xmldata"
    target => "data"
    xpath => [ "//foo/text()","xpath_field" ]
}
<foobar>
    some text
</foobar>

Will result in empty [] fields being set in elastic.

This is due to the data variable being initialized and set outside of the normalized_nodeset loop with no condition for empty/nil value setting.
See below -

# Initialize empty resultset
data = []
normalized_nodeset.each do |value|
# some XPath functions return empty arrays as string
next if value.is_a?(Array) && value.length == 0
if value
matched = true
data << value.to_s
end
end
# set the destination attribute, if it's an array with a bigger size than one, leave as is. otherwise make it a string. added force_array param to provide same functionality as writing it in an xml target
if data.size == 1 && !@force_array
event.set(xpath_dest, data[0])
else
event.set(xpath_dest, data)
end

I propose #59 to address this issue by simply appending an unless data.nil? || data.empty? condition to the set operation. See - https://github.com/logstash-plugins/logstash-filter-xml/pull/59/files#diff-46633c67f32820575bd30a6a35b252c7R176

jsvd commented

fixed by #59