You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@JacksonInject with @JsonIdentityInfo causes deserialization failure (unresolved forward reference) #639
changed the title [-]Support mixing forward references with @JacksonInject[/-][+]@JsonInject causes deserialization failure (unresolved forward reference)[/+]on Dec 19, 2014
Actually I think this is different, and bit trickier. Problem is that there is an actual cycle from deserializer perspective, since parent2 may not be constructed before all JSON for it (and its descendants) are consumed; but its child child2 needs parent reference passed via constructor. So the solution here would be to remove @JsonCreator on child2 with non-creator alternative.
Except that in theory, since parent2 only needs injectable value, perhaps implementing #1268 would allow this to be handled even without removing of @JsonCreator.
and the reason this can not be resolved at this point is only partially due to @JacksonInject. The underlying problem is that neither Parent2 nor Child2 may be constructed before all data is available, as both have @JsonCreator annotated constructor.
Now, in theory Jackson could notice that the only parameter for Parent2 is injectable and create it immediately -- however, before #1381 is implemented, there is no way to indicate that something is purely injectable, that is, something that only comes via injection and not data. With #1268 it should be possible to define this, and with that, this problem would be solvable theoretically.
Jackson fails to deserialize forward references when a constructor parameter includes @JacksonInject, though the same forward references work fine without injection.
Expected: Both objects deserialize with proper circular reference resolution. Actual:MismatchedInputException: Cannot resolve ObjectId forward reference using property 'child' (of type Parent2): Bean not yet resolved
Root Cause Analysis
Deserialization Flow
Start deserializing Parent2 (will be id:1)
Parent2 uses property-based creator with @JacksonInject parameter
Bean cannot be fully constructed until all parameters are available
Deserialize child property → Start Child2 (will be id:2)
staticclassBeanReferringextendsReferring
{
privatefinalDeserializationContext_context;
privatefinalSettableBeanProperty_prop;
privateObject_bean;
// NEW: Store resolved value for later applicationprivateObject_resolvedValue;
publicvoidsetBean(Objectbean) throwsIOException {
_bean = bean;
// Apply deferred resolution if value was resolved before bean was availableif (_resolvedValue != null) {
// Type check to handle complex nested reference scenariosif (_prop.getType().getRawClass().isAssignableFrom(_resolvedValue.getClass())) {
_prop.set(_bean, _resolvedValue);
} else {
_resolvedValue = null;
}
}
}
@OverridepublicvoidhandleResolvedForwardReference(Objectid, Objectvalue) throwsIOException
{
if (_bean == null) {
// Defer: bean not yet available (likely due to injectable constructor params)_resolvedValue = value;
} else {
// Apply immediately: bean is ready_prop.set(_bean, value);
}
}
}
Issues Discovered
The fix revealed a deeper architectural problem: In complex nested scenarios with forward references in constructor parameters, the wrong BeanReferring receives resolution callbacks.
Observed behavior:
JSON: {"@id":1,"child":{"@id":2,"parent":1}}
The BeanReferring for Parent2's child property receives Parent2 (id:1) as the resolved value
Expected: It should receive Child2 (id:2)
The type mismatch check prevents the error, but child remains null
Why this happens:
Child2's constructor needs parent = forward ref to Parent2 (id:1)
This causes Child2 deserialization to throw UnresolvedForwardReference
The exception is caught at the child property level
A BeanReferring is created for the child property
This BeanReferring is registered with Parent2's ReadableObjectId (id:1)
When Parent2 is bound, it notifies ALL referrings registered with id:1
This includes the child property's BeanReferring, which receives Parent2 as the value
The architecture conflates two different concerns:
Constructor parameter forward reference: Child2.parent waiting for Parent2 (id:1)
Property forward reference: Parent2.child waiting for Child2 (id:2)
Currently, both get registered with the same ReadableObjectId, causing incorrect resolution callbacks.
Comparison with Working Test
Test #538 (ObjectIdWithInjectables538Test) - Works correctly:
Both classes have @JacksonInject in constructors
BUT: The circular references are in regular fields, not constructor parameters
Forward references can be resolved after objects are constructed
No timing issue with injectable parameters
Key difference: Constructor parameter forward references require the bean to exist before the forward reference can be resolved, creating a circular dependency that the current architecture doesn't handle when combined with injectable parameters.
Recommendations
Short-term Workaround
Users encountering this issue can work around it by:
Moving forward-referenced properties from constructor parameters to regular fields
Using a default constructor with @JacksonInject parameters
Setting circular references via regular setters after construction
Long-term Solution Options
Option 1: Separate Referring Types
Create a distinct CreatorPropertyReferring class for constructor parameter forward references that:
Stores the parameter value in PropertyValueBuffer rather than trying to set it immediately
Allows the bean to be constructed with the forward reference value
Properly distinguishes parameter-level from property-level forward references
Option 2: Delayed ObjectId Binding
Delay the bindItem() call until after all referrings have had setBean() called:
Modify PropertyBasedCreator.build() to NOT call handleIdValue() immediately
Return the bean and let the caller bind the ObjectId after setting up referrings
Requires significant refactoring of the creator/buffer flow
Option 3: Two-Phase Resolution
Implement two-phase forward reference resolution:
Phase 1: Collect all forward references without resolving
Phase 2: After all beans are constructed, resolve forward references
Most invasive but most robust solution
Testing Recommendations
Before implementing a fix:
Create additional test cases isolating different scenarios:
Injectable + forward ref in constructor param (current failing case)
Forward references in those same constructor parameters
The current architecture's timing of ObjectId binding (inside creator.build()) conflicts with the timing of BeanReferring initialization (after creator.build()). The attempted fix reveals that the problem is deeper than just timing - the forward reference registration mechanism doesn't properly distinguish between constructor parameter dependencies and property dependencies.
A proper fix will require careful architectural changes to one of the core deserialization flows, with thorough testing to ensure existing functionality isn't broken.
Analysis Date: 2025-11-25 Jackson Version: 2.21.0-SNAPSHOT (2.x branch) Analyst: Claude (Anthropic)
Activity
cowwoc commentedon Nov 28, 2014
Testcase: https://gist.github.com/cowwoc/743d2839bc7297a47516
Inlined
ObjectIdWithInjectable639Test.java:[-]Support mixing forward references with @JacksonInject[/-][+]@JsonInject causes deserialization failure (unresolved forward reference)[/+]cowtowncoder commentedon Jun 15, 2016
Actually I think this is different, and bit trickier. Problem is that there is an actual cycle from deserializer perspective, since
parent2may not be constructed before all JSON for it (and its descendants) are consumed; but its childchild2needs parent reference passed via constructor. So the solution here would be to remove@JsonCreatoronchild2with non-creator alternative.Except that in theory, since
parent2only needs injectable value, perhaps implementing #1268 would allow this to be handled even without removing of@JsonCreator.cowtowncoder commentedon Jul 19, 2016
So, the json output here is something like:
{"@id":1,"child":{"@id":2,"parent":1}}and the reason this can not be resolved at this point is only partially due to
@JacksonInject. The underlying problem is that neitherParent2norChild2may be constructed before all data is available, as both have@JsonCreatorannotated constructor.Now, in theory Jackson could notice that the only parameter for
Parent2is injectable and create it immediately -- however, before #1381 is implemented, there is no way to indicate that something is purely injectable, that is, something that only comes via injection and not data. With #1268 it should be possible to define this, and with that, this problem would be solvable theoretically.add failing test for #639
[-]@JsonInject causes deserialization failure (unresolved forward reference)[/-][+]`@JacksonInject` causes deserialization failure (unresolved forward reference)[/+]Remove dup test for #639
cowtowncoder commentedon Nov 26, 2025
Summary of investigation by Claude:
Issue #639: @JacksonInject with @JsonIdentityInfo Forward References - Analysis
Issue Summary
Issue: #639 - @JacksonInject with Forward References
Status: Open since November 2014
Test Case:
ObjectIdWithInjectable639Test.java(marked with@JacksonTestFailureExpected)Jackson fails to deserialize forward references when a constructor parameter includes
@JacksonInject, though the same forward references work fine without injection.Test Case
JSON:
{"@id":1,"child":{"@id":2,"parent":1}}Expected: Both objects deserialize with proper circular reference resolution.
Actual:
MismatchedInputException: Cannot resolve ObjectId forward reference using property 'child' (of type Parent2): Bean not yet resolvedRoot Cause Analysis
Deserialization Flow
Start deserializing Parent2 (will be id:1)
@JacksonInjectparameterDeserialize
childproperty → Start Child2 (will be id:2)parentparameter = forward reference to id:1 (Parent2)UnresolvedForwardReferenceCreate
BeanReferringfor thechildpropertyBeanDeserializer._deserializeUsingPropertyBased()line 496-506BeanReferringis createdReadableObjectIdComplete Parent2 construction
PropertyBasedCreator.build()calls_valueInstantiator.createFromObjectWith()buffer.handleIdValue(ctxt, bean)which callsroid.bindItem(bean)bindItem()immediately callshandleResolvedForwardReference()on all referringsBeanReferring._beanis stillnullat this point!Set bean on referrings (BeanDeserializer.java line 568-572)
creator.build()returnsreferring.setBean(bean)on allBeanReferringobjectsThe Timing Issue
The issue is that
bindItem()is called insidecreator.build(), butsetBean()is called aftercreator.build()returns.Why Injectables Cause This
Without
@JacksonInject:setBean()would be called before the bean is boundWith
@JacksonInject:PropertyValueBuffer.getParameters()handles injectionBeanReferring.setBean()is called_beanis still nullRelated Code Locations
Key Classes
BeanDeserializer.java:
UnresolvedForwardReferenceand createsBeanReferringcreator.build()creates bean and binds ObjectIdBeanReferringobjects (too late!)BeanReferringinner classPropertyBasedCreator.java:
build()methodcreateFromObjectWith()handleIdValue()which binds the ObjectIdPropertyValueBuffer.java:
handleIdValue()methodroid.bindItem(bean)which triggers forward reference resolutionReadableObjectId.java:
bindItem()methodhandleResolvedForwardReference()on all referringsAttempted Fix
Approach
Modify
BeanReferringto defer property assignment when bean isn't available yet:_resolvedValuefield to store the resolved valuehandleResolvedForwardReference(): if_beanis null, store value in_resolvedValuesetBean(): if_resolvedValueis set, apply it to the beanImplementation
File:
src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.javaIssues Discovered
The fix revealed a deeper architectural problem: In complex nested scenarios with forward references in constructor parameters, the wrong
BeanReferringreceives resolution callbacks.Observed behavior:
{"@id":1,"child":{"@id":2,"parent":1}}BeanReferringfor Parent2'schildproperty receives Parent2 (id:1) as the resolved valuechildremains nullWhy this happens:
parent= forward ref to Parent2 (id:1)UnresolvedForwardReferencechildproperty levelBeanReferringis created for thechildpropertyBeanReferringis registered with Parent2'sReadableObjectId(id:1)childproperty'sBeanReferring, which receives Parent2 as the valueThe architecture conflates two different concerns:
Currently, both get registered with the same
ReadableObjectId, causing incorrect resolution callbacks.Comparison with Working Test
Test #538 (
ObjectIdWithInjectables538Test) - Works correctly:@JacksonInjectin constructorsKey difference: Constructor parameter forward references require the bean to exist before the forward reference can be resolved, creating a circular dependency that the current architecture doesn't handle when combined with injectable parameters.
Recommendations
Short-term Workaround
Users encountering this issue can work around it by:
@JacksonInjectparametersLong-term Solution Options
Option 1: Separate Referring Types
Create a distinct
CreatorPropertyReferringclass for constructor parameter forward references that:PropertyValueBufferrather than trying to set it immediatelyOption 2: Delayed ObjectId Binding
Delay the
bindItem()call until after all referrings have hadsetBean()called:PropertyBasedCreator.build()to NOT callhandleIdValue()immediatelyOption 3: Two-Phase Resolution
Implement two-phase forward reference resolution:
Testing Recommendations
Before implementing a fix:
Conclusion
Issue #639 exposes a fundamental limitation in how Jackson handles the combination of:
The current architecture's timing of ObjectId binding (inside
creator.build()) conflicts with the timing ofBeanReferringinitialization (aftercreator.build()). The attempted fix reveals that the problem is deeper than just timing - the forward reference registration mechanism doesn't properly distinguish between constructor parameter dependencies and property dependencies.A proper fix will require careful architectural changes to one of the core deserialization flows, with thorough testing to ensure existing functionality isn't broken.
Analysis Date: 2025-11-25
Jackson Version: 2.21.0-SNAPSHOT (2.x branch)
Analyst: Claude (Anthropic)
Fix #3030 and #639
[-]`@JacksonInject` causes deserialization failure (unresolved forward reference)[/-][+]`@JacksonInject` with `@JsonIdentityInfo` causes deserialization failure (unresolved forward reference)[/+]Fix #3030 and #639 (#5758)
cowtowncoder commentedon Mar 11, 2026
2nd time's the charm; Claude crunched an acceptable fix, to be included in 3.2.0.