6
6
import com .fasterxml .jackson .core .*;
7
7
import com .fasterxml .jackson .databind .*;
8
8
import com .fasterxml .jackson .databind .deser .impl .*;
9
+ import com .fasterxml .jackson .databind .deser .impl .ReadableObjectId .Referring ;
9
10
import com .fasterxml .jackson .databind .util .NameTransformer ;
10
11
import com .fasterxml .jackson .databind .util .TokenBuffer ;
11
12
@@ -384,6 +385,7 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
384
385
TokenBuffer unknown = null ;
385
386
386
387
JsonToken t = p .getCurrentToken ();
388
+ List <BeanReferring > referrings = null ;
387
389
for (; t == JsonToken .FIELD_NAME ; t = p .nextToken ()) {
388
390
String propName = p .getCurrentName ();
389
391
p .nextToken (); // to point to value
@@ -426,11 +428,21 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
426
428
// regular property? needs buffering
427
429
SettableBeanProperty prop = _beanProperties .find (propName );
428
430
if (prop != null ) {
429
- buffer .bufferProperty (prop , _deserializeWithErrorWrapping (p , ctxt , prop ));
431
+ try {
432
+ buffer .bufferProperty (prop , _deserializeWithErrorWrapping (p , ctxt , prop ));
433
+ } catch (UnresolvedForwardReference reference ) {
434
+ // 14-Jun-2016, tatu: As per [databind#1261], looks like we need additional
435
+ // handling of forward references here. Not exactly sure why existing
436
+ // facilities did not cover, but this does appear to solve the problem
437
+ BeanReferring referring = handleUnresolvedReference (p , prop , buffer , reference );
438
+ if (referrings == null ) {
439
+ referrings = new ArrayList <BeanReferring >();
440
+ }
441
+ referrings .add (referring );
442
+ }
430
443
continue ;
431
444
}
432
- // As per [JACKSON-313], things marked as ignorable should not be
433
- // passed to any setter
445
+ // Things marked as ignorable should not be passed to any setter
434
446
if (_ignorableProps != null && _ignorableProps .contains (propName )) {
435
447
handleIgnoredProperty (p , ctxt , handledType (), propName );
436
448
continue ;
@@ -460,6 +472,11 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
460
472
wrapInstantiationProblem (e , ctxt );
461
473
bean = null ; // never gets here
462
474
}
475
+ if (referrings != null ) {
476
+ for (BeanReferring referring : referrings ) {
477
+ referring .setBean (bean );
478
+ }
479
+ }
463
480
if (unknown != null ) {
464
481
// polymorphic?
465
482
if (bean .getClass () != _beanType .getRawClass ()) {
@@ -471,6 +488,20 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
471
488
return bean ;
472
489
}
473
490
491
+ /**
492
+ * @since 2.8
493
+ */
494
+ private BeanReferring handleUnresolvedReference (JsonParser p ,
495
+ SettableBeanProperty prop , PropertyValueBuffer buffer ,
496
+ UnresolvedForwardReference reference )
497
+ throws JsonMappingException
498
+ {
499
+ BeanReferring referring = new BeanReferring (reference , prop .getType ().getRawClass (),
500
+ buffer , prop );
501
+ reference .getRoid ().appendReferring (referring );
502
+ return referring ;
503
+ }
504
+
474
505
protected final Object _deserializeWithErrorWrapping (JsonParser p ,
475
506
DeserializationContext ctxt , SettableBeanProperty prop )
476
507
throws IOException
@@ -920,4 +951,28 @@ protected Exception _creatorReturnedNullException() {
920
951
}
921
952
return _nullFromCreator ;
922
953
}
954
+
955
+ /**
956
+ * @since 2.8
957
+ */
958
+ static class BeanReferring extends Referring {
959
+ private final SettableBeanProperty _prop ;
960
+ private Object _bean ;
961
+
962
+ public void setBean (Object bean ) {
963
+ _bean = bean ;
964
+ }
965
+
966
+ BeanReferring (UnresolvedForwardReference ref ,
967
+ Class <?> valueType , PropertyValueBuffer buffer , SettableBeanProperty prop )
968
+ {
969
+ super (ref , valueType );
970
+ _prop = prop ;
971
+ }
972
+
973
+ @ Override
974
+ public void handleResolvedForwardReference (Object id , Object value ) throws IOException {
975
+ _prop .set (_bean , value );
976
+ }
977
+ }
923
978
}
0 commit comments