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