Skip to content

Commit

Permalink
Test case for issue FasterXML#351.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Gélinas committed Nov 22, 2013
1 parent df554c6 commit ea2c4bf
Showing 1 changed file with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.struct.TestObjectId.Company;
import com.fasterxml.jackson.databind.struct.TestObjectId.Employee;

/**
* Unit test to verify handling of Object Id deserialization
Expand Down Expand Up @@ -166,7 +167,44 @@ public void testSimpleDeserWithForwardRefs() throws Exception
assertEquals(7, result.node.value);
assertSame(result.node, result.node.next.node);
}


public void testLateForwardReferenceInCollection() throws Exception
{
// This is the output of the TestObjectId#testMixedRefsIssue188 test method, so it's json generated by Jackson.
// Must be able to deser this if we generated it!
String json = "{\"employees\":["
+ "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]},"
+ "{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}"
+ "]}";
Company company = mapper.readValue(json, Company.class);
assertEquals(2, company.employees.size());
// Deser must keep object ordering.
Employee firstEmployee = company.employees.get(0);
Employee secondEmployee = company.employees.get(1);
assertEquals(1, firstEmployee.id);
assertEquals(2, secondEmployee.id);
assertSame(secondEmployee, firstEmployee.reports.get(0)); // Ensure that forward reference was properly resolved.
assertSame(firstEmployee, secondEmployee.manager); // And that back reference is also properly resolved.
}

// Variant of before but forward reference is not "wrapped" inside a collection, might be easier to fix first.
public void testLateForwardReference() throws Exception
{
String json = "{\"employees\":["
+ "{\"id\":1,\"name\":\"First\",\"manager\":2,\"reports\":[]},"
+ "{\"id\":2,\"name\":\"Second\",\"manager\":null,\"reports\":[1]}"
+ "]}";
Company company = mapper.readValue(json, Company.class);
assertEquals(2, company.employees.size());
// Deser must keep object ordering.
Employee firstEmployee = company.employees.get(0);
Employee secondEmployee = company.employees.get(1);
assertEquals(1, firstEmployee.id);
assertEquals(2, secondEmployee.id);
assertEquals(secondEmployee, firstEmployee.manager); // Ensure that forward reference was properly resolved.
assertEquals(firstEmployee, secondEmployee.reports.get(0)); // And that back reference is also properly resolved.
}

/*
/*****************************************************
/* Unit tests, custom (property-based) id deserialization
Expand Down

0 comments on commit ea2c4bf

Please sign in to comment.