77import com .fasterxml .jackson .annotation .ObjectIdGenerators ;
88import com .fasterxml .jackson .databind .ObjectMapper ;
99import com .fasterxml .jackson .dataformat .yaml .ModuleTestBase ;
10+ import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
11+ import com .fasterxml .jackson .dataformat .yaml .YAMLAnchorReplayingFactory ;
1012import com .fasterxml .jackson .dataformat .yaml .testutil .failure .JacksonTestFailureExpected ;
1113
1214import static org .junit .jupiter .api .Assertions .assertEquals ;
1315import static org .junit .jupiter .api .Assertions .assertNotNull ;
1416import static org .junit .jupiter .api .Assertions .assertSame ;
1517
16- // [dataformats-text#296]: YAML Anchor and alias fails with ObjectIdGenerators.None
18+ /**
19+ * [dataformats-text#296]: YAML Anchor and alias with ObjectIdGenerators.None
20+ *<p>
21+ * NOTE: ObjectIdGenerators.None with YAML anchors/aliases currently does NOT work
22+ * as expected due to a limitation in Jackson's databind layer.
23+ *<p>
24+ * While the parser correctly exposes object IDs via getObjectId(), Jackson's
25+ * object ID resolution doesn't properly cache and reuse objects when using
26+ * ObjectIdGenerators.None, even with YAMLAnchorReplayingFactory.
27+ *<p>
28+ * WORKAROUND: Users should use ObjectIdGenerators.StringIdGenerator (or other
29+ * generators) instead of ObjectIdGenerators.None for YAML anchor/alias support.
30+ */
1731public class ObjectIdNone296Test extends ModuleTestBase
1832{
19- private final ObjectMapper MAPPER = newObjectMapper ();
33+ // YAMLAnchorReplayingFactory properly replays anchored events, but
34+ // ObjectIdGenerators.None still doesn't work for de-duplication
35+ private final ObjectMapper MAPPER = YAMLMapper .builder (new YAMLAnchorReplayingFactory ()).build ();
2036
2137 @ JacksonTestFailureExpected
2238 @ Test
@@ -28,10 +44,6 @@ public void testObjectIdUsingNativeAnchorsWithNoneGenerator() throws Exception
2844 " value: bar\n " +
2945 "boo: *foo1\n " ;
3046
31- // This should work: YAML anchors/aliases should be recognized natively
32- // when using ObjectIdGenerators.None, but currently fails with:
33- // "Cannot construct instance of StringHolder... no String-argument
34- // constructor/factory method to deserialize from String value ('foo1')"
3547 ScratchModel result = MAPPER .readValue (YAML_CONTENT , ScratchModel .class );
3648
3749 assertNotNull (result );
@@ -40,6 +52,7 @@ public void testObjectIdUsingNativeAnchorsWithNoneGenerator() throws Exception
4052 assertNotNull (result .boo );
4153 assertEquals ("bar" , result .boo .value );
4254 // The key assertion: both fields should point to the same object instance
55+ // Currently FAILS - creates two separate instances with same content
4356 assertSame (result .foo , result .boo );
4457 }
4558
@@ -54,7 +67,7 @@ static class StringHolder {
5467 public String value ;
5568
5669 protected StringHolder () { }
57-
70+
5871 @ JsonCreator (mode = JsonCreator .Mode .DELEGATING )
5972 public StringHolder (String v ) { value = v ; }
6073
0 commit comments