From c27511671b07f19c147f7dd52450a9345efef90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20G=C3=A9linas?= Date: Wed, 4 Dec 2013 17:59:06 -0500 Subject: [PATCH] Added a unit test for issue 81. --- .../dataformat/xml/types/TestPolymorphic.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/types/TestPolymorphic.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/types/TestPolymorphic.java index cfa1c6f6f..14d2f730c 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/types/TestPolymorphic.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/types/TestPolymorphic.java @@ -1,6 +1,11 @@ package com.fasterxml.jackson.dataformat.xml.types; +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlTestBase; @@ -36,6 +41,20 @@ public SubTypeWithClassObject() { } public SubTypeWithClassObject(String s) { name = s; } } + @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY) + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") + protected static class TypeWithClassPropertyAndObjectId { + public String id; + + public TypeWithClassPropertyAndObjectId(String id) { this.id = id; } + } + + protected static class Wrapper { + public List data; + + public Wrapper(List data) { this.data = data; } + } + /* /********************************************************** /* Set up @@ -86,5 +105,21 @@ public void testAsClassObject() throws Exception assertEquals(SubTypeWithClassObject.class, result.getClass()); assertEquals("Foobar", ((SubTypeWithClassObject) result).name); } + + /** + * Test for issue 81 + */ + public void testAsPropertyWithObjectId() throws Exception { + List data = new ArrayList(); + TypeWithClassPropertyAndObjectId object = new TypeWithClassPropertyAndObjectId("Foobar"); + data.add(object); + // This will be written as an id reference instead of object; as such, no type info will be written. + data.add(object); + String xml = _xmlMapper.writeValueAsString(new Wrapper(data)); + Wrapper result = _xmlMapper.readValue(xml, Wrapper.class); + assertNotNull(result); + assertSame(result.data.get(0), result.data.get(1)); + assertEquals("Foobar", result.data.get(0).id); + } } \ No newline at end of file