Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tools.jackson.databind.tofix;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.junit.jupiter.api.Test;
Expand All @@ -13,7 +14,7 @@
import tools.jackson.databind.testutil.DatabindTestUtil;
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;

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

class ObjectIdWithBuilder1496Test extends DatabindTestUtil {
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Expand Down Expand Up @@ -79,8 +80,54 @@ public POJO readFromCacheOrBuild() {
}
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonDeserialize(builder = EntityBuilder.class)
static class Entity
{
private long id;
private Entity ref;
private List<Entity> refs;

Entity(long id, Entity ref, List<Entity> refs) {
this.id = id;
this.ref = ref;
this.refs = refs;
}

public long getId() { return id; }
public Entity getRef() { return ref; }
public List<Entity> getRefs() { return refs; }
}

@JsonPOJOBuilder(withPrefix = "")
static class EntityBuilder
{
private long id;
private Entity ref;
private List<Entity> refs;

public EntityBuilder id(long id) { this.id = id; return this; }
public EntityBuilder ref(Entity ref) { this.ref = ref; return this; }
public EntityBuilder refs(List<Entity> refs) { this.refs = refs; return this; }

public Entity build() {
return new Entity(id, ref, refs);
}
}

static class EntityContainer
{
public List<Entity> entities;
}

private final ObjectMapper MAPPER = newJsonMapper();

/*
/**********************************************************************
/* Test cases
/**********************************************************************
*/

@JacksonTestFailureExpected
@Test
void builderId1496() throws Exception {
Expand All @@ -89,4 +136,58 @@ void builderId1496() throws Exception {
POJO result = MAPPER.readValue(json, POJO.class);
assertNotNull(result);
}

// Forward reference: first entity references second entity (which comes later)
@JacksonTestFailureExpected
@Test
public void testForwardReference() throws Exception
{
String json = a2q("{'entities':["
+ "{'id':1,'ref':2,'refs':[]},"
+ "{'id':2,'refs':[1]}"
+ "]}");

EntityContainer container = MAPPER.readValue(json, EntityContainer.class);
assertNotNull(container);
assertEquals(2, container.entities.size());

Entity first = container.entities.get(0);
Entity second = container.entities.get(1);

assertEquals(1, first.getId());
assertEquals(2, second.getId());

// first.ref -> second (forward reference)
assertSame(second, first.getRef());
// second.refs[0] -> first (back reference in collection)
assertEquals(1, second.getRefs().size());
assertSame(first, second.getRefs().get(0));
}

// Back reference: second entity references first entity (which came earlier)
@JacksonTestFailureExpected
@Test
public void testBackReference() throws Exception
{
String json = a2q("{'entities':["
+ "{'id':1,'refs':[2]},"
+ "{'id':2,'ref':1,'refs':[]}"
+ "]}");

EntityContainer container = MAPPER.readValue(json, EntityContainer.class);
assertNotNull(container);
assertEquals(2, container.entities.size());

Entity first = container.entities.get(0);
Entity second = container.entities.get(1);

assertEquals(1, first.getId());
assertEquals(2, second.getId());

// second.ref -> first (back reference)
assertSame(first, second.getRef());
// first.refs[0] -> second (forward reference in collection)
assertEquals(1, first.getRefs().size());
assertSame(second, first.getRefs().get(0));
}
}