|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +// Tests for [#2821] |
| 11 | +public class TypeFactoryConstructTypeRegression2821Test extends BaseMapTest { |
| 12 | + static final class Wrapper { |
| 13 | + // if Entity<?> -> Entity , the test passes |
| 14 | + private final List<Entity<?>> entities; |
| 15 | + |
| 16 | + @JsonCreator |
| 17 | + public Wrapper(List<Entity<?>> entities) { |
| 18 | + this.entities = entities; |
| 19 | + } |
| 20 | + |
| 21 | + public List<Entity<?>> getEntities() { |
| 22 | + return this.entities; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public static class Entity<T> { |
| 27 | + @JsonIgnore |
| 28 | + private final Attributes attributes; |
| 29 | + |
| 30 | + private final T data; |
| 31 | + |
| 32 | + public Entity(Attributes attributes, T data) { |
| 33 | + this.attributes = attributes; |
| 34 | + this.data = data; |
| 35 | + } |
| 36 | + |
| 37 | + // if @JsonUnwrapped is removed, the test passes |
| 38 | + @JsonUnwrapped |
| 39 | + public Attributes getAttributes() { |
| 40 | + return attributes; |
| 41 | + } |
| 42 | + |
| 43 | + public T getData() { |
| 44 | + return data; |
| 45 | + } |
| 46 | + |
| 47 | + @JsonCreator |
| 48 | + public static <T> Entity<T> create(Attributes attributes, T data) { |
| 49 | + return new Entity<>(attributes, data); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static class Attributes { |
| 54 | + private final String id; |
| 55 | + |
| 56 | + public Attributes(String id) { |
| 57 | + this.id = id; |
| 58 | + } |
| 59 | + |
| 60 | + public String getId() { |
| 61 | + return id; |
| 62 | + } |
| 63 | + |
| 64 | + @JsonCreator |
| 65 | + public static Attributes create(String id) { |
| 66 | + return new Attributes(id); |
| 67 | + } |
| 68 | + |
| 69 | + // if this method is removed, the test passes |
| 70 | + public static Attributes dummyMethod(Map attributes) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // this test passes with Jackson 2.11.1, but fails with Jackson 2.11.2 |
| 76 | + public void testReproduceSerializerBug() throws JsonProcessingException { |
| 77 | + ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules(); |
| 78 | + Entity<String> entity = new Entity<>(new Attributes("id"), "hello"); |
| 79 | + Wrapper val = new Wrapper(Collections.<Entity<?>>singletonList(entity)); |
| 80 | + // fails with com.fasterxml.jackson.databind.JsonMappingException: Strange Map type java.util.Map: cannot determine type parameters (through reference chain: com.github.lhotari.jacksonbug.JacksonBugIsolatedTest$Wrapper["entities"]->java.util.Collections$SingletonList[0]->com.github.lhotari.jacksonbug.JacksonBugIsolatedTest$Entity["attributes"]) |
| 81 | + System.out.println(objectMapper.writeValueAsString(val)); |
| 82 | + } |
| 83 | +} |
0 commit comments