Skip to content

Commit

Permalink
failing test for customer use case (#914) (#917)
Browse files Browse the repository at this point in the history
Fixes issue when a Class of X contains a field with a List<X> then WireMarshaller recursively attempts to resolve X. closes #915

Co-authored-by: Jerry Shea <[email protected]>
  • Loading branch information
David Ryan and JerryShea authored Jun 27, 2024
1 parent 6d96cb2 commit d293d9b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/WireMarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,8 @@ else if (type == Set.class)
componentType = extractClass(computeActualTypeArguments(Collection.class, field)[0]);
if (componentType != Object.class) {
isLeaf = !Throwable.class.isAssignableFrom(componentType)
// Don't recurse into the same class
&& !componentType.equals(field.getDeclaringClass())
&& WIRE_MARSHALLER_CL.get(componentType).isLeaf;
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/recursive/Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.openhft.chronicle.wire.recursive;

import net.openhft.chronicle.wire.AbstractEventCfg;

public class Base extends AbstractEventCfg<Base> {
private final String name;

public Base(String name) {
this.name = name;
}

public String name() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.openhft.chronicle.wire.recursive;

import net.openhft.chronicle.wire.WireMarshaller;
import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test for recursion in the marshaller and fields. WIRE_MARSHALLER_CL.get should not recurse while
* looking up fields of the class. At time of writing this occurs when checking if the component class
* of a subfield is a leaf and is only a problem when the component class is the same as the parent class.
*/
public class RecursiveTest {

@Test
public void referToBaseClass() {
test(new ReferToBaseClass("hello"), new ReferToBaseClass(null));
}

@Test
public void referToSameClass() {
test(new ReferToSameClass("test"), new ReferToSameClass(null));
}

@Test
public void marshallerReferToSameClass() {
WireMarshaller<?> marshaller= WireMarshaller.WIRE_MARSHALLER_CL.get(ReferToSameClass.class);
assertNotNull(marshaller);
}

@Test
public void marshallerReferToBaseClass() {
WireMarshaller<?> marshaller = WireMarshaller.WIRE_MARSHALLER_CL.get(ReferToBaseClass.class);
assertNotNull(marshaller);
}

private void test(Base from, Base to) {
from.copyTo(to);
assertEquals(from.name(), to.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.openhft.chronicle.wire.recursive;

import java.util.ArrayList;
import java.util.List;

public class ReferToBaseClass extends ReferToSameClass {
private final List<ReferToSameClass> list = new ArrayList<>();

public ReferToBaseClass(String name) {
super(name);
}

public List<ReferToSameClass> list() {
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.openhft.chronicle.wire.recursive;

import java.util.ArrayList;
import java.util.List;

public class ReferToSameClass extends Base {
private final List<ReferToSameClass> list = new ArrayList<>();

public ReferToSameClass(String name) {
super(name);
}

public List<ReferToSameClass> list() {
return list;
}
}

0 comments on commit d293d9b

Please sign in to comment.