Skip to content

Commit cb7ea05

Browse files
cowtowncoderpjankovsky
authored andcommitted
1 parent c804727 commit cb7ea05

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Modules:
1414
(reported by jflefebvre06@github)
1515
#122: (csv) `readValues(null)` causes infinite loop
1616
(reported by andyeko@github)
17+
#123: (yaml) YAML Anchor, reference fails with simple example
1718

1819
2.9.8 (15-Dec-2018)
1920

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ public JsonToken nextToken() throws IOException
366366
return (_currToken = null);
367367
}
368368
_lastEvent = evt;
369-
370-
// One complication: field names are only inferred from the
371-
// fact that we are in Object context...
369+
// One complication: field names are only inferred from the fact that we are
370+
// in Object context; they are just ScalarEvents (but separate and NOT just tagged
371+
// on values)
372372
if (_parsingContext.inObject()) {
373373
if (_currToken != JsonToken.FIELD_NAME) {
374374
if (!evt.is(Event.ID.Scalar)) {
@@ -383,11 +383,20 @@ public JsonToken nextToken() throws IOException
383383
}
384384
_reportError("Expected a field name (Scalar value in YAML), got this instead: "+evt);
385385
}
386-
ScalarEvent scalar = (ScalarEvent) evt;
387-
String name = scalar.getValue();
386+
// 20-Feb-2019, tatu: [dataformats-text#123] Looks like YAML exposes Anchor for Object at point
387+
// where we return START_OBJECT (which makes sense), but, alas, Jackson expects that at point
388+
// after first FIELD_NAME. So we will need to defer clearing of the anchor slightly,
389+
// just for the very first entry; and only if no anchor for name found.
390+
// ... not even 100% sure this is correct, or robust, but does appear to work for specific
391+
// test case given.
392+
final ScalarEvent scalar = (ScalarEvent) evt;
393+
final String newAnchor = scalar.getAnchor();
394+
if ((newAnchor != null) || (_currToken != JsonToken.START_OBJECT)) {
395+
_currentAnchor = scalar.getAnchor();
396+
}
397+
final String name = scalar.getValue();
388398
_currentFieldName = name;
389399
_parsingContext.setCurrentName(name);
390-
_currentAnchor = scalar.getAnchor();
391400
return (_currToken = JsonToken.FIELD_NAME);
392401
}
393402
}
@@ -856,10 +865,8 @@ public String getTypeId() throws IOException, JsonGenerationException
856865
return null;
857866
}
858867
if (tag != null) {
859-
/* 04-Aug-2013, tatu: Looks like YAML parser's expose these in...
860-
* somewhat exotic ways sometimes. So let's prepare to peel off
861-
* some wrappings:
862-
*/
868+
// 04-Aug-2013, tatu: Looks like YAML parser's expose these in... somewhat exotic
869+
// ways sometimes. So let's prepare to peel off some wrappings:
863870
while (tag.startsWith("!")) {
864871
tag = tag.substring(1);
865872
}

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/ObjectIdTest.java renamed to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/ObjectIdWithTreeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Although native Object Ids work in general, Tree Model currently
1111
* has issues with it (see [dataformat-yaml#24])
1212
*/
13-
public class ObjectIdTest extends ModuleTestBase
13+
public class ObjectIdWithTreeTest extends ModuleTestBase
1414
{
1515
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
1616
static class Node
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fasterxml.jackson.dataformat.yaml.misc;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.dataformat.yaml.*;
6+
7+
//for [dataformats-text#123], problem with YAML, Object Ids
8+
public class ObjectId123Test extends ModuleTestBase
9+
{
10+
private final ObjectMapper MAPPER = newObjectMapper();
11+
12+
public void testObjectIdUsingNative() throws Exception
13+
{
14+
final String YAML_CONTENT =
15+
"foo: &foo1\n" +
16+
" value: bar\n" +
17+
"boo: *foo1\n";
18+
ScratchModel result = MAPPER.readValue(YAML_CONTENT, ScratchModel.class);
19+
assertNotNull(result);
20+
assertNotNull(result.foo);
21+
assertNotNull(result. boo);
22+
assertSame(result.foo, result.boo);
23+
}
24+
25+
static class ScratchModel {
26+
public StringHolder foo;
27+
public StringHolder boo;
28+
}
29+
30+
// @JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
31+
@JsonIdentityInfo(generator = ObjectIdGenerators.StringIdGenerator.class)
32+
static class StringHolder {
33+
public String value;
34+
35+
@Override
36+
public String toString() {
37+
return value;
38+
}
39+
}
40+
}

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/ObjectId63Test.java renamed to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ObjectId63Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.dataformat.yaml.failing;
1+
package com.fasterxml.jackson.dataformat.yaml.misc;
22

33
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
44
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

0 commit comments

Comments
 (0)