Skip to content

@JacksonXmlProperty with attributes raises an exception when used with @JsonIdentityInfo #248

@lightbringer

Description

@lightbringer

I'm running into an issue with version 2.8.9, which might be related to issue #81 / #82: I'm trying to serialize an object graph with a circular reference using JsonIdentityInfo. This works well unless the referenced object has properties marked with @JacksonXmlProperty( isAttribute = true )

Minimal example below:

@JsonIdentityInfo( generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id" )
@JacksonXmlRootElement( localName = "a" )
public class A {
    public static void main( String[] args ) throws IOException {
        final A a = new A();
        a.b = new B(); 
        a.b.setA( a ); //B has a reference back to A
        
        final XmlMapper mapper = new XmlMapper();
        final String json = mapper.writeValueAsString( a );
        System.out.println( json );
    }

    private B b;

    private String name = "test";

    
    @JacksonXmlProperty( isAttribute = true ) //test runs with isAttribute set to false (but serializes name as an element)
    public String getName() {
        return name;
    }
 //other getter/setter omitted for brevity    

Expected output:

<a name="test">
<id>1</id>
<b>
<a>1</a>
</b>
</a>

Instead the exception:

 Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Unexpected IOException (of type java.io.IOException): javax.xml.stream.XMLStreamException: Trying to write an attribute when there is no open start element.
	at com.fasterxml.jackson.databind.JsonMappingException.fromUnexpectedIOE(JsonMappingException.java:333)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3077)
	at test.A.main(A.java:20)

is thrown. Removing the JsonIdentityInfo annotation solves the issue as well, i.e. name is correctly serialized as an attribute, but then a circular reference causes a stack overflow - obviously.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions