[backport 7.17] Avoid to itereate with each_index (#13603) (#13609)

Clean backport of #13603 to branch 7.17
(cherry picked from commit e27fdeb252)

----
Original commit message:

Sometime the deep_replace could be invoked by plugins, using the LogStash::Config::Mixin#validate.
This method receives a Ruby hash which could contains Java ArrayList instead of Ruby Array.
The iteration method `each_index` is not available for ArrayList, so resort to some form of "plain old way".
The reason why an ArrayList is recognized as a Ruby Array is due to the override classes, like RubyJavaIntegration.JavaCollectionOverride that monkey patches Ruby Array, so that a Java Collection could be seen as a RubyArray but it doesn't implement all the abstractions, like `each_index`.

Co-authored-by: Karol Bucek <kares@users.noreply.github.com>
Co-authored-by: Ry Biesemeyer <yaauie@users.noreply.github.com>
This commit is contained in:
Andrea Selva 2022-01-12 17:59:33 +01:00 committed by GitHub
parent d1e2816c47
commit 236cbcaade
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -37,8 +37,9 @@ module ::LogStash::Util::SubstitutionVariables
end
else
if value.is_a?(Array)
value.each_index do | valueArrayIndex|
value[valueArrayIndex] = deep_replace(value[valueArrayIndex])
value_array_index = 0
value.each_with_index do |single_value, i|
value[i] = deep_replace(single_value)
end
else
return replace_placeholders(value)