|
| 1 | +package com.fasterxml.jackson.databind.mixins; |
| 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 | +// 23-Aug-2021, tatu: UGGGGH. This is a recurring problem; users assuming |
| 12 | +// that a generic type parameter T (or whatever) of a Class will be related |
| 13 | +// to locally declared type parameter with same name (T), assume Creator |
| 14 | +// factory method will magically work. Despite Java Language quite clearly |
| 15 | +// considering these separate bindings regardless of name overlap. |
| 16 | +// |
| 17 | +// But for Jackson 2.x there is common enough usage of this anti-patterns so |
| 18 | +// we cannot block it. |
| 19 | +// |
| 20 | +// NOTE! Problem is actually not the mixin handling but type resolution; |
| 21 | +// see f.ex [databind#2821], [databind#2895] |
| 22 | +public class MixinForFactoryMethod3220Test |
| 23 | + extends BaseMapTest |
| 24 | +{ |
| 25 | + // [databind#3220] |
| 26 | + static class Timestamped<T> { |
| 27 | + private final T value; |
| 28 | + private final int timestamp; |
| 29 | + |
| 30 | + Timestamped(T value, int timestamp) { |
| 31 | + this.value = value; |
| 32 | + this.timestamp = timestamp; |
| 33 | + } |
| 34 | + |
| 35 | + public static <T> Timestamped<T> stamp(T value, int timestamp) { |
| 36 | + return new Timestamped<>(value, timestamp); |
| 37 | + } |
| 38 | + |
| 39 | + public T getValue() { |
| 40 | + return value; |
| 41 | + } |
| 42 | + |
| 43 | + public int getTimestamp() { |
| 44 | + return timestamp; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean equals(Object o) { |
| 49 | + if (this == o) return true; |
| 50 | + if (o == null || getClass() != o.getClass()) return false; |
| 51 | + Timestamped<?> that = (Timestamped<?>) o; |
| 52 | + return timestamp == that.timestamp && Objects.equals(value, that.value); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int hashCode() { |
| 57 | + return Objects.hash(value, timestamp); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + abstract static class TimestampedMixin<T> { |
| 62 | + @JsonCreator |
| 63 | + public static <T> void stamp( |
| 64 | + @JsonProperty("value") T value, |
| 65 | + @JsonProperty("timestamp") int timestamp) { |
| 66 | + } |
| 67 | + |
| 68 | + @JsonGetter("value") |
| 69 | + abstract T getValue(); |
| 70 | + |
| 71 | + @JsonGetter("timestamp") |
| 72 | + abstract int getTimestamp(); |
| 73 | + } |
| 74 | + |
| 75 | + static class Profile { |
| 76 | + private final String firstName; |
| 77 | + private final String lastName; |
| 78 | + |
| 79 | + @JsonCreator |
| 80 | + public Profile(@JsonProperty("firstName") String firstName, |
| 81 | + @JsonProperty("lastName") String lastName |
| 82 | + ) { |
| 83 | + this.firstName = firstName; |
| 84 | + this.lastName = lastName; |
| 85 | + } |
| 86 | + |
| 87 | + @JsonGetter("firstName") |
| 88 | + public String getFirstName() { |
| 89 | + return firstName; |
| 90 | + } |
| 91 | + |
| 92 | + @JsonGetter("lastName") |
| 93 | + public String getLastName() { |
| 94 | + return lastName; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean equals(Object o) { |
| 99 | + if (this == o) return true; |
| 100 | + if (o == null || getClass() != o.getClass()) return false; |
| 101 | + Profile profile = (Profile) o; |
| 102 | + return Objects.equals(firstName, profile.firstName) && Objects.equals(lastName, profile.lastName); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int hashCode() { |
| 107 | + return Objects.hash(firstName, lastName); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static class User { |
| 112 | + private final Timestamped<Profile> profile; |
| 113 | + |
| 114 | + @JsonCreator |
| 115 | + User(@JsonProperty("profile") Timestamped<Profile> profile) { |
| 116 | + this.profile = profile; |
| 117 | + } |
| 118 | + |
| 119 | + @JsonGetter("profile") |
| 120 | + public Timestamped<Profile> getProfile() { |
| 121 | + return profile; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean equals(Object o) { |
| 126 | + if (this == o) return true; |
| 127 | + if (o == null || getClass() != o.getClass()) return false; |
| 128 | + User user = (User) o; |
| 129 | + return Objects.equals(profile, user.profile); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int hashCode() { |
| 134 | + return Objects.hash(profile); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // [databind#3220] |
| 139 | + public void testMixin3220() throws Exception |
| 140 | + { |
| 141 | + ObjectMapper mapper = JsonMapper.builder() |
| 142 | + .addMixIn(Timestamped.class, TimestampedMixin.class) |
| 143 | + .build(); |
| 144 | + |
| 145 | + Profile profile = new Profile("Jackson", "Databind"); |
| 146 | + User user = new User(new Timestamped<>(profile, 1)); |
| 147 | + |
| 148 | + User deserializedUser = mapper.readValue( |
| 149 | + mapper.writerFor(User.class).writeValueAsString(user), |
| 150 | + User.class |
| 151 | + ); |
| 152 | + |
| 153 | + Timestamped<Profile> td = deserializedUser.getProfile(); |
| 154 | + Profile deserializedProfile = td.getValue(); |
| 155 | + assertEquals(profile, deserializedProfile); |
| 156 | + assertEquals(user, deserializedUser); |
| 157 | + } |
| 158 | +} |
0 commit comments