Skip to content

Commit e2ff116

Browse files
committed
impl
1 parent 2657ff3 commit e2ff116

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/AbstractDeserializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ protected Object _deserializeFromObjectId(JsonParser jp, DeserializationContext
195195
// do we have it resolved?
196196
Object pojo = roid.resolve();
197197
if (pojo == null) { // not yet; should wait...
198-
throw new IllegalStateException("Could not resolve Object Id ["+id+"] -- unresolved forward-reference?");
198+
throw new UnresolvedForwardReference("Could not resolve Object Id ["+id+"] -- unresolved forward-reference?", jp.getCurrentLocation(), roid);
199199
}
200200
return pojo;
201201
}
202202
}
203203

204+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fasterxml.jackson.databind.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonSubTypes;
7+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8+
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
9+
import com.fasterxml.jackson.core.JsonParseException;
10+
import com.fasterxml.jackson.databind.BaseMapTest;
11+
import com.fasterxml.jackson.databind.DeserializationFeature;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.SerializationFeature;
14+
15+
import java.io.IOException;
16+
import java.util.List;
17+
18+
/**
19+
* Test for testing forward reference handling
20+
*/
21+
public class TestForwardReference extends BaseMapTest {
22+
23+
private final ObjectMapper MAPPER = new ObjectMapper()
24+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
25+
.enable(SerializationFeature.INDENT_OUTPUT)
26+
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
27+
28+
/** Tests that we can read a hierarchical structure with forward references*/
29+
public void testForwardRef() throws IOException {
30+
MAPPER.readValue("{" +
31+
" \"@type\" : \"TestForwardReference$ForwardReferenceContainerClass\"," +
32+
" \"frc\" : \"willBeForwardReferenced\"," +
33+
" \"yac\" : {" +
34+
" \"@type\" : \"TestForwardReference$YetAnotherClass\"," +
35+
" \"frc\" : [ {" +
36+
" \"@type\" : \"Two\"," +
37+
" \"id\" : \"anotherInstance\"" +
38+
" }, {" +
39+
" \"@type\" : \"One\"," +
40+
" \"id\" : \"willBeForwardReferenced\"" +
41+
" } ]," +
42+
" \"id\" : \"anId\"\n" +
43+
" },\n" +
44+
" \"id\" : \"ForwardReferenceContainerClass1\"\n" +
45+
"}", ForwardReferenceContainerClass.class);
46+
47+
48+
}
49+
50+
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY)
51+
public static class ForwardReferenceContainerClass
52+
{
53+
public ForwardReferenceClass frc;
54+
public YetAnotherClass yac;
55+
public String id;
56+
}
57+
58+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
59+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
60+
@JsonSubTypes({
61+
@JsonSubTypes.Type(value = ForwardReferenceClassOne.class, name = "One"),
62+
@JsonSubTypes.Type(value = ForwardReferenceClassTwo.class, name = "Two")})
63+
private static abstract class ForwardReferenceClass
64+
{
65+
public String id;
66+
public void setId(String id) {
67+
this.id = id;
68+
}
69+
}
70+
71+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
72+
private static class YetAnotherClass
73+
{
74+
public YetAnotherClass() {}
75+
public List<ForwardReferenceClass> frc;
76+
public String id;
77+
}
78+
79+
public static class ForwardReferenceClassOne extends ForwardReferenceClass { }
80+
81+
public static class ForwardReferenceClassTwo extends ForwardReferenceClass { }
82+
}

0 commit comments

Comments
 (0)