Skip to content

YAML anchors don't seem to work with @JsonCreator #22

Description

@ryegleason

Java code:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class)
class Student {
	private String name;
	private Integer age;

	@JsonCreator
        public Student(@JsonProperty("age") int age, @JsonProperty(value = "name") String name){
		this.name = name;
		this.age = age;
	}

        public String toString(){
            return "Student [ name: "+name+", age: "+ age+ " ]";
    }
}

public static void main(String args[]){
        try {
            YAMLMapper mapper = new YAMLMapper(new YAMLFactory());

            Student student = mapper.readValue(new File("student_yaml.yml"), Student.class);

	        System.out.println(student);
        }
        catch (Exception e) {
	        e.printStackTrace();
        }
    }

student_yaml.yml:

---
&1 name: "billy"
age: 5

Runtime Error:

com.fasterxml.jackson.databind.JsonMappingException: No Object Id found for an instance of Student, to assign to property '@id'
 at [Source: student_yaml.yml; line: 3, column: 7]
	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
	at com.fasterxml.jackson.databind.DeserializationContext.reportUnresolvedObjectId(DeserializationContext.java:1259)
	at com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer.handleIdValue(PropertyValueBuffer.java:241)
	at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:143)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:471)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1198)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1168)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:146)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740)
	at JacksonTester.main(JacksonTester.java:50)

So for this, when attempting to use the YAML anchor notation, it doesn't seem to be recognized. Everything works fine when I make Student stop using JsonCreator (and make the fields public, of course), or change student_yaml.yml to

---
'@id': 1
name: "billy"
age: 5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    yamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions