eclipse-ee4j / eclipse-ee4j/jersey
MoxyObjectProvider does not handle classes with instance variables of same type correctly
- Dominant language
- Java
- Stars
- 730
- Forks
- 382
- PR merge metrics
- No merged PRs in 30d
Description
There seems to be a bug in how the MoxyObjectProvider breaks cycles in the object graph.
I think the bug is related to this code in the createSubgraphs(String, Subgraph, Class, Map, Set processed) method (line 123):
```
final String processedSubgraph = getProcessedSubgraph(entityClass, fieldName, subEntityClass);
if (!subgraphs.isEmpty() && !processed.contains(processedSubgraph)) {
processed.add(processedSubgraph);
createSubgraphs(path, subgraph, subEntityClass, subgraphs, processed);
}
```
I have a class looking like this:
```
public class Foo {
private Bar a;
private Bar b;
private Bar c;
}
```
All classes are with the appropriate getters, setters, and zero args constructor.
Bar looks like this:
```
public class Bar {
private Baz k1;
}
```
Baz looks like this:
```
public class Baz {
private Boz boz;
private String s;
}
```
Boz:
```
public class Boz {
private int a;
}
```
In this case the Bar instances are only serialised properly for the first encountered instance, the two other does not serialise properly as the string "Bar_k1_Baz" is now appearing in the processed set, and thus resulting in the other subgraphs getting ignored. This will later result in Moxy not generating the subgraphs. The result is below.
```
{
"a" : {
"k1" : {
"s" : "string A"
}
},
"b" : {
"k1" : {
"s" : "string B"
}
},
"c" : {
"k1" : {
"boz" : {
"a" : 24
},
"s" : "string C"
}
}
}
```
#### Environment
Using Jersey 2.17, running on Grizzly, using the Moxy to marshal to and from JSON. Running a resource with @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
#### Affected Versions
[2.17]
Contributor guide
Assessment
This issue has not been assessed yet.