Skip to content

Commit 884efab

Browse files
authored
Mixin deserialization reproduction (#3221)
1 parent bbb5c99 commit 884efab

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.fasterxml.jackson.databind.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonGetter;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Objects;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class MixingFactoryMethodTest {
14+
public static class Timestamped<T> {
15+
private final T value;
16+
private final int timestamp;
17+
18+
private Timestamped(T value, int timestamp) {
19+
this.value = value;
20+
this.timestamp = timestamp;
21+
}
22+
23+
public static <T> Timestamped<T> stamp(T value, int timestamp) {
24+
return new Timestamped<>(value, timestamp);
25+
}
26+
27+
public T getValue() {
28+
return value;
29+
}
30+
31+
public int getTimestamp() {
32+
return timestamp;
33+
}
34+
35+
@Override
36+
public boolean equals(Object o) {
37+
if (this == o) return true;
38+
if (o == null || getClass() != o.getClass()) return false;
39+
Timestamped<?> that = (Timestamped<?>) o;
40+
return timestamp == that.timestamp && Objects.equals(value, that.value);
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
return Objects.hash(value, timestamp);
46+
}
47+
}
48+
49+
public abstract static class TimestampedMixin<T> {
50+
@JsonCreator
51+
public static <T> void stamp(
52+
@JsonProperty("value") T value,
53+
@JsonProperty("timestamp") int timestamp
54+
) {
55+
}
56+
57+
@JsonGetter("value")
58+
abstract T getValue();
59+
60+
@JsonGetter("timestamp")
61+
abstract int getTimestamp();
62+
}
63+
64+
public static class Profile {
65+
private final String firstName;
66+
private final String lastName;
67+
68+
@JsonCreator
69+
public Profile(
70+
@JsonProperty("firstName")
71+
String firstName,
72+
@JsonProperty("lastName")
73+
String lastName
74+
) {
75+
this.firstName = firstName;
76+
this.lastName = lastName;
77+
}
78+
79+
@JsonGetter("firstName")
80+
public String getFirstName() {
81+
return firstName;
82+
}
83+
84+
@JsonGetter("lastName")
85+
public String getLastName() {
86+
return lastName;
87+
}
88+
89+
@Override
90+
public boolean equals(Object o) {
91+
if (this == o) return true;
92+
if (o == null || getClass() != o.getClass()) return false;
93+
Profile profile = (Profile) o;
94+
return Objects.equals(firstName, profile.firstName) && Objects.equals(lastName, profile.lastName);
95+
}
96+
97+
@Override
98+
public int hashCode() {
99+
return Objects.hash(firstName, lastName);
100+
}
101+
}
102+
103+
public static class User {
104+
private final Timestamped<Profile> profile;
105+
106+
@JsonCreator
107+
private User(
108+
@JsonProperty("profile")
109+
Timestamped<Profile> profile
110+
) {
111+
this.profile = profile;
112+
}
113+
114+
@JsonGetter("profile")
115+
public Timestamped<Profile> getProfile() {
116+
return profile;
117+
}
118+
119+
@Override
120+
public boolean equals(Object o) {
121+
if (this == o) return true;
122+
if (o == null || getClass() != o.getClass()) return false;
123+
User user = (User) o;
124+
return Objects.equals(profile, user.profile);
125+
}
126+
127+
@Override
128+
public int hashCode() {
129+
return Objects.hash(profile);
130+
}
131+
}
132+
133+
@Test
134+
public void testMixin() throws JsonProcessingException {
135+
ObjectMapper mapper = new ObjectMapper();
136+
mapper.addMixIn(Timestamped.class, TimestampedMixin.class);
137+
138+
Profile profile = new Profile("Jackson", "Databind");
139+
User user = new User(new Timestamped<>(profile, 1));
140+
141+
User deserializedUser = mapper.readValue(
142+
mapper.writerFor(User.class).writeValueAsString(user),
143+
User.class
144+
);
145+
146+
Profile deserializedProfile = deserializedUser.getProfile().getValue();
147+
148+
assertEquals(profile, deserializedProfile);
149+
assertEquals(user, deserializedUser);
150+
}
151+
}

0 commit comments

Comments
 (0)