| 1 |
package com.fasterxml.jackson.module.jaxb.id; |
| 2 |
|
| 3 |
import java.util.*; |
| 4 |
|
| 5 |
import javax.xml.bind.annotation.*; |
| 6 |
|
| 7 |
import com.fasterxml.jackson.core.type.TypeReference; |
| 8 |
import com.fasterxml.jackson.databind.*; |
| 9 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 10 |
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; |
| 11 |
|
| 12 |
// Reproduction of [Issue-9] |
| 13 |
public class TestXmlID2 extends BaseJaxbTest |
| 14 |
{ |
| 15 |
@XmlRootElement(name = "department") |
| 16 |
@XmlAccessorType(XmlAccessType.FIELD) |
| 17 |
static class Department { |
| 18 |
@XmlElement |
| 19 |
@XmlID |
| 20 |
public Long id; |
| 21 |
|
| 22 |
public String name; |
| 23 |
|
| 24 |
@XmlIDREF |
| 25 |
public List<User> employees = new ArrayList<User>(); |
| 26 |
|
| 27 |
protected Department() { } |
| 28 |
public Department(Long id) { |
| 29 |
this.id = id; |
| 30 |
} |
| 31 |
|
| 32 |
public void setId(Long id) { |
| 33 |
this.id = id; |
| 34 |
} |
| 35 |
|
| 36 |
public void setName(String name) { |
| 37 |
this.name = name; |
| 38 |
} |
| 39 |
|
| 40 |
public void setEmployees(List<User> employees) { |
| 41 |
this.employees = employees; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
@XmlRootElement(name = "user") |
| 47 |
@XmlAccessorType(XmlAccessType.FIELD) |
| 48 |
static class User |
| 49 |
{ |
| 50 |
@XmlElement @XmlID |
| 51 |
public Long id; |
| 52 |
|
| 53 |
public String username; |
| 54 |
public String email; |
| 55 |
|
| 56 |
@XmlIDREF |
| 57 |
public Department department; |
| 58 |
|
| 59 |
protected User() { } |
| 60 |
public User(Long id) { |
| 61 |
this.id = id; |
| 62 |
} |
| 63 |
|
| 64 |
public void setId(Long id) { |
| 65 |
this.id = id; |
| 66 |
} |
| 67 |
|
| 68 |
public void setUsername(String username) { |
| 69 |
this.username = username; |
| 70 |
} |
| 71 |
|
| 72 |
public void setEmail(String email) { |
| 73 |
this.email = email; |
| 74 |
} |
| 75 |
|
| 76 |
public void setDepartment(Department department) { |
| 77 |
this.department = department; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
private List<User> getUserList() |
| 82 |
{ |
| 83 |
List<User> resultList = new ArrayList<User>(); |
| 84 |
List<User> users = new java.util.ArrayList<User>(); |
| 85 |
|
| 86 |
User user1, user2, user3; |
| 87 |
Department dep; |
| 88 |
user1 = new User(11L); |
| 89 |
user1.setUsername("11"); |
| 90 |
user1.setEmail("[email protected]"); |
| 91 |
user2 = new User(22L); |
| 92 |
user2.setUsername("22"); |
| 93 |
user2.setEmail("[email protected]"); |
| 94 |
user3 = new User(33L); |
| 95 |
user3.setUsername("33"); |
| 96 |
user3.setEmail("[email protected]"); |
| 97 |
|
| 98 |
dep = new Department(9L); |
| 99 |
dep.setName("department9"); |
| 100 |
user1.setDepartment(dep); |
| 101 |
users.add(user1); |
| 102 |
user2.setDepartment(dep); |
| 103 |
users.add(user2); |
| 104 |
|
| 105 |
dep.setEmployees(users); |
| 106 |
resultList.clear(); |
| 107 |
resultList.add(user1); |
| 108 |
resultList.add(user2); |
| 109 |
resultList.add(user3); |
| 110 |
return resultList; |
| 111 |
} |
| 112 |
|
| 113 |
public void testIdWithJacksonRules() throws Exception |
| 114 |
{ |
| 115 |
String expected = "[{\"id\":11,\"username\":\"11\",\"email\":\"[email protected]\"," |
| 116 |
+"\"department\":{\"id\":9,\"name\":\"department9\",\"employees\":[" |
| 117 |
+"11,{\"id\":22,\"username\":\"22\",\"email\":\"[email protected]\"," |
| 118 |
+"\"department\":9}]}},22,{\"id\":33,\"username\":\"33\",\"email\":\"[email protected]\",\"department\":null}]"; |
| 119 |
ObjectMapper mapper = new ObjectMapper(); |
| 120 |
// true -> ignore XmlIDREF annotation |
| 121 |
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory(), true)); |
| 122 |
|
| 123 |
// first, with default settings (first NOT as id) |
| 124 |
List<User> users = getUserList(); |
| 125 |
String json = mapper.writeValueAsString(users); |
| 126 |
assertEquals(expected, json); |
| 127 |
|
| 128 |
List<User> result = mapper.readValue(json, new TypeReference<List<User>>() { }); |
| 129 |
assertEquals(3, result.size()); |
| 130 |
assertEquals(Long.valueOf(11), result.get(0).id); |
| 131 |
assertEquals(Long.valueOf(22), result.get(1).id); |
| 132 |
assertEquals(Long.valueOf(33), result.get(2).id); |
| 133 |
} |
| 134 |
|
| 135 |
public void testIdWithJaxbRules() throws Exception |
| 136 |
{ |
| 137 |
ObjectMapper mapper = new ObjectMapper(); |
| 138 |
// but then also variant where ID is ALWAYS used for XmlID / XmlIDREF |
| 139 |
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory())); |
| 140 |
List<User> users = getUserList(); |
| 141 |
final String json = mapper.writeValueAsString(users); |
| 142 |
String expected = "[{\"id\":11,\"username\":\"11\",\"email\":\"[email protected]\",\"department\":9}" |
| 143 |
+",{\"id\":22,\"username\":\"22\",\"email\":\"[email protected]\",\"department\":9}" |
| 144 |
+",{\"id\":33,\"username\":\"33\",\"email\":\"[email protected]\",\"department\":null}]"; |
| 145 |
|
| 146 |
assertEquals(expected, json); |
| 147 |
|
| 148 |
// However, there is no way to resolve those back, without some external mechanism... |
| 149 |
} |
| 150 |
} |