Skip to content

Commit

Permalink
Fix Cloner to properly clone ruby strings
Browse files Browse the repository at this point in the history
Currently, any invocation of clone, including pipeline->pipeline comms and the clone filter
won't actually clone the string data, just the structure. We may be missing other value types too.

This is a start however.

Fixes elastic#9652

Fixes elastic#9653
  • Loading branch information
andrewvc committed May 24, 2018
1 parent 91a23d5 commit c99c5a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions logstash-core/src/main/java/org/logstash/Cloner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.logstash;

import org.jruby.RubyString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -19,6 +21,8 @@ public static <T> T deep(final T input) {
return (T) deepMap((Map<?, ?>) input);
} else if (input instanceof List<?>) {
return (T) deepList((List<?>) input);
} else if (input instanceof RubyString) {
return (T) ((RubyString) input).doClone();
} else if (input instanceof Collection<?>) {
throw new ClassCastException("unexpected Collection type " + input.getClass());
}
Expand Down
26 changes: 26 additions & 0 deletions logstash-core/src/test/java/org/logstash/ClonerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.logstash;

import org.jruby.RubyString;
import org.junit.Test;

import static org.junit.Assert.*;

public class ClonerTest {
@Test
public void testRubyStringCloning() {
String javaString = "fooBar";
RubyString original = RubyString.newString(RubyUtil.RUBY, javaString);

RubyString result = Cloner.deep(original);
// Check object identity
assertTrue(result != original);

// Check different underlying bytes
assertTrue(result.getByteList() != original.getByteList());

// Check string equality
assertEquals(result, original);

assertEquals(javaString, result.asJavaString());
}
}

0 comments on commit c99c5a2

Please sign in to comment.